cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

bq25890_charger.c (36140B)


      1// SPDX-License-Identifier: GPL-2.0-or-later
      2/*
      3 * TI BQ25890 charger driver
      4 *
      5 * Copyright (C) 2015 Intel Corporation
      6 */
      7
      8#include <linux/module.h>
      9#include <linux/i2c.h>
     10#include <linux/power_supply.h>
     11#include <linux/power/bq25890_charger.h>
     12#include <linux/regmap.h>
     13#include <linux/regulator/driver.h>
     14#include <linux/types.h>
     15#include <linux/gpio/consumer.h>
     16#include <linux/interrupt.h>
     17#include <linux/delay.h>
     18#include <linux/usb/phy.h>
     19
     20#include <linux/acpi.h>
     21#include <linux/of.h>
     22
     23#define BQ25890_MANUFACTURER		"Texas Instruments"
     24#define BQ25890_IRQ_PIN			"bq25890_irq"
     25
     26#define BQ25890_ID			3
     27#define BQ25895_ID			7
     28#define BQ25896_ID			0
     29
     30#define PUMP_EXPRESS_START_DELAY	(5 * HZ)
     31#define PUMP_EXPRESS_MAX_TRIES		6
     32#define PUMP_EXPRESS_VBUS_MARGIN_uV	1000000
     33
     34enum bq25890_chip_version {
     35	BQ25890,
     36	BQ25892,
     37	BQ25895,
     38	BQ25896,
     39};
     40
     41static const char *const bq25890_chip_name[] = {
     42	"BQ25890",
     43	"BQ25892",
     44	"BQ25895",
     45	"BQ25896",
     46};
     47
     48enum bq25890_fields {
     49	F_EN_HIZ, F_EN_ILIM, F_IINLIM,				     /* Reg00 */
     50	F_BHOT, F_BCOLD, F_VINDPM_OFS,				     /* Reg01 */
     51	F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
     52	F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN,	     /* Reg02 */
     53	F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
     54	F_MIN_VBAT_SEL,						     /* Reg03 */
     55	F_PUMPX_EN, F_ICHG,					     /* Reg04 */
     56	F_IPRECHG, F_ITERM,					     /* Reg05 */
     57	F_VREG, F_BATLOWV, F_VRECHG,				     /* Reg06 */
     58	F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
     59	F_JEITA_ISET,						     /* Reg07 */
     60	F_BATCMP, F_VCLAMP, F_TREG,				     /* Reg08 */
     61	F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
     62	F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN,	     /* Reg09 */
     63	F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI,			     /* Reg0A */
     64	F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
     65	F_VSYS_STAT,						     /* Reg0B */
     66	F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
     67	F_NTC_FAULT,						     /* Reg0C */
     68	F_FORCE_VINDPM, F_VINDPM,				     /* Reg0D */
     69	F_THERM_STAT, F_BATV,					     /* Reg0E */
     70	F_SYSV,							     /* Reg0F */
     71	F_TSPCT,						     /* Reg10 */
     72	F_VBUS_GD, F_VBUSV,					     /* Reg11 */
     73	F_ICHGR,						     /* Reg12 */
     74	F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM,			     /* Reg13 */
     75	F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV,   /* Reg14 */
     76
     77	F_MAX_FIELDS
     78};
     79
     80/* initial field values, converted to register values */
     81struct bq25890_init_data {
     82	u8 ichg;	/* charge current		*/
     83	u8 vreg;	/* regulation voltage		*/
     84	u8 iterm;	/* termination current		*/
     85	u8 iprechg;	/* precharge current		*/
     86	u8 sysvmin;	/* minimum system voltage limit */
     87	u8 boostv;	/* boost regulation voltage	*/
     88	u8 boosti;	/* boost current limit		*/
     89	u8 boostf;	/* boost frequency		*/
     90	u8 ilim_en;	/* enable ILIM pin		*/
     91	u8 treg;	/* thermal regulation threshold */
     92	u8 rbatcomp;	/* IBAT sense resistor value    */
     93	u8 vclamp;	/* IBAT compensation voltage limit */
     94};
     95
     96struct bq25890_state {
     97	u8 online;
     98	u8 chrg_status;
     99	u8 chrg_fault;
    100	u8 vsys_status;
    101	u8 boost_fault;
    102	u8 bat_fault;
    103	u8 ntc_fault;
    104};
    105
    106struct bq25890_device {
    107	struct i2c_client *client;
    108	struct device *dev;
    109	struct power_supply *charger;
    110
    111	struct usb_phy *usb_phy;
    112	struct notifier_block usb_nb;
    113	struct work_struct usb_work;
    114	struct delayed_work pump_express_work;
    115	unsigned long usb_event;
    116
    117	struct regmap *rmap;
    118	struct regmap_field *rmap_fields[F_MAX_FIELDS];
    119
    120	bool skip_reset;
    121	bool read_back_init_data;
    122	u32 pump_express_vbus_max;
    123	enum bq25890_chip_version chip_version;
    124	struct bq25890_init_data init_data;
    125	struct bq25890_state state;
    126
    127	struct mutex lock; /* protect state data */
    128};
    129
    130static const struct regmap_range bq25890_readonly_reg_ranges[] = {
    131	regmap_reg_range(0x0b, 0x0c),
    132	regmap_reg_range(0x0e, 0x13),
    133};
    134
    135static const struct regmap_access_table bq25890_writeable_regs = {
    136	.no_ranges = bq25890_readonly_reg_ranges,
    137	.n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
    138};
    139
    140static const struct regmap_range bq25890_volatile_reg_ranges[] = {
    141	regmap_reg_range(0x00, 0x00),
    142	regmap_reg_range(0x02, 0x02),
    143	regmap_reg_range(0x09, 0x09),
    144	regmap_reg_range(0x0b, 0x14),
    145};
    146
    147static const struct regmap_access_table bq25890_volatile_regs = {
    148	.yes_ranges = bq25890_volatile_reg_ranges,
    149	.n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
    150};
    151
    152static const struct regmap_config bq25890_regmap_config = {
    153	.reg_bits = 8,
    154	.val_bits = 8,
    155
    156	.max_register = 0x14,
    157	.cache_type = REGCACHE_RBTREE,
    158
    159	.wr_table = &bq25890_writeable_regs,
    160	.volatile_table = &bq25890_volatile_regs,
    161};
    162
    163static const struct reg_field bq25890_reg_fields[] = {
    164	/* REG00 */
    165	[F_EN_HIZ]		= REG_FIELD(0x00, 7, 7),
    166	[F_EN_ILIM]		= REG_FIELD(0x00, 6, 6),
    167	[F_IINLIM]		= REG_FIELD(0x00, 0, 5),
    168	/* REG01 */
    169	[F_BHOT]		= REG_FIELD(0x01, 6, 7),
    170	[F_BCOLD]		= REG_FIELD(0x01, 5, 5),
    171	[F_VINDPM_OFS]		= REG_FIELD(0x01, 0, 4),
    172	/* REG02 */
    173	[F_CONV_START]		= REG_FIELD(0x02, 7, 7),
    174	[F_CONV_RATE]		= REG_FIELD(0x02, 6, 6),
    175	[F_BOOSTF]		= REG_FIELD(0x02, 5, 5),
    176	[F_ICO_EN]		= REG_FIELD(0x02, 4, 4),
    177	[F_HVDCP_EN]		= REG_FIELD(0x02, 3, 3),  // reserved on BQ25896
    178	[F_MAXC_EN]		= REG_FIELD(0x02, 2, 2),  // reserved on BQ25896
    179	[F_FORCE_DPM]		= REG_FIELD(0x02, 1, 1),
    180	[F_AUTO_DPDM_EN]	= REG_FIELD(0x02, 0, 0),
    181	/* REG03 */
    182	[F_BAT_LOAD_EN]		= REG_FIELD(0x03, 7, 7),
    183	[F_WD_RST]		= REG_FIELD(0x03, 6, 6),
    184	[F_OTG_CFG]		= REG_FIELD(0x03, 5, 5),
    185	[F_CHG_CFG]		= REG_FIELD(0x03, 4, 4),
    186	[F_SYSVMIN]		= REG_FIELD(0x03, 1, 3),
    187	[F_MIN_VBAT_SEL]	= REG_FIELD(0x03, 0, 0), // BQ25896 only
    188	/* REG04 */
    189	[F_PUMPX_EN]		= REG_FIELD(0x04, 7, 7),
    190	[F_ICHG]		= REG_FIELD(0x04, 0, 6),
    191	/* REG05 */
    192	[F_IPRECHG]		= REG_FIELD(0x05, 4, 7),
    193	[F_ITERM]		= REG_FIELD(0x05, 0, 3),
    194	/* REG06 */
    195	[F_VREG]		= REG_FIELD(0x06, 2, 7),
    196	[F_BATLOWV]		= REG_FIELD(0x06, 1, 1),
    197	[F_VRECHG]		= REG_FIELD(0x06, 0, 0),
    198	/* REG07 */
    199	[F_TERM_EN]		= REG_FIELD(0x07, 7, 7),
    200	[F_STAT_DIS]		= REG_FIELD(0x07, 6, 6),
    201	[F_WD]			= REG_FIELD(0x07, 4, 5),
    202	[F_TMR_EN]		= REG_FIELD(0x07, 3, 3),
    203	[F_CHG_TMR]		= REG_FIELD(0x07, 1, 2),
    204	[F_JEITA_ISET]		= REG_FIELD(0x07, 0, 0), // reserved on BQ25895
    205	/* REG08 */
    206	[F_BATCMP]		= REG_FIELD(0x08, 5, 7),
    207	[F_VCLAMP]		= REG_FIELD(0x08, 2, 4),
    208	[F_TREG]		= REG_FIELD(0x08, 0, 1),
    209	/* REG09 */
    210	[F_FORCE_ICO]		= REG_FIELD(0x09, 7, 7),
    211	[F_TMR2X_EN]		= REG_FIELD(0x09, 6, 6),
    212	[F_BATFET_DIS]		= REG_FIELD(0x09, 5, 5),
    213	[F_JEITA_VSET]		= REG_FIELD(0x09, 4, 4), // reserved on BQ25895
    214	[F_BATFET_DLY]		= REG_FIELD(0x09, 3, 3),
    215	[F_BATFET_RST_EN]	= REG_FIELD(0x09, 2, 2),
    216	[F_PUMPX_UP]		= REG_FIELD(0x09, 1, 1),
    217	[F_PUMPX_DN]		= REG_FIELD(0x09, 0, 0),
    218	/* REG0A */
    219	[F_BOOSTV]		= REG_FIELD(0x0A, 4, 7),
    220	[F_BOOSTI]		= REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
    221	[F_PFM_OTG_DIS]		= REG_FIELD(0x0A, 3, 3), // BQ25896 only
    222	/* REG0B */
    223	[F_VBUS_STAT]		= REG_FIELD(0x0B, 5, 7),
    224	[F_CHG_STAT]		= REG_FIELD(0x0B, 3, 4),
    225	[F_PG_STAT]		= REG_FIELD(0x0B, 2, 2),
    226	[F_SDP_STAT]		= REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
    227	[F_VSYS_STAT]		= REG_FIELD(0x0B, 0, 0),
    228	/* REG0C */
    229	[F_WD_FAULT]		= REG_FIELD(0x0C, 7, 7),
    230	[F_BOOST_FAULT]		= REG_FIELD(0x0C, 6, 6),
    231	[F_CHG_FAULT]		= REG_FIELD(0x0C, 4, 5),
    232	[F_BAT_FAULT]		= REG_FIELD(0x0C, 3, 3),
    233	[F_NTC_FAULT]		= REG_FIELD(0x0C, 0, 2),
    234	/* REG0D */
    235	[F_FORCE_VINDPM]	= REG_FIELD(0x0D, 7, 7),
    236	[F_VINDPM]		= REG_FIELD(0x0D, 0, 6),
    237	/* REG0E */
    238	[F_THERM_STAT]		= REG_FIELD(0x0E, 7, 7),
    239	[F_BATV]		= REG_FIELD(0x0E, 0, 6),
    240	/* REG0F */
    241	[F_SYSV]		= REG_FIELD(0x0F, 0, 6),
    242	/* REG10 */
    243	[F_TSPCT]		= REG_FIELD(0x10, 0, 6),
    244	/* REG11 */
    245	[F_VBUS_GD]		= REG_FIELD(0x11, 7, 7),
    246	[F_VBUSV]		= REG_FIELD(0x11, 0, 6),
    247	/* REG12 */
    248	[F_ICHGR]		= REG_FIELD(0x12, 0, 6),
    249	/* REG13 */
    250	[F_VDPM_STAT]		= REG_FIELD(0x13, 7, 7),
    251	[F_IDPM_STAT]		= REG_FIELD(0x13, 6, 6),
    252	[F_IDPM_LIM]		= REG_FIELD(0x13, 0, 5),
    253	/* REG14 */
    254	[F_REG_RST]		= REG_FIELD(0x14, 7, 7),
    255	[F_ICO_OPTIMIZED]	= REG_FIELD(0x14, 6, 6),
    256	[F_PN]			= REG_FIELD(0x14, 3, 5),
    257	[F_TS_PROFILE]		= REG_FIELD(0x14, 2, 2),
    258	[F_DEV_REV]		= REG_FIELD(0x14, 0, 1)
    259};
    260
    261/*
    262 * Most of the val -> idx conversions can be computed, given the minimum,
    263 * maximum and the step between values. For the rest of conversions, we use
    264 * lookup tables.
    265 */
    266enum bq25890_table_ids {
    267	/* range tables */
    268	TBL_ICHG,
    269	TBL_ITERM,
    270	TBL_IINLIM,
    271	TBL_VREG,
    272	TBL_BOOSTV,
    273	TBL_SYSVMIN,
    274	TBL_VBUSV,
    275	TBL_VBATCOMP,
    276	TBL_RBATCOMP,
    277
    278	/* lookup tables */
    279	TBL_TREG,
    280	TBL_BOOSTI,
    281	TBL_TSPCT,
    282};
    283
    284/* Thermal Regulation Threshold lookup table, in degrees Celsius */
    285static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
    286
    287#define BQ25890_TREG_TBL_SIZE		ARRAY_SIZE(bq25890_treg_tbl)
    288
    289/* Boost mode current limit lookup table, in uA */
    290static const u32 bq25890_boosti_tbl[] = {
    291	500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
    292};
    293
    294#define BQ25890_BOOSTI_TBL_SIZE		ARRAY_SIZE(bq25890_boosti_tbl)
    295
    296/* NTC 10K temperature lookup table in tenths of a degree */
    297static const u32 bq25890_tspct_tbl[] = {
    298	850, 840, 830, 820, 810, 800, 790, 780,
    299	770, 760, 750, 740, 730, 720, 710, 700,
    300	690, 685, 680, 675, 670, 660, 650, 645,
    301	640, 630, 620, 615, 610, 600, 590, 585,
    302	580, 570, 565, 560, 550, 540, 535, 530,
    303	520, 515, 510, 500, 495, 490, 480, 475,
    304	470, 460, 455, 450, 440, 435, 430, 425,
    305	420, 410, 405, 400, 390, 385, 380, 370,
    306	365, 360, 355, 350, 340, 335, 330, 320,
    307	310, 305, 300, 290, 285, 280, 275, 270,
    308	260, 250, 245, 240, 230, 225, 220, 210,
    309	205, 200, 190, 180, 175, 170, 160, 150,
    310	145, 140, 130, 120, 115, 110, 100, 90,
    311	80, 70, 60, 50, 40, 30, 20, 10,
    312	0, -10, -20, -30, -40, -60, -70, -80,
    313	-90, -10, -120, -140, -150, -170, -190, -210,
    314};
    315
    316#define BQ25890_TSPCT_TBL_SIZE		ARRAY_SIZE(bq25890_tspct_tbl)
    317
    318struct bq25890_range {
    319	u32 min;
    320	u32 max;
    321	u32 step;
    322};
    323
    324struct bq25890_lookup {
    325	const u32 *tbl;
    326	u32 size;
    327};
    328
    329static const union {
    330	struct bq25890_range  rt;
    331	struct bq25890_lookup lt;
    332} bq25890_tables[] = {
    333	/* range tables */
    334	/* TODO: BQ25896 has max ICHG 3008 mA */
    335	[TBL_ICHG] =	 { .rt = {0,        5056000, 64000} },	 /* uA */
    336	[TBL_ITERM] =	 { .rt = {64000,    1024000, 64000} },	 /* uA */
    337	[TBL_IINLIM] =   { .rt = {100000,   3250000, 50000} },	 /* uA */
    338	[TBL_VREG] =	 { .rt = {3840000,  4608000, 16000} },	 /* uV */
    339	[TBL_BOOSTV] =	 { .rt = {4550000,  5510000, 64000} },	 /* uV */
    340	[TBL_SYSVMIN] =  { .rt = {3000000,  3700000, 100000} },	 /* uV */
    341	[TBL_VBUSV] =	 { .rt = {2600000, 15300000, 100000} },	 /* uV */
    342	[TBL_VBATCOMP] = { .rt = {0,         224000, 32000} },	 /* uV */
    343	[TBL_RBATCOMP] = { .rt = {0,         140000, 20000} },	 /* uOhm */
    344
    345	/* lookup tables */
    346	[TBL_TREG] =	{ .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
    347	[TBL_BOOSTI] =	{ .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} },
    348	[TBL_TSPCT] =	{ .lt = {bq25890_tspct_tbl, BQ25890_TSPCT_TBL_SIZE} }
    349};
    350
    351static int bq25890_field_read(struct bq25890_device *bq,
    352			      enum bq25890_fields field_id)
    353{
    354	int ret;
    355	int val;
    356
    357	ret = regmap_field_read(bq->rmap_fields[field_id], &val);
    358	if (ret < 0)
    359		return ret;
    360
    361	return val;
    362}
    363
    364static int bq25890_field_write(struct bq25890_device *bq,
    365			       enum bq25890_fields field_id, u8 val)
    366{
    367	return regmap_field_write(bq->rmap_fields[field_id], val);
    368}
    369
    370static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
    371{
    372	u8 idx;
    373
    374	if (id >= TBL_TREG) {
    375		const u32 *tbl = bq25890_tables[id].lt.tbl;
    376		u32 tbl_size = bq25890_tables[id].lt.size;
    377
    378		for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
    379			;
    380	} else {
    381		const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
    382		u8 rtbl_size;
    383
    384		rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
    385
    386		for (idx = 1;
    387		     idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
    388		     idx++)
    389			;
    390	}
    391
    392	return idx - 1;
    393}
    394
    395static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
    396{
    397	const struct bq25890_range *rtbl;
    398
    399	/* lookup table? */
    400	if (id >= TBL_TREG)
    401		return bq25890_tables[id].lt.tbl[idx];
    402
    403	/* range table */
    404	rtbl = &bq25890_tables[id].rt;
    405
    406	return (rtbl->min + idx * rtbl->step);
    407}
    408
    409enum bq25890_status {
    410	STATUS_NOT_CHARGING,
    411	STATUS_PRE_CHARGING,
    412	STATUS_FAST_CHARGING,
    413	STATUS_TERMINATION_DONE,
    414};
    415
    416enum bq25890_chrg_fault {
    417	CHRG_FAULT_NORMAL,
    418	CHRG_FAULT_INPUT,
    419	CHRG_FAULT_THERMAL_SHUTDOWN,
    420	CHRG_FAULT_TIMER_EXPIRED,
    421};
    422
    423enum bq25890_ntc_fault {
    424	NTC_FAULT_NORMAL = 0,
    425	NTC_FAULT_WARM = 2,
    426	NTC_FAULT_COOL = 3,
    427	NTC_FAULT_COLD = 5,
    428	NTC_FAULT_HOT = 6,
    429};
    430
    431static bool bq25890_is_adc_property(enum power_supply_property psp)
    432{
    433	switch (psp) {
    434	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
    435	case POWER_SUPPLY_PROP_CURRENT_NOW:
    436	case POWER_SUPPLY_PROP_TEMP:
    437		return true;
    438
    439	default:
    440		return false;
    441	}
    442}
    443
    444static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
    445
    446static int bq25890_get_vbus_voltage(struct bq25890_device *bq)
    447{
    448	int ret;
    449
    450	ret = bq25890_field_read(bq, F_VBUSV);
    451	if (ret < 0)
    452		return ret;
    453
    454	return bq25890_find_val(ret, TBL_VBUSV);
    455}
    456
    457static int bq25890_power_supply_get_property(struct power_supply *psy,
    458					     enum power_supply_property psp,
    459					     union power_supply_propval *val)
    460{
    461	struct bq25890_device *bq = power_supply_get_drvdata(psy);
    462	struct bq25890_state state;
    463	bool do_adc_conv;
    464	int ret;
    465
    466	mutex_lock(&bq->lock);
    467	/* update state in case we lost an interrupt */
    468	__bq25890_handle_irq(bq);
    469	state = bq->state;
    470	do_adc_conv = !state.online && bq25890_is_adc_property(psp);
    471	if (do_adc_conv)
    472		bq25890_field_write(bq, F_CONV_START, 1);
    473	mutex_unlock(&bq->lock);
    474
    475	if (do_adc_conv)
    476		regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
    477			ret, !ret, 25000, 1000000);
    478
    479	switch (psp) {
    480	case POWER_SUPPLY_PROP_STATUS:
    481		if (!state.online)
    482			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
    483		else if (state.chrg_status == STATUS_NOT_CHARGING)
    484			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
    485		else if (state.chrg_status == STATUS_PRE_CHARGING ||
    486			 state.chrg_status == STATUS_FAST_CHARGING)
    487			val->intval = POWER_SUPPLY_STATUS_CHARGING;
    488		else if (state.chrg_status == STATUS_TERMINATION_DONE)
    489			val->intval = POWER_SUPPLY_STATUS_FULL;
    490		else
    491			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
    492
    493		break;
    494
    495	case POWER_SUPPLY_PROP_CHARGE_TYPE:
    496		if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
    497		    state.chrg_status == STATUS_TERMINATION_DONE)
    498			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
    499		else if (state.chrg_status == STATUS_PRE_CHARGING)
    500			val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
    501		else if (state.chrg_status == STATUS_FAST_CHARGING)
    502			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
    503		else /* unreachable */
    504			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
    505		break;
    506
    507	case POWER_SUPPLY_PROP_MANUFACTURER:
    508		val->strval = BQ25890_MANUFACTURER;
    509		break;
    510
    511	case POWER_SUPPLY_PROP_MODEL_NAME:
    512		val->strval = bq25890_chip_name[bq->chip_version];
    513		break;
    514
    515	case POWER_SUPPLY_PROP_ONLINE:
    516		val->intval = state.online;
    517		break;
    518
    519	case POWER_SUPPLY_PROP_HEALTH:
    520		if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
    521			val->intval = POWER_SUPPLY_HEALTH_GOOD;
    522		else if (state.bat_fault)
    523			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
    524		else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
    525			val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
    526		else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
    527			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
    528		else
    529			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
    530		break;
    531
    532	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
    533		val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
    534
    535		/* When temperature is too low, charge current is decreased */
    536		if (bq->state.ntc_fault == NTC_FAULT_COOL) {
    537			ret = bq25890_field_read(bq, F_JEITA_ISET);
    538			if (ret < 0)
    539				return ret;
    540
    541			if (ret)
    542				val->intval /= 5;
    543			else
    544				val->intval /= 2;
    545		}
    546		break;
    547
    548	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
    549		if (!state.online) {
    550			val->intval = 0;
    551			break;
    552		}
    553
    554		ret = bq25890_field_read(bq, F_BATV); /* read measured value */
    555		if (ret < 0)
    556			return ret;
    557
    558		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
    559		val->intval = 2304000 + ret * 20000;
    560		break;
    561
    562	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
    563		val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
    564		break;
    565
    566	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
    567		val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
    568		break;
    569
    570	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
    571		val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
    572		break;
    573
    574	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
    575		ret = bq25890_field_read(bq, F_IINLIM);
    576		if (ret < 0)
    577			return ret;
    578
    579		val->intval = bq25890_find_val(ret, TBL_IINLIM);
    580		break;
    581
    582	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
    583		ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
    584		if (ret < 0)
    585			return ret;
    586
    587		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
    588		val->intval = 2304000 + ret * 20000;
    589		break;
    590
    591	case POWER_SUPPLY_PROP_CURRENT_NOW:
    592		ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
    593		if (ret < 0)
    594			return ret;
    595
    596		/* converted_val = ADC_val * 50mA (table 10.3.19) */
    597		val->intval = ret * -50000;
    598		break;
    599
    600	case POWER_SUPPLY_PROP_TEMP:
    601		ret = bq25890_field_read(bq, F_TSPCT);
    602		if (ret < 0)
    603			return ret;
    604
    605		/* convert TS percentage into rough temperature */
    606		val->intval = bq25890_find_val(ret, TBL_TSPCT);
    607		break;
    608
    609	default:
    610		return -EINVAL;
    611	}
    612
    613	return 0;
    614}
    615
    616/* On the BQ25892 try to get charger-type info from our supplier */
    617static void bq25890_charger_external_power_changed(struct power_supply *psy)
    618{
    619	struct bq25890_device *bq = power_supply_get_drvdata(psy);
    620	union power_supply_propval val;
    621	int input_current_limit, ret;
    622
    623	if (bq->chip_version != BQ25892)
    624		return;
    625
    626	ret = power_supply_get_property_from_supplier(bq->charger,
    627						      POWER_SUPPLY_PROP_USB_TYPE,
    628						      &val);
    629	if (ret)
    630		return;
    631
    632	switch (val.intval) {
    633	case POWER_SUPPLY_USB_TYPE_DCP:
    634		input_current_limit = bq25890_find_idx(2000000, TBL_IINLIM);
    635		if (bq->pump_express_vbus_max) {
    636			queue_delayed_work(system_power_efficient_wq,
    637					   &bq->pump_express_work,
    638					   PUMP_EXPRESS_START_DELAY);
    639		}
    640		break;
    641	case POWER_SUPPLY_USB_TYPE_CDP:
    642	case POWER_SUPPLY_USB_TYPE_ACA:
    643		input_current_limit = bq25890_find_idx(1500000, TBL_IINLIM);
    644		break;
    645	case POWER_SUPPLY_USB_TYPE_SDP:
    646	default:
    647		input_current_limit = bq25890_find_idx(500000, TBL_IINLIM);
    648	}
    649
    650	bq25890_field_write(bq, F_IINLIM, input_current_limit);
    651}
    652
    653static int bq25890_get_chip_state(struct bq25890_device *bq,
    654				  struct bq25890_state *state)
    655{
    656	int i, ret;
    657
    658	struct {
    659		enum bq25890_fields id;
    660		u8 *data;
    661	} state_fields[] = {
    662		{F_CHG_STAT,	&state->chrg_status},
    663		{F_PG_STAT,	&state->online},
    664		{F_VSYS_STAT,	&state->vsys_status},
    665		{F_BOOST_FAULT, &state->boost_fault},
    666		{F_BAT_FAULT,	&state->bat_fault},
    667		{F_CHG_FAULT,	&state->chrg_fault},
    668		{F_NTC_FAULT,	&state->ntc_fault}
    669	};
    670
    671	for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
    672		ret = bq25890_field_read(bq, state_fields[i].id);
    673		if (ret < 0)
    674			return ret;
    675
    676		*state_fields[i].data = ret;
    677	}
    678
    679	dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n",
    680		state->chrg_status, state->online, state->vsys_status,
    681		state->chrg_fault, state->boost_fault, state->bat_fault,
    682		state->ntc_fault);
    683
    684	return 0;
    685}
    686
    687static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
    688{
    689	struct bq25890_state new_state;
    690	int ret;
    691
    692	ret = bq25890_get_chip_state(bq, &new_state);
    693	if (ret < 0)
    694		return IRQ_NONE;
    695
    696	if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
    697		return IRQ_NONE;
    698
    699	if (!new_state.online && bq->state.online) {	    /* power removed */
    700		/* disable ADC */
    701		ret = bq25890_field_write(bq, F_CONV_RATE, 0);
    702		if (ret < 0)
    703			goto error;
    704	} else if (new_state.online && !bq->state.online) { /* power inserted */
    705		/* enable ADC, to have control of charge current/voltage */
    706		ret = bq25890_field_write(bq, F_CONV_RATE, 1);
    707		if (ret < 0)
    708			goto error;
    709	}
    710
    711	bq->state = new_state;
    712	power_supply_changed(bq->charger);
    713
    714	return IRQ_HANDLED;
    715error:
    716	dev_err(bq->dev, "Error communicating with the chip: %pe\n",
    717		ERR_PTR(ret));
    718	return IRQ_HANDLED;
    719}
    720
    721static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
    722{
    723	struct bq25890_device *bq = private;
    724	irqreturn_t ret;
    725
    726	mutex_lock(&bq->lock);
    727	ret = __bq25890_handle_irq(bq);
    728	mutex_unlock(&bq->lock);
    729
    730	return ret;
    731}
    732
    733static int bq25890_chip_reset(struct bq25890_device *bq)
    734{
    735	int ret;
    736	int rst_check_counter = 10;
    737
    738	ret = bq25890_field_write(bq, F_REG_RST, 1);
    739	if (ret < 0)
    740		return ret;
    741
    742	do {
    743		ret = bq25890_field_read(bq, F_REG_RST);
    744		if (ret < 0)
    745			return ret;
    746
    747		usleep_range(5, 10);
    748	} while (ret == 1 && --rst_check_counter);
    749
    750	if (!rst_check_counter)
    751		return -ETIMEDOUT;
    752
    753	return 0;
    754}
    755
    756static int bq25890_rw_init_data(struct bq25890_device *bq)
    757{
    758	bool write = !bq->read_back_init_data;
    759	int ret;
    760	int i;
    761
    762	const struct {
    763		enum bq25890_fields id;
    764		u8 *value;
    765	} init_data[] = {
    766		{F_ICHG,	 &bq->init_data.ichg},
    767		{F_VREG,	 &bq->init_data.vreg},
    768		{F_ITERM,	 &bq->init_data.iterm},
    769		{F_IPRECHG,	 &bq->init_data.iprechg},
    770		{F_SYSVMIN,	 &bq->init_data.sysvmin},
    771		{F_BOOSTV,	 &bq->init_data.boostv},
    772		{F_BOOSTI,	 &bq->init_data.boosti},
    773		{F_BOOSTF,	 &bq->init_data.boostf},
    774		{F_EN_ILIM,	 &bq->init_data.ilim_en},
    775		{F_TREG,	 &bq->init_data.treg},
    776		{F_BATCMP,	 &bq->init_data.rbatcomp},
    777		{F_VCLAMP,	 &bq->init_data.vclamp},
    778	};
    779
    780	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
    781		if (write) {
    782			ret = bq25890_field_write(bq, init_data[i].id,
    783						  *init_data[i].value);
    784		} else {
    785			ret = bq25890_field_read(bq, init_data[i].id);
    786			if (ret >= 0)
    787				*init_data[i].value = ret;
    788		}
    789		if (ret < 0) {
    790			dev_dbg(bq->dev, "Accessing init data failed %d\n", ret);
    791			return ret;
    792		}
    793	}
    794
    795	return 0;
    796}
    797
    798static int bq25890_hw_init(struct bq25890_device *bq)
    799{
    800	int ret;
    801
    802	if (!bq->skip_reset) {
    803		ret = bq25890_chip_reset(bq);
    804		if (ret < 0) {
    805			dev_dbg(bq->dev, "Reset failed %d\n", ret);
    806			return ret;
    807		}
    808	} else {
    809		/*
    810		 * Ensure charging is enabled, on some boards where the fw
    811		 * takes care of initalizition F_CHG_CFG is set to 0 before
    812		 * handing control over to the OS.
    813		 */
    814		ret = bq25890_field_write(bq, F_CHG_CFG, 1);
    815		if (ret < 0) {
    816			dev_dbg(bq->dev, "Enabling charging failed %d\n", ret);
    817			return ret;
    818		}
    819	}
    820
    821	/* disable watchdog */
    822	ret = bq25890_field_write(bq, F_WD, 0);
    823	if (ret < 0) {
    824		dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
    825		return ret;
    826	}
    827
    828	/* initialize currents/voltages and other parameters */
    829	ret = bq25890_rw_init_data(bq);
    830	if (ret)
    831		return ret;
    832
    833	ret = bq25890_get_chip_state(bq, &bq->state);
    834	if (ret < 0) {
    835		dev_dbg(bq->dev, "Get state failed %d\n", ret);
    836		return ret;
    837	}
    838
    839	/* Configure ADC for continuous conversions when charging */
    840	ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
    841	if (ret < 0) {
    842		dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
    843		return ret;
    844	}
    845
    846	return 0;
    847}
    848
    849static const enum power_supply_property bq25890_power_supply_props[] = {
    850	POWER_SUPPLY_PROP_MANUFACTURER,
    851	POWER_SUPPLY_PROP_MODEL_NAME,
    852	POWER_SUPPLY_PROP_STATUS,
    853	POWER_SUPPLY_PROP_CHARGE_TYPE,
    854	POWER_SUPPLY_PROP_ONLINE,
    855	POWER_SUPPLY_PROP_HEALTH,
    856	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
    857	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
    858	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
    859	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
    860	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
    861	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
    862	POWER_SUPPLY_PROP_VOLTAGE_NOW,
    863	POWER_SUPPLY_PROP_CURRENT_NOW,
    864	POWER_SUPPLY_PROP_TEMP,
    865};
    866
    867static char *bq25890_charger_supplied_to[] = {
    868	"main-battery",
    869};
    870
    871static const struct power_supply_desc bq25890_power_supply_desc = {
    872	.name = "bq25890-charger",
    873	.type = POWER_SUPPLY_TYPE_USB,
    874	.properties = bq25890_power_supply_props,
    875	.num_properties = ARRAY_SIZE(bq25890_power_supply_props),
    876	.get_property = bq25890_power_supply_get_property,
    877	.external_power_changed	= bq25890_charger_external_power_changed,
    878};
    879
    880static int bq25890_power_supply_init(struct bq25890_device *bq)
    881{
    882	struct power_supply_config psy_cfg = { .drv_data = bq, };
    883
    884	psy_cfg.supplied_to = bq25890_charger_supplied_to;
    885	psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
    886
    887	bq->charger = devm_power_supply_register(bq->dev,
    888						 &bq25890_power_supply_desc,
    889						 &psy_cfg);
    890
    891	return PTR_ERR_OR_ZERO(bq->charger);
    892}
    893
    894static int bq25890_set_otg_cfg(struct bq25890_device *bq, u8 val)
    895{
    896	int ret;
    897
    898	ret = bq25890_field_write(bq, F_OTG_CFG, val);
    899	if (ret < 0)
    900		dev_err(bq->dev, "Error switching to boost/charger mode: %d\n", ret);
    901
    902	return ret;
    903}
    904
    905static void bq25890_pump_express_work(struct work_struct *data)
    906{
    907	struct bq25890_device *bq =
    908		container_of(data, struct bq25890_device, pump_express_work.work);
    909	int voltage, i, ret;
    910
    911	dev_dbg(bq->dev, "Start to request input voltage increasing\n");
    912
    913	/* Enable current pulse voltage control protocol */
    914	ret = bq25890_field_write(bq, F_PUMPX_EN, 1);
    915	if (ret < 0)
    916		goto error_print;
    917
    918	for (i = 0; i < PUMP_EXPRESS_MAX_TRIES; i++) {
    919		voltage = bq25890_get_vbus_voltage(bq);
    920		if (voltage < 0)
    921			goto error_print;
    922		dev_dbg(bq->dev, "input voltage = %d uV\n", voltage);
    923
    924		if ((voltage + PUMP_EXPRESS_VBUS_MARGIN_uV) >
    925					bq->pump_express_vbus_max)
    926			break;
    927
    928		ret = bq25890_field_write(bq, F_PUMPX_UP, 1);
    929		if (ret < 0)
    930			goto error_print;
    931
    932		/* Note a single PUMPX up pulse-sequence takes 2.1s */
    933		ret = regmap_field_read_poll_timeout(bq->rmap_fields[F_PUMPX_UP],
    934						     ret, !ret, 100000, 3000000);
    935		if (ret < 0)
    936			goto error_print;
    937
    938		/* Make sure ADC has sampled Vbus before checking again */
    939		msleep(1000);
    940	}
    941
    942	bq25890_field_write(bq, F_PUMPX_EN, 0);
    943
    944	dev_info(bq->dev, "Hi-voltage charging requested, input voltage is %d mV\n",
    945		 voltage);
    946
    947	return;
    948error_print:
    949	dev_err(bq->dev, "Failed to request hi-voltage charging\n");
    950}
    951
    952static void bq25890_usb_work(struct work_struct *data)
    953{
    954	int ret;
    955	struct bq25890_device *bq =
    956			container_of(data, struct bq25890_device, usb_work);
    957
    958	switch (bq->usb_event) {
    959	case USB_EVENT_ID:
    960		/* Enable boost mode */
    961		bq25890_set_otg_cfg(bq, 1);
    962		break;
    963
    964	case USB_EVENT_NONE:
    965		/* Disable boost mode */
    966		ret = bq25890_set_otg_cfg(bq, 0);
    967		if (ret == 0)
    968			power_supply_changed(bq->charger);
    969		break;
    970	}
    971}
    972
    973static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
    974				void *priv)
    975{
    976	struct bq25890_device *bq =
    977			container_of(nb, struct bq25890_device, usb_nb);
    978
    979	bq->usb_event = val;
    980	queue_work(system_power_efficient_wq, &bq->usb_work);
    981
    982	return NOTIFY_OK;
    983}
    984
    985#ifdef CONFIG_REGULATOR
    986static int bq25890_vbus_enable(struct regulator_dev *rdev)
    987{
    988	struct bq25890_device *bq = rdev_get_drvdata(rdev);
    989
    990	return bq25890_set_otg_cfg(bq, 1);
    991}
    992
    993static int bq25890_vbus_disable(struct regulator_dev *rdev)
    994{
    995	struct bq25890_device *bq = rdev_get_drvdata(rdev);
    996
    997	return bq25890_set_otg_cfg(bq, 0);
    998}
    999
   1000static int bq25890_vbus_is_enabled(struct regulator_dev *rdev)
   1001{
   1002	struct bq25890_device *bq = rdev_get_drvdata(rdev);
   1003
   1004	return bq25890_field_read(bq, F_OTG_CFG);
   1005}
   1006
   1007static const struct regulator_ops bq25890_vbus_ops = {
   1008	.enable = bq25890_vbus_enable,
   1009	.disable = bq25890_vbus_disable,
   1010	.is_enabled = bq25890_vbus_is_enabled,
   1011};
   1012
   1013static const struct regulator_desc bq25890_vbus_desc = {
   1014	.name = "usb_otg_vbus",
   1015	.of_match = "usb-otg-vbus",
   1016	.type = REGULATOR_VOLTAGE,
   1017	.owner = THIS_MODULE,
   1018	.ops = &bq25890_vbus_ops,
   1019	.fixed_uV = 5000000,
   1020	.n_voltages = 1,
   1021};
   1022#endif
   1023
   1024static int bq25890_get_chip_version(struct bq25890_device *bq)
   1025{
   1026	int id, rev;
   1027
   1028	id = bq25890_field_read(bq, F_PN);
   1029	if (id < 0) {
   1030		dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
   1031		return id;
   1032	}
   1033
   1034	rev = bq25890_field_read(bq, F_DEV_REV);
   1035	if (rev < 0) {
   1036		dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
   1037		return rev;
   1038	}
   1039
   1040	switch (id) {
   1041	case BQ25890_ID:
   1042		bq->chip_version = BQ25890;
   1043		break;
   1044
   1045	/* BQ25892 and BQ25896 share same ID 0 */
   1046	case BQ25896_ID:
   1047		switch (rev) {
   1048		case 2:
   1049			bq->chip_version = BQ25896;
   1050			break;
   1051		case 1:
   1052			bq->chip_version = BQ25892;
   1053			break;
   1054		default:
   1055			dev_err(bq->dev,
   1056				"Unknown device revision %d, assume BQ25892\n",
   1057				rev);
   1058			bq->chip_version = BQ25892;
   1059		}
   1060		break;
   1061
   1062	case BQ25895_ID:
   1063		bq->chip_version = BQ25895;
   1064		break;
   1065
   1066	default:
   1067		dev_err(bq->dev, "Unknown chip ID %d\n", id);
   1068		return -ENODEV;
   1069	}
   1070
   1071	return 0;
   1072}
   1073
   1074static int bq25890_irq_probe(struct bq25890_device *bq)
   1075{
   1076	struct gpio_desc *irq;
   1077
   1078	irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
   1079	if (IS_ERR(irq))
   1080		return dev_err_probe(bq->dev, PTR_ERR(irq),
   1081				     "Could not probe irq pin.\n");
   1082
   1083	return gpiod_to_irq(irq);
   1084}
   1085
   1086static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
   1087{
   1088	int ret;
   1089	u32 property;
   1090	int i;
   1091	struct bq25890_init_data *init = &bq->init_data;
   1092	struct {
   1093		char *name;
   1094		bool optional;
   1095		enum bq25890_table_ids tbl_id;
   1096		u8 *conv_data; /* holds converted value from given property */
   1097	} props[] = {
   1098		/* required properties */
   1099		{"ti,charge-current", false, TBL_ICHG, &init->ichg},
   1100		{"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
   1101		{"ti,termination-current", false, TBL_ITERM, &init->iterm},
   1102		{"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
   1103		{"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
   1104		{"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
   1105		{"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
   1106
   1107		/* optional properties */
   1108		{"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
   1109		{"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
   1110		{"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
   1111	};
   1112
   1113	/* initialize data for optional properties */
   1114	init->treg = 3; /* 120 degrees Celsius */
   1115	init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
   1116
   1117	for (i = 0; i < ARRAY_SIZE(props); i++) {
   1118		ret = device_property_read_u32(bq->dev, props[i].name,
   1119					       &property);
   1120		if (ret < 0) {
   1121			if (props[i].optional)
   1122				continue;
   1123
   1124			dev_err(bq->dev, "Unable to read property %d %s\n", ret,
   1125				props[i].name);
   1126
   1127			return ret;
   1128		}
   1129
   1130		*props[i].conv_data = bq25890_find_idx(property,
   1131						       props[i].tbl_id);
   1132	}
   1133
   1134	return 0;
   1135}
   1136
   1137static int bq25890_fw_probe(struct bq25890_device *bq)
   1138{
   1139	int ret;
   1140	struct bq25890_init_data *init = &bq->init_data;
   1141
   1142	/* Optional, left at 0 if property is not present */
   1143	device_property_read_u32(bq->dev, "linux,pump-express-vbus-max",
   1144				 &bq->pump_express_vbus_max);
   1145
   1146	bq->skip_reset = device_property_read_bool(bq->dev, "linux,skip-reset");
   1147	bq->read_back_init_data = device_property_read_bool(bq->dev,
   1148						"linux,read-back-settings");
   1149	if (bq->read_back_init_data)
   1150		return 0;
   1151
   1152	ret = bq25890_fw_read_u32_props(bq);
   1153	if (ret < 0)
   1154		return ret;
   1155
   1156	init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
   1157	init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
   1158
   1159	return 0;
   1160}
   1161
   1162static int bq25890_probe(struct i2c_client *client,
   1163			 const struct i2c_device_id *id)
   1164{
   1165	struct device *dev = &client->dev;
   1166	struct bq25890_device *bq;
   1167	int ret;
   1168
   1169	bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
   1170	if (!bq)
   1171		return -ENOMEM;
   1172
   1173	bq->client = client;
   1174	bq->dev = dev;
   1175
   1176	mutex_init(&bq->lock);
   1177	INIT_DELAYED_WORK(&bq->pump_express_work, bq25890_pump_express_work);
   1178
   1179	bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
   1180	if (IS_ERR(bq->rmap))
   1181		return dev_err_probe(dev, PTR_ERR(bq->rmap),
   1182				     "failed to allocate register map\n");
   1183
   1184	ret = devm_regmap_field_bulk_alloc(dev, bq->rmap, bq->rmap_fields,
   1185					   bq25890_reg_fields, F_MAX_FIELDS);
   1186	if (ret)
   1187		return ret;
   1188
   1189	i2c_set_clientdata(client, bq);
   1190
   1191	ret = bq25890_get_chip_version(bq);
   1192	if (ret) {
   1193		dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
   1194		return ret;
   1195	}
   1196
   1197	ret = bq25890_fw_probe(bq);
   1198	if (ret < 0)
   1199		return dev_err_probe(dev, ret, "reading device properties\n");
   1200
   1201	ret = bq25890_hw_init(bq);
   1202	if (ret < 0) {
   1203		dev_err(dev, "Cannot initialize the chip: %d\n", ret);
   1204		return ret;
   1205	}
   1206
   1207	if (client->irq <= 0)
   1208		client->irq = bq25890_irq_probe(bq);
   1209
   1210	if (client->irq < 0) {
   1211		dev_err(dev, "No irq resource found.\n");
   1212		return client->irq;
   1213	}
   1214
   1215	/* OTG reporting */
   1216	bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
   1217	if (!IS_ERR_OR_NULL(bq->usb_phy)) {
   1218		INIT_WORK(&bq->usb_work, bq25890_usb_work);
   1219		bq->usb_nb.notifier_call = bq25890_usb_notifier;
   1220		usb_register_notifier(bq->usb_phy, &bq->usb_nb);
   1221	}
   1222#ifdef CONFIG_REGULATOR
   1223	else {
   1224		struct bq25890_platform_data *pdata = dev_get_platdata(dev);
   1225		struct regulator_config cfg = { };
   1226		struct regulator_dev *reg;
   1227
   1228		cfg.dev = dev;
   1229		cfg.driver_data = bq;
   1230		if (pdata)
   1231			cfg.init_data = pdata->regulator_init_data;
   1232
   1233		reg = devm_regulator_register(dev, &bq25890_vbus_desc, &cfg);
   1234		if (IS_ERR(reg))
   1235			return dev_err_probe(dev, PTR_ERR(reg), "registering regulator");
   1236	}
   1237#endif
   1238
   1239	ret = bq25890_power_supply_init(bq);
   1240	if (ret < 0) {
   1241		dev_err(dev, "Failed to register power supply\n");
   1242		goto err_unregister_usb_notifier;
   1243	}
   1244
   1245	ret = devm_request_threaded_irq(dev, client->irq, NULL,
   1246					bq25890_irq_handler_thread,
   1247					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
   1248					BQ25890_IRQ_PIN, bq);
   1249	if (ret)
   1250		goto err_unregister_usb_notifier;
   1251
   1252	return 0;
   1253
   1254err_unregister_usb_notifier:
   1255	if (!IS_ERR_OR_NULL(bq->usb_phy))
   1256		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
   1257
   1258	return ret;
   1259}
   1260
   1261static int bq25890_remove(struct i2c_client *client)
   1262{
   1263	struct bq25890_device *bq = i2c_get_clientdata(client);
   1264
   1265	if (!IS_ERR_OR_NULL(bq->usb_phy))
   1266		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
   1267
   1268	if (!bq->skip_reset) {
   1269		/* reset all registers to default values */
   1270		bq25890_chip_reset(bq);
   1271	}
   1272
   1273	return 0;
   1274}
   1275
   1276static void bq25890_shutdown(struct i2c_client *client)
   1277{
   1278	struct bq25890_device *bq = i2c_get_clientdata(client);
   1279
   1280	/*
   1281	 * TODO this if + return should probably be removed, but that would
   1282	 * introduce a function change for boards using the usb-phy framework.
   1283	 * This needs to be tested on such a board before making this change.
   1284	 */
   1285	if (!IS_ERR_OR_NULL(bq->usb_phy))
   1286		return;
   1287
   1288	/*
   1289	 * Turn off the 5v Boost regulator which outputs Vbus to the device's
   1290	 * Micro-USB or Type-C USB port. Leaving this on drains power and
   1291	 * this avoids the PMIC on some device-models seeing this as Vbus
   1292	 * getting inserted after shutdown, causing the device to immediately
   1293	 * power-up again.
   1294	 */
   1295	bq25890_set_otg_cfg(bq, 0);
   1296}
   1297
   1298#ifdef CONFIG_PM_SLEEP
   1299static int bq25890_suspend(struct device *dev)
   1300{
   1301	struct bq25890_device *bq = dev_get_drvdata(dev);
   1302
   1303	/*
   1304	 * If charger is removed, while in suspend, make sure ADC is diabled
   1305	 * since it consumes slightly more power.
   1306	 */
   1307	return bq25890_field_write(bq, F_CONV_RATE, 0);
   1308}
   1309
   1310static int bq25890_resume(struct device *dev)
   1311{
   1312	int ret;
   1313	struct bq25890_device *bq = dev_get_drvdata(dev);
   1314
   1315	mutex_lock(&bq->lock);
   1316
   1317	ret = bq25890_get_chip_state(bq, &bq->state);
   1318	if (ret < 0)
   1319		goto unlock;
   1320
   1321	/* Re-enable ADC only if charger is plugged in. */
   1322	if (bq->state.online) {
   1323		ret = bq25890_field_write(bq, F_CONV_RATE, 1);
   1324		if (ret < 0)
   1325			goto unlock;
   1326	}
   1327
   1328	/* signal userspace, maybe state changed while suspended */
   1329	power_supply_changed(bq->charger);
   1330
   1331unlock:
   1332	mutex_unlock(&bq->lock);
   1333
   1334	return ret;
   1335}
   1336#endif
   1337
   1338static const struct dev_pm_ops bq25890_pm = {
   1339	SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
   1340};
   1341
   1342static const struct i2c_device_id bq25890_i2c_ids[] = {
   1343	{ "bq25890", 0 },
   1344	{ "bq25892", 0 },
   1345	{ "bq25895", 0 },
   1346	{ "bq25896", 0 },
   1347	{},
   1348};
   1349MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
   1350
   1351static const struct of_device_id bq25890_of_match[] = {
   1352	{ .compatible = "ti,bq25890", },
   1353	{ .compatible = "ti,bq25892", },
   1354	{ .compatible = "ti,bq25895", },
   1355	{ .compatible = "ti,bq25896", },
   1356	{ },
   1357};
   1358MODULE_DEVICE_TABLE(of, bq25890_of_match);
   1359
   1360#ifdef CONFIG_ACPI
   1361static const struct acpi_device_id bq25890_acpi_match[] = {
   1362	{"BQ258900", 0},
   1363	{},
   1364};
   1365MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
   1366#endif
   1367
   1368static struct i2c_driver bq25890_driver = {
   1369	.driver = {
   1370		.name = "bq25890-charger",
   1371		.of_match_table = of_match_ptr(bq25890_of_match),
   1372		.acpi_match_table = ACPI_PTR(bq25890_acpi_match),
   1373		.pm = &bq25890_pm,
   1374	},
   1375	.probe = bq25890_probe,
   1376	.remove = bq25890_remove,
   1377	.shutdown = bq25890_shutdown,
   1378	.id_table = bq25890_i2c_ids,
   1379};
   1380module_i2c_driver(bq25890_driver);
   1381
   1382MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
   1383MODULE_DESCRIPTION("bq25890 charger driver");
   1384MODULE_LICENSE("GPL");