xref: /openbmc/linux/drivers/power/supply/bq25890_charger.c (revision c942fddf8793b2013be8c901b47d0a8dc02bf99f)
1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * TI BQ25890 charger driver
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  * Copyright (C) 2015 Intel Corporation
68c0984e5SSebastian Reichel  */
78c0984e5SSebastian Reichel 
88c0984e5SSebastian Reichel #include <linux/module.h>
98c0984e5SSebastian Reichel #include <linux/i2c.h>
108c0984e5SSebastian Reichel #include <linux/power_supply.h>
118c0984e5SSebastian Reichel #include <linux/regmap.h>
128c0984e5SSebastian Reichel #include <linux/types.h>
138c0984e5SSebastian Reichel #include <linux/gpio/consumer.h>
148c0984e5SSebastian Reichel #include <linux/interrupt.h>
158c0984e5SSebastian Reichel #include <linux/delay.h>
168c0984e5SSebastian Reichel #include <linux/usb/phy.h>
178c0984e5SSebastian Reichel 
188c0984e5SSebastian Reichel #include <linux/acpi.h>
198c0984e5SSebastian Reichel #include <linux/of.h>
208c0984e5SSebastian Reichel 
218c0984e5SSebastian Reichel #define BQ25890_MANUFACTURER		"Texas Instruments"
228c0984e5SSebastian Reichel #define BQ25890_IRQ_PIN			"bq25890_irq"
238c0984e5SSebastian Reichel 
248c0984e5SSebastian Reichel #define BQ25890_ID			3
252e1a2ddeSAngus Ainslie (Purism) #define BQ25896_ID			0
268c0984e5SSebastian Reichel 
278c0984e5SSebastian Reichel enum bq25890_fields {
288c0984e5SSebastian Reichel 	F_EN_HIZ, F_EN_ILIM, F_IILIM,				     /* Reg00 */
298c0984e5SSebastian Reichel 	F_BHOT, F_BCOLD, F_VINDPM_OFS,				     /* Reg01 */
308c0984e5SSebastian Reichel 	F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
318c0984e5SSebastian Reichel 	F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN,	     /* Reg02 */
328c0984e5SSebastian Reichel 	F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,    /* Reg03 */
338c0984e5SSebastian Reichel 	F_PUMPX_EN, F_ICHG,					     /* Reg04 */
348c0984e5SSebastian Reichel 	F_IPRECHG, F_ITERM,					     /* Reg05 */
358c0984e5SSebastian Reichel 	F_VREG, F_BATLOWV, F_VRECHG,				     /* Reg06 */
368c0984e5SSebastian Reichel 	F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
378c0984e5SSebastian Reichel 	F_JEITA_ISET,						     /* Reg07 */
388c0984e5SSebastian Reichel 	F_BATCMP, F_VCLAMP, F_TREG,				     /* Reg08 */
398c0984e5SSebastian Reichel 	F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
408c0984e5SSebastian Reichel 	F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN,	     /* Reg09 */
418c0984e5SSebastian Reichel 	F_BOOSTV, F_BOOSTI,					     /* Reg0A */
428c0984e5SSebastian Reichel 	F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_VSYS_STAT, /* Reg0B */
438c0984e5SSebastian Reichel 	F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
448c0984e5SSebastian Reichel 	F_NTC_FAULT,						     /* Reg0C */
458c0984e5SSebastian Reichel 	F_FORCE_VINDPM, F_VINDPM,				     /* Reg0D */
468c0984e5SSebastian Reichel 	F_THERM_STAT, F_BATV,					     /* Reg0E */
478c0984e5SSebastian Reichel 	F_SYSV,							     /* Reg0F */
488c0984e5SSebastian Reichel 	F_TSPCT,						     /* Reg10 */
498c0984e5SSebastian Reichel 	F_VBUS_GD, F_VBUSV,					     /* Reg11 */
508c0984e5SSebastian Reichel 	F_ICHGR,						     /* Reg12 */
518c0984e5SSebastian Reichel 	F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM,			     /* Reg13 */
528c0984e5SSebastian Reichel 	F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV,   /* Reg14 */
538c0984e5SSebastian Reichel 
548c0984e5SSebastian Reichel 	F_MAX_FIELDS
558c0984e5SSebastian Reichel };
568c0984e5SSebastian Reichel 
578c0984e5SSebastian Reichel /* initial field values, converted to register values */
588c0984e5SSebastian Reichel struct bq25890_init_data {
598c0984e5SSebastian Reichel 	u8 ichg;	/* charge current		*/
608c0984e5SSebastian Reichel 	u8 vreg;	/* regulation voltage		*/
618c0984e5SSebastian Reichel 	u8 iterm;	/* termination current		*/
628c0984e5SSebastian Reichel 	u8 iprechg;	/* precharge current		*/
638c0984e5SSebastian Reichel 	u8 sysvmin;	/* minimum system voltage limit */
648c0984e5SSebastian Reichel 	u8 boostv;	/* boost regulation voltage	*/
658c0984e5SSebastian Reichel 	u8 boosti;	/* boost current limit		*/
668c0984e5SSebastian Reichel 	u8 boostf;	/* boost frequency		*/
678c0984e5SSebastian Reichel 	u8 ilim_en;	/* enable ILIM pin		*/
688c0984e5SSebastian Reichel 	u8 treg;	/* thermal regulation threshold */
698c0984e5SSebastian Reichel };
708c0984e5SSebastian Reichel 
718c0984e5SSebastian Reichel struct bq25890_state {
728c0984e5SSebastian Reichel 	u8 online;
738c0984e5SSebastian Reichel 	u8 chrg_status;
748c0984e5SSebastian Reichel 	u8 chrg_fault;
758c0984e5SSebastian Reichel 	u8 vsys_status;
768c0984e5SSebastian Reichel 	u8 boost_fault;
778c0984e5SSebastian Reichel 	u8 bat_fault;
788c0984e5SSebastian Reichel };
798c0984e5SSebastian Reichel 
808c0984e5SSebastian Reichel struct bq25890_device {
818c0984e5SSebastian Reichel 	struct i2c_client *client;
828c0984e5SSebastian Reichel 	struct device *dev;
838c0984e5SSebastian Reichel 	struct power_supply *charger;
848c0984e5SSebastian Reichel 
858c0984e5SSebastian Reichel 	struct usb_phy *usb_phy;
868c0984e5SSebastian Reichel 	struct notifier_block usb_nb;
878c0984e5SSebastian Reichel 	struct work_struct usb_work;
888c0984e5SSebastian Reichel 	unsigned long usb_event;
898c0984e5SSebastian Reichel 
908c0984e5SSebastian Reichel 	struct regmap *rmap;
918c0984e5SSebastian Reichel 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
928c0984e5SSebastian Reichel 
938c0984e5SSebastian Reichel 	int chip_id;
948c0984e5SSebastian Reichel 	struct bq25890_init_data init_data;
958c0984e5SSebastian Reichel 	struct bq25890_state state;
968c0984e5SSebastian Reichel 
978c0984e5SSebastian Reichel 	struct mutex lock; /* protect state data */
988c0984e5SSebastian Reichel };
998c0984e5SSebastian Reichel 
1008c0984e5SSebastian Reichel static const struct regmap_range bq25890_readonly_reg_ranges[] = {
1018c0984e5SSebastian Reichel 	regmap_reg_range(0x0b, 0x0c),
1028c0984e5SSebastian Reichel 	regmap_reg_range(0x0e, 0x13),
1038c0984e5SSebastian Reichel };
1048c0984e5SSebastian Reichel 
1058c0984e5SSebastian Reichel static const struct regmap_access_table bq25890_writeable_regs = {
1068c0984e5SSebastian Reichel 	.no_ranges = bq25890_readonly_reg_ranges,
1078c0984e5SSebastian Reichel 	.n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
1088c0984e5SSebastian Reichel };
1098c0984e5SSebastian Reichel 
1108c0984e5SSebastian Reichel static const struct regmap_range bq25890_volatile_reg_ranges[] = {
1118c0984e5SSebastian Reichel 	regmap_reg_range(0x00, 0x00),
1128c0984e5SSebastian Reichel 	regmap_reg_range(0x09, 0x09),
1138c0984e5SSebastian Reichel 	regmap_reg_range(0x0b, 0x0c),
1148c0984e5SSebastian Reichel 	regmap_reg_range(0x0e, 0x14),
1158c0984e5SSebastian Reichel };
1168c0984e5SSebastian Reichel 
1178c0984e5SSebastian Reichel static const struct regmap_access_table bq25890_volatile_regs = {
1188c0984e5SSebastian Reichel 	.yes_ranges = bq25890_volatile_reg_ranges,
1198c0984e5SSebastian Reichel 	.n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
1208c0984e5SSebastian Reichel };
1218c0984e5SSebastian Reichel 
1228c0984e5SSebastian Reichel static const struct regmap_config bq25890_regmap_config = {
1238c0984e5SSebastian Reichel 	.reg_bits = 8,
1248c0984e5SSebastian Reichel 	.val_bits = 8,
1258c0984e5SSebastian Reichel 
1268c0984e5SSebastian Reichel 	.max_register = 0x14,
1278c0984e5SSebastian Reichel 	.cache_type = REGCACHE_RBTREE,
1288c0984e5SSebastian Reichel 
1298c0984e5SSebastian Reichel 	.wr_table = &bq25890_writeable_regs,
1308c0984e5SSebastian Reichel 	.volatile_table = &bq25890_volatile_regs,
1318c0984e5SSebastian Reichel };
1328c0984e5SSebastian Reichel 
1338c0984e5SSebastian Reichel static const struct reg_field bq25890_reg_fields[] = {
1348c0984e5SSebastian Reichel 	/* REG00 */
1358c0984e5SSebastian Reichel 	[F_EN_HIZ]		= REG_FIELD(0x00, 7, 7),
1368c0984e5SSebastian Reichel 	[F_EN_ILIM]		= REG_FIELD(0x00, 6, 6),
1378c0984e5SSebastian Reichel 	[F_IILIM]		= REG_FIELD(0x00, 0, 5),
1388c0984e5SSebastian Reichel 	/* REG01 */
1398c0984e5SSebastian Reichel 	[F_BHOT]		= REG_FIELD(0x01, 6, 7),
1408c0984e5SSebastian Reichel 	[F_BCOLD]		= REG_FIELD(0x01, 5, 5),
1418c0984e5SSebastian Reichel 	[F_VINDPM_OFS]		= REG_FIELD(0x01, 0, 4),
1428c0984e5SSebastian Reichel 	/* REG02 */
1438c0984e5SSebastian Reichel 	[F_CONV_START]		= REG_FIELD(0x02, 7, 7),
1448c0984e5SSebastian Reichel 	[F_CONV_RATE]		= REG_FIELD(0x02, 6, 6),
1458c0984e5SSebastian Reichel 	[F_BOOSTF]		= REG_FIELD(0x02, 5, 5),
1468c0984e5SSebastian Reichel 	[F_ICO_EN]		= REG_FIELD(0x02, 4, 4),
1472e1a2ddeSAngus Ainslie (Purism) 	[F_HVDCP_EN]		= REG_FIELD(0x02, 3, 3),  // reserved on BQ25896
1482e1a2ddeSAngus Ainslie (Purism) 	[F_MAXC_EN]		= REG_FIELD(0x02, 2, 2),  // reserved on BQ25896
1498c0984e5SSebastian Reichel 	[F_FORCE_DPM]		= REG_FIELD(0x02, 1, 1),
1508c0984e5SSebastian Reichel 	[F_AUTO_DPDM_EN]	= REG_FIELD(0x02, 0, 0),
1518c0984e5SSebastian Reichel 	/* REG03 */
1528c0984e5SSebastian Reichel 	[F_BAT_LOAD_EN]		= REG_FIELD(0x03, 7, 7),
1538c0984e5SSebastian Reichel 	[F_WD_RST]		= REG_FIELD(0x03, 6, 6),
1548c0984e5SSebastian Reichel 	[F_OTG_CFG]		= REG_FIELD(0x03, 5, 5),
1558c0984e5SSebastian Reichel 	[F_CHG_CFG]		= REG_FIELD(0x03, 4, 4),
1568c0984e5SSebastian Reichel 	[F_SYSVMIN]		= REG_FIELD(0x03, 1, 3),
1572e1a2ddeSAngus Ainslie (Purism) 	/* MIN_VBAT_SEL on BQ25896 */
1588c0984e5SSebastian Reichel 	/* REG04 */
1598c0984e5SSebastian Reichel 	[F_PUMPX_EN]		= REG_FIELD(0x04, 7, 7),
1608c0984e5SSebastian Reichel 	[F_ICHG]		= REG_FIELD(0x04, 0, 6),
1618c0984e5SSebastian Reichel 	/* REG05 */
1628c0984e5SSebastian Reichel 	[F_IPRECHG]		= REG_FIELD(0x05, 4, 7),
1638c0984e5SSebastian Reichel 	[F_ITERM]		= REG_FIELD(0x05, 0, 3),
1648c0984e5SSebastian Reichel 	/* REG06 */
1658c0984e5SSebastian Reichel 	[F_VREG]		= REG_FIELD(0x06, 2, 7),
1668c0984e5SSebastian Reichel 	[F_BATLOWV]		= REG_FIELD(0x06, 1, 1),
1678c0984e5SSebastian Reichel 	[F_VRECHG]		= REG_FIELD(0x06, 0, 0),
1688c0984e5SSebastian Reichel 	/* REG07 */
1698c0984e5SSebastian Reichel 	[F_TERM_EN]		= REG_FIELD(0x07, 7, 7),
1708c0984e5SSebastian Reichel 	[F_STAT_DIS]		= REG_FIELD(0x07, 6, 6),
1718c0984e5SSebastian Reichel 	[F_WD]			= REG_FIELD(0x07, 4, 5),
1728c0984e5SSebastian Reichel 	[F_TMR_EN]		= REG_FIELD(0x07, 3, 3),
1738c0984e5SSebastian Reichel 	[F_CHG_TMR]		= REG_FIELD(0x07, 1, 2),
1748c0984e5SSebastian Reichel 	[F_JEITA_ISET]		= REG_FIELD(0x07, 0, 0),
1758c0984e5SSebastian Reichel 	/* REG08 */
17695809139SMichał Mirosław 	[F_BATCMP]		= REG_FIELD(0x08, 5, 7),
1778c0984e5SSebastian Reichel 	[F_VCLAMP]		= REG_FIELD(0x08, 2, 4),
1788c0984e5SSebastian Reichel 	[F_TREG]		= REG_FIELD(0x08, 0, 1),
1798c0984e5SSebastian Reichel 	/* REG09 */
1808c0984e5SSebastian Reichel 	[F_FORCE_ICO]		= REG_FIELD(0x09, 7, 7),
1818c0984e5SSebastian Reichel 	[F_TMR2X_EN]		= REG_FIELD(0x09, 6, 6),
1828c0984e5SSebastian Reichel 	[F_BATFET_DIS]		= REG_FIELD(0x09, 5, 5),
1838c0984e5SSebastian Reichel 	[F_JEITA_VSET]		= REG_FIELD(0x09, 4, 4),
1848c0984e5SSebastian Reichel 	[F_BATFET_DLY]		= REG_FIELD(0x09, 3, 3),
1858c0984e5SSebastian Reichel 	[F_BATFET_RST_EN]	= REG_FIELD(0x09, 2, 2),
1868c0984e5SSebastian Reichel 	[F_PUMPX_UP]		= REG_FIELD(0x09, 1, 1),
1878c0984e5SSebastian Reichel 	[F_PUMPX_DN]		= REG_FIELD(0x09, 0, 0),
1888c0984e5SSebastian Reichel 	/* REG0A */
1898c0984e5SSebastian Reichel 	[F_BOOSTV]		= REG_FIELD(0x0A, 4, 7),
1902e1a2ddeSAngus Ainslie (Purism) 	/* PFM_OTG_DIS 3 on BQ25896 */
1918c0984e5SSebastian Reichel 	[F_BOOSTI]		= REG_FIELD(0x0A, 0, 2),
1928c0984e5SSebastian Reichel 	/* REG0B */
1938c0984e5SSebastian Reichel 	[F_VBUS_STAT]		= REG_FIELD(0x0B, 5, 7),
1948c0984e5SSebastian Reichel 	[F_CHG_STAT]		= REG_FIELD(0x0B, 3, 4),
1958c0984e5SSebastian Reichel 	[F_PG_STAT]		= REG_FIELD(0x0B, 2, 2),
1962e1a2ddeSAngus Ainslie (Purism) 	[F_SDP_STAT]		= REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
1978c0984e5SSebastian Reichel 	[F_VSYS_STAT]		= REG_FIELD(0x0B, 0, 0),
1988c0984e5SSebastian Reichel 	/* REG0C */
1998c0984e5SSebastian Reichel 	[F_WD_FAULT]		= REG_FIELD(0x0C, 7, 7),
2008c0984e5SSebastian Reichel 	[F_BOOST_FAULT]		= REG_FIELD(0x0C, 6, 6),
2018c0984e5SSebastian Reichel 	[F_CHG_FAULT]		= REG_FIELD(0x0C, 4, 5),
2028c0984e5SSebastian Reichel 	[F_BAT_FAULT]		= REG_FIELD(0x0C, 3, 3),
2038c0984e5SSebastian Reichel 	[F_NTC_FAULT]		= REG_FIELD(0x0C, 0, 2),
2048c0984e5SSebastian Reichel 	/* REG0D */
2058c0984e5SSebastian Reichel 	[F_FORCE_VINDPM]	= REG_FIELD(0x0D, 7, 7),
2068c0984e5SSebastian Reichel 	[F_VINDPM]		= REG_FIELD(0x0D, 0, 6),
2078c0984e5SSebastian Reichel 	/* REG0E */
2088c0984e5SSebastian Reichel 	[F_THERM_STAT]		= REG_FIELD(0x0E, 7, 7),
2098c0984e5SSebastian Reichel 	[F_BATV]		= REG_FIELD(0x0E, 0, 6),
2108c0984e5SSebastian Reichel 	/* REG0F */
2118c0984e5SSebastian Reichel 	[F_SYSV]		= REG_FIELD(0x0F, 0, 6),
2128c0984e5SSebastian Reichel 	/* REG10 */
2138c0984e5SSebastian Reichel 	[F_TSPCT]		= REG_FIELD(0x10, 0, 6),
2148c0984e5SSebastian Reichel 	/* REG11 */
2158c0984e5SSebastian Reichel 	[F_VBUS_GD]		= REG_FIELD(0x11, 7, 7),
2168c0984e5SSebastian Reichel 	[F_VBUSV]		= REG_FIELD(0x11, 0, 6),
2178c0984e5SSebastian Reichel 	/* REG12 */
2188c0984e5SSebastian Reichel 	[F_ICHGR]		= REG_FIELD(0x12, 0, 6),
2198c0984e5SSebastian Reichel 	/* REG13 */
2208c0984e5SSebastian Reichel 	[F_VDPM_STAT]		= REG_FIELD(0x13, 7, 7),
2218c0984e5SSebastian Reichel 	[F_IDPM_STAT]		= REG_FIELD(0x13, 6, 6),
2228c0984e5SSebastian Reichel 	[F_IDPM_LIM]		= REG_FIELD(0x13, 0, 5),
2238c0984e5SSebastian Reichel 	/* REG14 */
2248c0984e5SSebastian Reichel 	[F_REG_RST]		= REG_FIELD(0x14, 7, 7),
2258c0984e5SSebastian Reichel 	[F_ICO_OPTIMIZED]	= REG_FIELD(0x14, 6, 6),
2268c0984e5SSebastian Reichel 	[F_PN]			= REG_FIELD(0x14, 3, 5),
2278c0984e5SSebastian Reichel 	[F_TS_PROFILE]		= REG_FIELD(0x14, 2, 2),
2288c0984e5SSebastian Reichel 	[F_DEV_REV]		= REG_FIELD(0x14, 0, 1)
2298c0984e5SSebastian Reichel };
2308c0984e5SSebastian Reichel 
2318c0984e5SSebastian Reichel /*
2328c0984e5SSebastian Reichel  * Most of the val -> idx conversions can be computed, given the minimum,
2338c0984e5SSebastian Reichel  * maximum and the step between values. For the rest of conversions, we use
2348c0984e5SSebastian Reichel  * lookup tables.
2358c0984e5SSebastian Reichel  */
2368c0984e5SSebastian Reichel enum bq25890_table_ids {
2378c0984e5SSebastian Reichel 	/* range tables */
2388c0984e5SSebastian Reichel 	TBL_ICHG,
2398c0984e5SSebastian Reichel 	TBL_ITERM,
2408c0984e5SSebastian Reichel 	TBL_VREG,
2418c0984e5SSebastian Reichel 	TBL_BOOSTV,
2428c0984e5SSebastian Reichel 	TBL_SYSVMIN,
2438c0984e5SSebastian Reichel 
2448c0984e5SSebastian Reichel 	/* lookup tables */
2458c0984e5SSebastian Reichel 	TBL_TREG,
2468c0984e5SSebastian Reichel 	TBL_BOOSTI,
2478c0984e5SSebastian Reichel };
2488c0984e5SSebastian Reichel 
2498c0984e5SSebastian Reichel /* Thermal Regulation Threshold lookup table, in degrees Celsius */
2508c0984e5SSebastian Reichel static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
2518c0984e5SSebastian Reichel 
2528c0984e5SSebastian Reichel #define BQ25890_TREG_TBL_SIZE		ARRAY_SIZE(bq25890_treg_tbl)
2538c0984e5SSebastian Reichel 
2548c0984e5SSebastian Reichel /* Boost mode current limit lookup table, in uA */
2558c0984e5SSebastian Reichel static const u32 bq25890_boosti_tbl[] = {
2568c0984e5SSebastian Reichel 	500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
2578c0984e5SSebastian Reichel };
2588c0984e5SSebastian Reichel 
2598c0984e5SSebastian Reichel #define BQ25890_BOOSTI_TBL_SIZE		ARRAY_SIZE(bq25890_boosti_tbl)
2608c0984e5SSebastian Reichel 
2618c0984e5SSebastian Reichel struct bq25890_range {
2628c0984e5SSebastian Reichel 	u32 min;
2638c0984e5SSebastian Reichel 	u32 max;
2648c0984e5SSebastian Reichel 	u32 step;
2658c0984e5SSebastian Reichel };
2668c0984e5SSebastian Reichel 
2678c0984e5SSebastian Reichel struct bq25890_lookup {
2688c0984e5SSebastian Reichel 	const u32 *tbl;
2698c0984e5SSebastian Reichel 	u32 size;
2708c0984e5SSebastian Reichel };
2718c0984e5SSebastian Reichel 
2728c0984e5SSebastian Reichel static const union {
2738c0984e5SSebastian Reichel 	struct bq25890_range  rt;
2748c0984e5SSebastian Reichel 	struct bq25890_lookup lt;
2758c0984e5SSebastian Reichel } bq25890_tables[] = {
2768c0984e5SSebastian Reichel 	/* range tables */
2778c0984e5SSebastian Reichel 	[TBL_ICHG] =	{ .rt = {0,	  5056000, 64000} },	 /* uA */
2788c0984e5SSebastian Reichel 	[TBL_ITERM] =	{ .rt = {64000,   1024000, 64000} },	 /* uA */
2798c0984e5SSebastian Reichel 	[TBL_VREG] =	{ .rt = {3840000, 4608000, 16000} },	 /* uV */
2808c0984e5SSebastian Reichel 	[TBL_BOOSTV] =	{ .rt = {4550000, 5510000, 64000} },	 /* uV */
2818c0984e5SSebastian Reichel 	[TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} },	 /* uV */
2828c0984e5SSebastian Reichel 
2838c0984e5SSebastian Reichel 	/* lookup tables */
2848c0984e5SSebastian Reichel 	[TBL_TREG] =	{ .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
2858c0984e5SSebastian Reichel 	[TBL_BOOSTI] =	{ .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
2868c0984e5SSebastian Reichel };
2878c0984e5SSebastian Reichel 
2888c0984e5SSebastian Reichel static int bq25890_field_read(struct bq25890_device *bq,
2898c0984e5SSebastian Reichel 			      enum bq25890_fields field_id)
2908c0984e5SSebastian Reichel {
2918c0984e5SSebastian Reichel 	int ret;
2928c0984e5SSebastian Reichel 	int val;
2938c0984e5SSebastian Reichel 
2948c0984e5SSebastian Reichel 	ret = regmap_field_read(bq->rmap_fields[field_id], &val);
2958c0984e5SSebastian Reichel 	if (ret < 0)
2968c0984e5SSebastian Reichel 		return ret;
2978c0984e5SSebastian Reichel 
2988c0984e5SSebastian Reichel 	return val;
2998c0984e5SSebastian Reichel }
3008c0984e5SSebastian Reichel 
3018c0984e5SSebastian Reichel static int bq25890_field_write(struct bq25890_device *bq,
3028c0984e5SSebastian Reichel 			       enum bq25890_fields field_id, u8 val)
3038c0984e5SSebastian Reichel {
3048c0984e5SSebastian Reichel 	return regmap_field_write(bq->rmap_fields[field_id], val);
3058c0984e5SSebastian Reichel }
3068c0984e5SSebastian Reichel 
3078c0984e5SSebastian Reichel static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
3088c0984e5SSebastian Reichel {
3098c0984e5SSebastian Reichel 	u8 idx;
3108c0984e5SSebastian Reichel 
3118c0984e5SSebastian Reichel 	if (id >= TBL_TREG) {
3128c0984e5SSebastian Reichel 		const u32 *tbl = bq25890_tables[id].lt.tbl;
3138c0984e5SSebastian Reichel 		u32 tbl_size = bq25890_tables[id].lt.size;
3148c0984e5SSebastian Reichel 
3158c0984e5SSebastian Reichel 		for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
3168c0984e5SSebastian Reichel 			;
3178c0984e5SSebastian Reichel 	} else {
3188c0984e5SSebastian Reichel 		const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
3198c0984e5SSebastian Reichel 		u8 rtbl_size;
3208c0984e5SSebastian Reichel 
3218c0984e5SSebastian Reichel 		rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
3228c0984e5SSebastian Reichel 
3238c0984e5SSebastian Reichel 		for (idx = 1;
3248c0984e5SSebastian Reichel 		     idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
3258c0984e5SSebastian Reichel 		     idx++)
3268c0984e5SSebastian Reichel 			;
3278c0984e5SSebastian Reichel 	}
3288c0984e5SSebastian Reichel 
3298c0984e5SSebastian Reichel 	return idx - 1;
3308c0984e5SSebastian Reichel }
3318c0984e5SSebastian Reichel 
3328c0984e5SSebastian Reichel static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
3338c0984e5SSebastian Reichel {
3348c0984e5SSebastian Reichel 	const struct bq25890_range *rtbl;
3358c0984e5SSebastian Reichel 
3368c0984e5SSebastian Reichel 	/* lookup table? */
3378c0984e5SSebastian Reichel 	if (id >= TBL_TREG)
3388c0984e5SSebastian Reichel 		return bq25890_tables[id].lt.tbl[idx];
3398c0984e5SSebastian Reichel 
3408c0984e5SSebastian Reichel 	/* range table */
3418c0984e5SSebastian Reichel 	rtbl = &bq25890_tables[id].rt;
3428c0984e5SSebastian Reichel 
3438c0984e5SSebastian Reichel 	return (rtbl->min + idx * rtbl->step);
3448c0984e5SSebastian Reichel }
3458c0984e5SSebastian Reichel 
3468c0984e5SSebastian Reichel enum bq25890_status {
3478c0984e5SSebastian Reichel 	STATUS_NOT_CHARGING,
3488c0984e5SSebastian Reichel 	STATUS_PRE_CHARGING,
3498c0984e5SSebastian Reichel 	STATUS_FAST_CHARGING,
3508c0984e5SSebastian Reichel 	STATUS_TERMINATION_DONE,
3518c0984e5SSebastian Reichel };
3528c0984e5SSebastian Reichel 
3538c0984e5SSebastian Reichel enum bq25890_chrg_fault {
3548c0984e5SSebastian Reichel 	CHRG_FAULT_NORMAL,
3558c0984e5SSebastian Reichel 	CHRG_FAULT_INPUT,
3568c0984e5SSebastian Reichel 	CHRG_FAULT_THERMAL_SHUTDOWN,
3578c0984e5SSebastian Reichel 	CHRG_FAULT_TIMER_EXPIRED,
3588c0984e5SSebastian Reichel };
3598c0984e5SSebastian Reichel 
3608c0984e5SSebastian Reichel static int bq25890_power_supply_get_property(struct power_supply *psy,
3618c0984e5SSebastian Reichel 					     enum power_supply_property psp,
3628c0984e5SSebastian Reichel 					     union power_supply_propval *val)
3638c0984e5SSebastian Reichel {
3648c0984e5SSebastian Reichel 	int ret;
3658c0984e5SSebastian Reichel 	struct bq25890_device *bq = power_supply_get_drvdata(psy);
3668c0984e5SSebastian Reichel 	struct bq25890_state state;
3678c0984e5SSebastian Reichel 
3688c0984e5SSebastian Reichel 	mutex_lock(&bq->lock);
3698c0984e5SSebastian Reichel 	state = bq->state;
3708c0984e5SSebastian Reichel 	mutex_unlock(&bq->lock);
3718c0984e5SSebastian Reichel 
3728c0984e5SSebastian Reichel 	switch (psp) {
3738c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
3748c0984e5SSebastian Reichel 		if (!state.online)
3758c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
3768c0984e5SSebastian Reichel 		else if (state.chrg_status == STATUS_NOT_CHARGING)
3778c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
3788c0984e5SSebastian Reichel 		else if (state.chrg_status == STATUS_PRE_CHARGING ||
3798c0984e5SSebastian Reichel 			 state.chrg_status == STATUS_FAST_CHARGING)
3808c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
3818c0984e5SSebastian Reichel 		else if (state.chrg_status == STATUS_TERMINATION_DONE)
3828c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_FULL;
3838c0984e5SSebastian Reichel 		else
3848c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
3858c0984e5SSebastian Reichel 
3868c0984e5SSebastian Reichel 		break;
3878c0984e5SSebastian Reichel 
3888c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
3898c0984e5SSebastian Reichel 		val->strval = BQ25890_MANUFACTURER;
3908c0984e5SSebastian Reichel 		break;
3918c0984e5SSebastian Reichel 
3922e1a2ddeSAngus Ainslie (Purism) 	case POWER_SUPPLY_PROP_MODEL_NAME:
3932e1a2ddeSAngus Ainslie (Purism) 		if (bq->chip_id == BQ25890_ID)
3942e1a2ddeSAngus Ainslie (Purism) 			val->strval = "BQ25890";
3952e1a2ddeSAngus Ainslie (Purism) 		else if (bq->chip_id == BQ25896_ID)
3962e1a2ddeSAngus Ainslie (Purism) 			val->strval = "BQ25896";
3972e1a2ddeSAngus Ainslie (Purism) 		else
3982e1a2ddeSAngus Ainslie (Purism) 			val->strval = "UNKNOWN";
3992e1a2ddeSAngus Ainslie (Purism) 
4002e1a2ddeSAngus Ainslie (Purism) 		break;
4012e1a2ddeSAngus Ainslie (Purism) 
4028c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
4038c0984e5SSebastian Reichel 		val->intval = state.online;
4048c0984e5SSebastian Reichel 		break;
4058c0984e5SSebastian Reichel 
4068c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
4078c0984e5SSebastian Reichel 		if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
4088c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
4098c0984e5SSebastian Reichel 		else if (state.bat_fault)
4108c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
4118c0984e5SSebastian Reichel 		else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
4128c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
4138c0984e5SSebastian Reichel 		else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
4148c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
4158c0984e5SSebastian Reichel 		else
4168c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
4178c0984e5SSebastian Reichel 		break;
4188c0984e5SSebastian Reichel 
4198c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
4208c0984e5SSebastian Reichel 		ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
4218c0984e5SSebastian Reichel 		if (ret < 0)
4228c0984e5SSebastian Reichel 			return ret;
4238c0984e5SSebastian Reichel 
4248c0984e5SSebastian Reichel 		/* converted_val = ADC_val * 50mA (table 10.3.19) */
4258c0984e5SSebastian Reichel 		val->intval = ret * 50000;
4268c0984e5SSebastian Reichel 		break;
4278c0984e5SSebastian Reichel 
4288c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
429f83a6eceSMichał Mirosław 		val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
4308c0984e5SSebastian Reichel 		break;
4318c0984e5SSebastian Reichel 
4328c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
4338c0984e5SSebastian Reichel 		if (!state.online) {
4348c0984e5SSebastian Reichel 			val->intval = 0;
4358c0984e5SSebastian Reichel 			break;
4368c0984e5SSebastian Reichel 		}
4378c0984e5SSebastian Reichel 
4388c0984e5SSebastian Reichel 		ret = bq25890_field_read(bq, F_BATV); /* read measured value */
4398c0984e5SSebastian Reichel 		if (ret < 0)
4408c0984e5SSebastian Reichel 			return ret;
4418c0984e5SSebastian Reichel 
4428c0984e5SSebastian Reichel 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
4438c0984e5SSebastian Reichel 		val->intval = 2304000 + ret * 20000;
4448c0984e5SSebastian Reichel 		break;
4458c0984e5SSebastian Reichel 
4468c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
447f83a6eceSMichał Mirosław 		val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
4488c0984e5SSebastian Reichel 		break;
4498c0984e5SSebastian Reichel 
4508c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
4518c0984e5SSebastian Reichel 		val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
4528c0984e5SSebastian Reichel 		break;
4538c0984e5SSebastian Reichel 
454ae6fe7a3SAngus Ainslie (Purism) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
455ae6fe7a3SAngus Ainslie (Purism) 		ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
456ae6fe7a3SAngus Ainslie (Purism) 		if (ret < 0)
457ae6fe7a3SAngus Ainslie (Purism) 			return ret;
458ae6fe7a3SAngus Ainslie (Purism) 
459ae6fe7a3SAngus Ainslie (Purism) 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
460ae6fe7a3SAngus Ainslie (Purism) 		val->intval = 2304000 + ret * 20000;
461ae6fe7a3SAngus Ainslie (Purism) 		break;
462ae6fe7a3SAngus Ainslie (Purism) 
4638c0984e5SSebastian Reichel 	default:
4648c0984e5SSebastian Reichel 		return -EINVAL;
4658c0984e5SSebastian Reichel 	}
4668c0984e5SSebastian Reichel 
4678c0984e5SSebastian Reichel 	return 0;
4688c0984e5SSebastian Reichel }
4698c0984e5SSebastian Reichel 
4708c0984e5SSebastian Reichel static int bq25890_get_chip_state(struct bq25890_device *bq,
4718c0984e5SSebastian Reichel 				  struct bq25890_state *state)
4728c0984e5SSebastian Reichel {
4738c0984e5SSebastian Reichel 	int i, ret;
4748c0984e5SSebastian Reichel 
4758c0984e5SSebastian Reichel 	struct {
4768c0984e5SSebastian Reichel 		enum bq25890_fields id;
4778c0984e5SSebastian Reichel 		u8 *data;
4788c0984e5SSebastian Reichel 	} state_fields[] = {
4798c0984e5SSebastian Reichel 		{F_CHG_STAT,	&state->chrg_status},
4808c0984e5SSebastian Reichel 		{F_PG_STAT,	&state->online},
4818c0984e5SSebastian Reichel 		{F_VSYS_STAT,	&state->vsys_status},
4828c0984e5SSebastian Reichel 		{F_BOOST_FAULT, &state->boost_fault},
4838c0984e5SSebastian Reichel 		{F_BAT_FAULT,	&state->bat_fault},
4848c0984e5SSebastian Reichel 		{F_CHG_FAULT,	&state->chrg_fault}
4858c0984e5SSebastian Reichel 	};
4868c0984e5SSebastian Reichel 
4878c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
4888c0984e5SSebastian Reichel 		ret = bq25890_field_read(bq, state_fields[i].id);
4898c0984e5SSebastian Reichel 		if (ret < 0)
4908c0984e5SSebastian Reichel 			return ret;
4918c0984e5SSebastian Reichel 
4928c0984e5SSebastian Reichel 		*state_fields[i].data = ret;
4938c0984e5SSebastian Reichel 	}
4948c0984e5SSebastian Reichel 
4958c0984e5SSebastian Reichel 	dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
4968c0984e5SSebastian Reichel 		state->chrg_status, state->online, state->vsys_status,
4978c0984e5SSebastian Reichel 		state->chrg_fault, state->boost_fault, state->bat_fault);
4988c0984e5SSebastian Reichel 
4998c0984e5SSebastian Reichel 	return 0;
5008c0984e5SSebastian Reichel }
5018c0984e5SSebastian Reichel 
5028c0984e5SSebastian Reichel static bool bq25890_state_changed(struct bq25890_device *bq,
5038c0984e5SSebastian Reichel 				  struct bq25890_state *new_state)
5048c0984e5SSebastian Reichel {
5058c0984e5SSebastian Reichel 	struct bq25890_state old_state;
5068c0984e5SSebastian Reichel 
5078c0984e5SSebastian Reichel 	mutex_lock(&bq->lock);
5088c0984e5SSebastian Reichel 	old_state = bq->state;
5098c0984e5SSebastian Reichel 	mutex_unlock(&bq->lock);
5108c0984e5SSebastian Reichel 
5118c0984e5SSebastian Reichel 	return (old_state.chrg_status != new_state->chrg_status ||
5128c0984e5SSebastian Reichel 		old_state.chrg_fault != new_state->chrg_fault	||
5138c0984e5SSebastian Reichel 		old_state.online != new_state->online		||
5148c0984e5SSebastian Reichel 		old_state.bat_fault != new_state->bat_fault	||
5158c0984e5SSebastian Reichel 		old_state.boost_fault != new_state->boost_fault ||
5168c0984e5SSebastian Reichel 		old_state.vsys_status != new_state->vsys_status);
5178c0984e5SSebastian Reichel }
5188c0984e5SSebastian Reichel 
5198c0984e5SSebastian Reichel static void bq25890_handle_state_change(struct bq25890_device *bq,
5208c0984e5SSebastian Reichel 					struct bq25890_state *new_state)
5218c0984e5SSebastian Reichel {
5228c0984e5SSebastian Reichel 	int ret;
5238c0984e5SSebastian Reichel 	struct bq25890_state old_state;
5248c0984e5SSebastian Reichel 
5258c0984e5SSebastian Reichel 	mutex_lock(&bq->lock);
5268c0984e5SSebastian Reichel 	old_state = bq->state;
5278c0984e5SSebastian Reichel 	mutex_unlock(&bq->lock);
5288c0984e5SSebastian Reichel 
5298c0984e5SSebastian Reichel 	if (!new_state->online) {			     /* power removed */
5308c0984e5SSebastian Reichel 		/* disable ADC */
5318c0984e5SSebastian Reichel 		ret = bq25890_field_write(bq, F_CONV_START, 0);
5328c0984e5SSebastian Reichel 		if (ret < 0)
5338c0984e5SSebastian Reichel 			goto error;
5348c0984e5SSebastian Reichel 	} else if (!old_state.online) {			    /* power inserted */
5358c0984e5SSebastian Reichel 		/* enable ADC, to have control of charge current/voltage */
5368c0984e5SSebastian Reichel 		ret = bq25890_field_write(bq, F_CONV_START, 1);
5378c0984e5SSebastian Reichel 		if (ret < 0)
5388c0984e5SSebastian Reichel 			goto error;
5398c0984e5SSebastian Reichel 	}
5408c0984e5SSebastian Reichel 
5418c0984e5SSebastian Reichel 	return;
5428c0984e5SSebastian Reichel 
5438c0984e5SSebastian Reichel error:
5448c0984e5SSebastian Reichel 	dev_err(bq->dev, "Error communicating with the chip.\n");
5458c0984e5SSebastian Reichel }
5468c0984e5SSebastian Reichel 
5478c0984e5SSebastian Reichel static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
5488c0984e5SSebastian Reichel {
5498c0984e5SSebastian Reichel 	struct bq25890_device *bq = private;
5508c0984e5SSebastian Reichel 	int ret;
5518c0984e5SSebastian Reichel 	struct bq25890_state state;
5528c0984e5SSebastian Reichel 
5538c0984e5SSebastian Reichel 	ret = bq25890_get_chip_state(bq, &state);
5548c0984e5SSebastian Reichel 	if (ret < 0)
5558c0984e5SSebastian Reichel 		goto handled;
5568c0984e5SSebastian Reichel 
5578c0984e5SSebastian Reichel 	if (!bq25890_state_changed(bq, &state))
5588c0984e5SSebastian Reichel 		goto handled;
5598c0984e5SSebastian Reichel 
5608c0984e5SSebastian Reichel 	bq25890_handle_state_change(bq, &state);
5618c0984e5SSebastian Reichel 
5628c0984e5SSebastian Reichel 	mutex_lock(&bq->lock);
5638c0984e5SSebastian Reichel 	bq->state = state;
5648c0984e5SSebastian Reichel 	mutex_unlock(&bq->lock);
5658c0984e5SSebastian Reichel 
5668c0984e5SSebastian Reichel 	power_supply_changed(bq->charger);
5678c0984e5SSebastian Reichel 
5688c0984e5SSebastian Reichel handled:
5698c0984e5SSebastian Reichel 	return IRQ_HANDLED;
5708c0984e5SSebastian Reichel }
5718c0984e5SSebastian Reichel 
5728c0984e5SSebastian Reichel static int bq25890_chip_reset(struct bq25890_device *bq)
5738c0984e5SSebastian Reichel {
5748c0984e5SSebastian Reichel 	int ret;
5758c0984e5SSebastian Reichel 	int rst_check_counter = 10;
5768c0984e5SSebastian Reichel 
5778c0984e5SSebastian Reichel 	ret = bq25890_field_write(bq, F_REG_RST, 1);
5788c0984e5SSebastian Reichel 	if (ret < 0)
5798c0984e5SSebastian Reichel 		return ret;
5808c0984e5SSebastian Reichel 
5818c0984e5SSebastian Reichel 	do {
5828c0984e5SSebastian Reichel 		ret = bq25890_field_read(bq, F_REG_RST);
5838c0984e5SSebastian Reichel 		if (ret < 0)
5848c0984e5SSebastian Reichel 			return ret;
5858c0984e5SSebastian Reichel 
5868c0984e5SSebastian Reichel 		usleep_range(5, 10);
5878c0984e5SSebastian Reichel 	} while (ret == 1 && --rst_check_counter);
5888c0984e5SSebastian Reichel 
5898c0984e5SSebastian Reichel 	if (!rst_check_counter)
5908c0984e5SSebastian Reichel 		return -ETIMEDOUT;
5918c0984e5SSebastian Reichel 
5928c0984e5SSebastian Reichel 	return 0;
5938c0984e5SSebastian Reichel }
5948c0984e5SSebastian Reichel 
5958c0984e5SSebastian Reichel static int bq25890_hw_init(struct bq25890_device *bq)
5968c0984e5SSebastian Reichel {
5978c0984e5SSebastian Reichel 	int ret;
5988c0984e5SSebastian Reichel 	int i;
5998c0984e5SSebastian Reichel 	struct bq25890_state state;
6008c0984e5SSebastian Reichel 
6018c0984e5SSebastian Reichel 	const struct {
6028c0984e5SSebastian Reichel 		enum bq25890_fields id;
6038c0984e5SSebastian Reichel 		u32 value;
6048c0984e5SSebastian Reichel 	} init_data[] = {
6058c0984e5SSebastian Reichel 		{F_ICHG,	 bq->init_data.ichg},
6068c0984e5SSebastian Reichel 		{F_VREG,	 bq->init_data.vreg},
6078c0984e5SSebastian Reichel 		{F_ITERM,	 bq->init_data.iterm},
6088c0984e5SSebastian Reichel 		{F_IPRECHG,	 bq->init_data.iprechg},
6098c0984e5SSebastian Reichel 		{F_SYSVMIN,	 bq->init_data.sysvmin},
6108c0984e5SSebastian Reichel 		{F_BOOSTV,	 bq->init_data.boostv},
6118c0984e5SSebastian Reichel 		{F_BOOSTI,	 bq->init_data.boosti},
6128c0984e5SSebastian Reichel 		{F_BOOSTF,	 bq->init_data.boostf},
6138c0984e5SSebastian Reichel 		{F_EN_ILIM,	 bq->init_data.ilim_en},
6148c0984e5SSebastian Reichel 		{F_TREG,	 bq->init_data.treg}
6158c0984e5SSebastian Reichel 	};
6168c0984e5SSebastian Reichel 
6178c0984e5SSebastian Reichel 	ret = bq25890_chip_reset(bq);
6189d9ae341SAngus Ainslie (Purism) 	if (ret < 0) {
6199d9ae341SAngus Ainslie (Purism) 		dev_dbg(bq->dev, "Reset failed %d\n", ret);
6208c0984e5SSebastian Reichel 		return ret;
621ad1570d9Skbuild test robot 	}
6228c0984e5SSebastian Reichel 
6238c0984e5SSebastian Reichel 	/* disable watchdog */
6248c0984e5SSebastian Reichel 	ret = bq25890_field_write(bq, F_WD, 0);
6259d9ae341SAngus Ainslie (Purism) 	if (ret < 0) {
6269d9ae341SAngus Ainslie (Purism) 		dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
6278c0984e5SSebastian Reichel 		return ret;
628ad1570d9Skbuild test robot 	}
6298c0984e5SSebastian Reichel 
6308c0984e5SSebastian Reichel 	/* initialize currents/voltages and other parameters */
6318c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
6328c0984e5SSebastian Reichel 		ret = bq25890_field_write(bq, init_data[i].id,
6338c0984e5SSebastian Reichel 					  init_data[i].value);
6349d9ae341SAngus Ainslie (Purism) 		if (ret < 0) {
6359d9ae341SAngus Ainslie (Purism) 			dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
6368c0984e5SSebastian Reichel 			return ret;
637ad1570d9Skbuild test robot 		}
6388c0984e5SSebastian Reichel 	}
6398c0984e5SSebastian Reichel 
6408c0984e5SSebastian Reichel 	/* Configure ADC for continuous conversions. This does not enable it. */
6418c0984e5SSebastian Reichel 	ret = bq25890_field_write(bq, F_CONV_RATE, 1);
6429d9ae341SAngus Ainslie (Purism) 	if (ret < 0) {
6439d9ae341SAngus Ainslie (Purism) 		dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
6448c0984e5SSebastian Reichel 		return ret;
645ad1570d9Skbuild test robot 	}
6468c0984e5SSebastian Reichel 
6478c0984e5SSebastian Reichel 	ret = bq25890_get_chip_state(bq, &state);
6489d9ae341SAngus Ainslie (Purism) 	if (ret < 0) {
6499d9ae341SAngus Ainslie (Purism) 		dev_dbg(bq->dev, "Get state failed %d\n", ret);
6508c0984e5SSebastian Reichel 		return ret;
651ad1570d9Skbuild test robot 	}
6528c0984e5SSebastian Reichel 
6538c0984e5SSebastian Reichel 	mutex_lock(&bq->lock);
6548c0984e5SSebastian Reichel 	bq->state = state;
6558c0984e5SSebastian Reichel 	mutex_unlock(&bq->lock);
6568c0984e5SSebastian Reichel 
6578c0984e5SSebastian Reichel 	return 0;
6588c0984e5SSebastian Reichel }
6598c0984e5SSebastian Reichel 
6608c0984e5SSebastian Reichel static enum power_supply_property bq25890_power_supply_props[] = {
6618c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
6622e1a2ddeSAngus Ainslie (Purism) 	POWER_SUPPLY_PROP_MODEL_NAME,
6638c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6648c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
6658c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
6668c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
6678c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
6688c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
6698c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
6708c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
671ae6fe7a3SAngus Ainslie (Purism) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6728c0984e5SSebastian Reichel };
6738c0984e5SSebastian Reichel 
6748c0984e5SSebastian Reichel static char *bq25890_charger_supplied_to[] = {
6758c0984e5SSebastian Reichel 	"main-battery",
6768c0984e5SSebastian Reichel };
6778c0984e5SSebastian Reichel 
6788c0984e5SSebastian Reichel static const struct power_supply_desc bq25890_power_supply_desc = {
6798c0984e5SSebastian Reichel 	.name = "bq25890-charger",
6808c0984e5SSebastian Reichel 	.type = POWER_SUPPLY_TYPE_USB,
6818c0984e5SSebastian Reichel 	.properties = bq25890_power_supply_props,
6828c0984e5SSebastian Reichel 	.num_properties = ARRAY_SIZE(bq25890_power_supply_props),
6838c0984e5SSebastian Reichel 	.get_property = bq25890_power_supply_get_property,
6848c0984e5SSebastian Reichel };
6858c0984e5SSebastian Reichel 
6868c0984e5SSebastian Reichel static int bq25890_power_supply_init(struct bq25890_device *bq)
6878c0984e5SSebastian Reichel {
6888c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = { .drv_data = bq, };
6898c0984e5SSebastian Reichel 
6908c0984e5SSebastian Reichel 	psy_cfg.supplied_to = bq25890_charger_supplied_to;
6918c0984e5SSebastian Reichel 	psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
6928c0984e5SSebastian Reichel 
6938c0984e5SSebastian Reichel 	bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc,
6948c0984e5SSebastian Reichel 					    &psy_cfg);
6958c0984e5SSebastian Reichel 
6968c0984e5SSebastian Reichel 	return PTR_ERR_OR_ZERO(bq->charger);
6978c0984e5SSebastian Reichel }
6988c0984e5SSebastian Reichel 
6998c0984e5SSebastian Reichel static void bq25890_usb_work(struct work_struct *data)
7008c0984e5SSebastian Reichel {
7018c0984e5SSebastian Reichel 	int ret;
7028c0984e5SSebastian Reichel 	struct bq25890_device *bq =
7038c0984e5SSebastian Reichel 			container_of(data, struct bq25890_device, usb_work);
7048c0984e5SSebastian Reichel 
7058c0984e5SSebastian Reichel 	switch (bq->usb_event) {
7068c0984e5SSebastian Reichel 	case USB_EVENT_ID:
7078c0984e5SSebastian Reichel 		/* Enable boost mode */
7088c0984e5SSebastian Reichel 		ret = bq25890_field_write(bq, F_OTG_CFG, 1);
7098c0984e5SSebastian Reichel 		if (ret < 0)
7108c0984e5SSebastian Reichel 			goto error;
7118c0984e5SSebastian Reichel 		break;
7128c0984e5SSebastian Reichel 
7138c0984e5SSebastian Reichel 	case USB_EVENT_NONE:
7148c0984e5SSebastian Reichel 		/* Disable boost mode */
7158c0984e5SSebastian Reichel 		ret = bq25890_field_write(bq, F_OTG_CFG, 0);
7168c0984e5SSebastian Reichel 		if (ret < 0)
7178c0984e5SSebastian Reichel 			goto error;
7188c0984e5SSebastian Reichel 
7198c0984e5SSebastian Reichel 		power_supply_changed(bq->charger);
7208c0984e5SSebastian Reichel 		break;
7218c0984e5SSebastian Reichel 	}
7228c0984e5SSebastian Reichel 
7238c0984e5SSebastian Reichel 	return;
7248c0984e5SSebastian Reichel 
7258c0984e5SSebastian Reichel error:
7268c0984e5SSebastian Reichel 	dev_err(bq->dev, "Error switching to boost/charger mode.\n");
7278c0984e5SSebastian Reichel }
7288c0984e5SSebastian Reichel 
7298c0984e5SSebastian Reichel static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
7308c0984e5SSebastian Reichel 				void *priv)
7318c0984e5SSebastian Reichel {
7328c0984e5SSebastian Reichel 	struct bq25890_device *bq =
7338c0984e5SSebastian Reichel 			container_of(nb, struct bq25890_device, usb_nb);
7348c0984e5SSebastian Reichel 
7358c0984e5SSebastian Reichel 	bq->usb_event = val;
7368c0984e5SSebastian Reichel 	queue_work(system_power_efficient_wq, &bq->usb_work);
7378c0984e5SSebastian Reichel 
7388c0984e5SSebastian Reichel 	return NOTIFY_OK;
7398c0984e5SSebastian Reichel }
7408c0984e5SSebastian Reichel 
7418c0984e5SSebastian Reichel static int bq25890_irq_probe(struct bq25890_device *bq)
7428c0984e5SSebastian Reichel {
7438c0984e5SSebastian Reichel 	struct gpio_desc *irq;
7448c0984e5SSebastian Reichel 
74586775879SAndy Shevchenko 	irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
7468c0984e5SSebastian Reichel 	if (IS_ERR(irq)) {
7478c0984e5SSebastian Reichel 		dev_err(bq->dev, "Could not probe irq pin.\n");
7488c0984e5SSebastian Reichel 		return PTR_ERR(irq);
7498c0984e5SSebastian Reichel 	}
7508c0984e5SSebastian Reichel 
7518c0984e5SSebastian Reichel 	return gpiod_to_irq(irq);
7528c0984e5SSebastian Reichel }
7538c0984e5SSebastian Reichel 
7548c0984e5SSebastian Reichel static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
7558c0984e5SSebastian Reichel {
7568c0984e5SSebastian Reichel 	int ret;
7578c0984e5SSebastian Reichel 	u32 property;
7588c0984e5SSebastian Reichel 	int i;
7598c0984e5SSebastian Reichel 	struct bq25890_init_data *init = &bq->init_data;
7608c0984e5SSebastian Reichel 	struct {
7618c0984e5SSebastian Reichel 		char *name;
7628c0984e5SSebastian Reichel 		bool optional;
7638c0984e5SSebastian Reichel 		enum bq25890_table_ids tbl_id;
7648c0984e5SSebastian Reichel 		u8 *conv_data; /* holds converted value from given property */
7658c0984e5SSebastian Reichel 	} props[] = {
7668c0984e5SSebastian Reichel 		/* required properties */
7678c0984e5SSebastian Reichel 		{"ti,charge-current", false, TBL_ICHG, &init->ichg},
7688c0984e5SSebastian Reichel 		{"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
7698c0984e5SSebastian Reichel 		{"ti,termination-current", false, TBL_ITERM, &init->iterm},
7708c0984e5SSebastian Reichel 		{"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
7718c0984e5SSebastian Reichel 		{"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
7728c0984e5SSebastian Reichel 		{"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
7738c0984e5SSebastian Reichel 		{"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
7748c0984e5SSebastian Reichel 
7758c0984e5SSebastian Reichel 		/* optional properties */
7768c0984e5SSebastian Reichel 		{"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg}
7778c0984e5SSebastian Reichel 	};
7788c0984e5SSebastian Reichel 
7798c0984e5SSebastian Reichel 	/* initialize data for optional properties */
7808c0984e5SSebastian Reichel 	init->treg = 3; /* 120 degrees Celsius */
7818c0984e5SSebastian Reichel 
7828c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(props); i++) {
7838c0984e5SSebastian Reichel 		ret = device_property_read_u32(bq->dev, props[i].name,
7848c0984e5SSebastian Reichel 					       &property);
7858c0984e5SSebastian Reichel 		if (ret < 0) {
7868c0984e5SSebastian Reichel 			if (props[i].optional)
7878c0984e5SSebastian Reichel 				continue;
7888c0984e5SSebastian Reichel 
7899d9ae341SAngus Ainslie (Purism) 			dev_err(bq->dev, "Unable to read property %d %s\n", ret,
7909d9ae341SAngus Ainslie (Purism) 				props[i].name);
7919d9ae341SAngus Ainslie (Purism) 
7928c0984e5SSebastian Reichel 			return ret;
7938c0984e5SSebastian Reichel 		}
7948c0984e5SSebastian Reichel 
7958c0984e5SSebastian Reichel 		*props[i].conv_data = bq25890_find_idx(property,
7968c0984e5SSebastian Reichel 						       props[i].tbl_id);
7978c0984e5SSebastian Reichel 	}
7988c0984e5SSebastian Reichel 
7998c0984e5SSebastian Reichel 	return 0;
8008c0984e5SSebastian Reichel }
8018c0984e5SSebastian Reichel 
8028c0984e5SSebastian Reichel static int bq25890_fw_probe(struct bq25890_device *bq)
8038c0984e5SSebastian Reichel {
8048c0984e5SSebastian Reichel 	int ret;
8058c0984e5SSebastian Reichel 	struct bq25890_init_data *init = &bq->init_data;
8068c0984e5SSebastian Reichel 
8078c0984e5SSebastian Reichel 	ret = bq25890_fw_read_u32_props(bq);
8088c0984e5SSebastian Reichel 	if (ret < 0)
8098c0984e5SSebastian Reichel 		return ret;
8108c0984e5SSebastian Reichel 
8118c0984e5SSebastian Reichel 	init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
8128c0984e5SSebastian Reichel 	init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
8138c0984e5SSebastian Reichel 
8148c0984e5SSebastian Reichel 	return 0;
8158c0984e5SSebastian Reichel }
8168c0984e5SSebastian Reichel 
8178c0984e5SSebastian Reichel static int bq25890_probe(struct i2c_client *client,
8188c0984e5SSebastian Reichel 			 const struct i2c_device_id *id)
8198c0984e5SSebastian Reichel {
8208c0984e5SSebastian Reichel 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
8218c0984e5SSebastian Reichel 	struct device *dev = &client->dev;
8228c0984e5SSebastian Reichel 	struct bq25890_device *bq;
8238c0984e5SSebastian Reichel 	int ret;
8248c0984e5SSebastian Reichel 	int i;
8258c0984e5SSebastian Reichel 
8268c0984e5SSebastian Reichel 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
8278c0984e5SSebastian Reichel 		dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
8288c0984e5SSebastian Reichel 		return -ENODEV;
8298c0984e5SSebastian Reichel 	}
8308c0984e5SSebastian Reichel 
8318c0984e5SSebastian Reichel 	bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
8328c0984e5SSebastian Reichel 	if (!bq)
8338c0984e5SSebastian Reichel 		return -ENOMEM;
8348c0984e5SSebastian Reichel 
8358c0984e5SSebastian Reichel 	bq->client = client;
8368c0984e5SSebastian Reichel 	bq->dev = dev;
8378c0984e5SSebastian Reichel 
8388c0984e5SSebastian Reichel 	mutex_init(&bq->lock);
8398c0984e5SSebastian Reichel 
8408c0984e5SSebastian Reichel 	bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
8418c0984e5SSebastian Reichel 	if (IS_ERR(bq->rmap)) {
8428c0984e5SSebastian Reichel 		dev_err(dev, "failed to allocate register map\n");
8438c0984e5SSebastian Reichel 		return PTR_ERR(bq->rmap);
8448c0984e5SSebastian Reichel 	}
8458c0984e5SSebastian Reichel 
8468c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
8478c0984e5SSebastian Reichel 		const struct reg_field *reg_fields = bq25890_reg_fields;
8488c0984e5SSebastian Reichel 
8498c0984e5SSebastian Reichel 		bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
8508c0984e5SSebastian Reichel 							     reg_fields[i]);
8518c0984e5SSebastian Reichel 		if (IS_ERR(bq->rmap_fields[i])) {
8528c0984e5SSebastian Reichel 			dev_err(dev, "cannot allocate regmap field\n");
8538c0984e5SSebastian Reichel 			return PTR_ERR(bq->rmap_fields[i]);
8548c0984e5SSebastian Reichel 		}
8558c0984e5SSebastian Reichel 	}
8568c0984e5SSebastian Reichel 
8578c0984e5SSebastian Reichel 	i2c_set_clientdata(client, bq);
8588c0984e5SSebastian Reichel 
8598c0984e5SSebastian Reichel 	bq->chip_id = bq25890_field_read(bq, F_PN);
8608c0984e5SSebastian Reichel 	if (bq->chip_id < 0) {
8618c0984e5SSebastian Reichel 		dev_err(dev, "Cannot read chip ID.\n");
8628c0984e5SSebastian Reichel 		return bq->chip_id;
8638c0984e5SSebastian Reichel 	}
8648c0984e5SSebastian Reichel 
8652e1a2ddeSAngus Ainslie (Purism) 	if ((bq->chip_id != BQ25890_ID) && (bq->chip_id != BQ25896_ID)) {
8668c0984e5SSebastian Reichel 		dev_err(dev, "Chip with ID=%d, not supported!\n", bq->chip_id);
8678c0984e5SSebastian Reichel 		return -ENODEV;
8688c0984e5SSebastian Reichel 	}
8698c0984e5SSebastian Reichel 
8708c0984e5SSebastian Reichel 	if (!dev->platform_data) {
8718c0984e5SSebastian Reichel 		ret = bq25890_fw_probe(bq);
8728c0984e5SSebastian Reichel 		if (ret < 0) {
8738c0984e5SSebastian Reichel 			dev_err(dev, "Cannot read device properties.\n");
8748c0984e5SSebastian Reichel 			return ret;
8758c0984e5SSebastian Reichel 		}
8768c0984e5SSebastian Reichel 	} else {
8778c0984e5SSebastian Reichel 		return -ENODEV;
8788c0984e5SSebastian Reichel 	}
8798c0984e5SSebastian Reichel 
8808c0984e5SSebastian Reichel 	ret = bq25890_hw_init(bq);
8818c0984e5SSebastian Reichel 	if (ret < 0) {
8828c0984e5SSebastian Reichel 		dev_err(dev, "Cannot initialize the chip.\n");
8838c0984e5SSebastian Reichel 		return ret;
8848c0984e5SSebastian Reichel 	}
8858c0984e5SSebastian Reichel 
8868c0984e5SSebastian Reichel 	if (client->irq <= 0)
8878c0984e5SSebastian Reichel 		client->irq = bq25890_irq_probe(bq);
8888c0984e5SSebastian Reichel 
8898c0984e5SSebastian Reichel 	if (client->irq < 0) {
8908c0984e5SSebastian Reichel 		dev_err(dev, "No irq resource found.\n");
8918c0984e5SSebastian Reichel 		return client->irq;
8928c0984e5SSebastian Reichel 	}
8938c0984e5SSebastian Reichel 
8948c0984e5SSebastian Reichel 	/* OTG reporting */
8958c0984e5SSebastian Reichel 	bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
8968c0984e5SSebastian Reichel 	if (!IS_ERR_OR_NULL(bq->usb_phy)) {
8978c0984e5SSebastian Reichel 		INIT_WORK(&bq->usb_work, bq25890_usb_work);
8988c0984e5SSebastian Reichel 		bq->usb_nb.notifier_call = bq25890_usb_notifier;
8998c0984e5SSebastian Reichel 		usb_register_notifier(bq->usb_phy, &bq->usb_nb);
9008c0984e5SSebastian Reichel 	}
9018c0984e5SSebastian Reichel 
9028c0984e5SSebastian Reichel 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
9038c0984e5SSebastian Reichel 					bq25890_irq_handler_thread,
9048c0984e5SSebastian Reichel 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
9058c0984e5SSebastian Reichel 					BQ25890_IRQ_PIN, bq);
9068c0984e5SSebastian Reichel 	if (ret)
9078c0984e5SSebastian Reichel 		goto irq_fail;
9088c0984e5SSebastian Reichel 
9098c0984e5SSebastian Reichel 	ret = bq25890_power_supply_init(bq);
9108c0984e5SSebastian Reichel 	if (ret < 0) {
9118c0984e5SSebastian Reichel 		dev_err(dev, "Failed to register power supply\n");
9128c0984e5SSebastian Reichel 		goto irq_fail;
9138c0984e5SSebastian Reichel 	}
9148c0984e5SSebastian Reichel 
9158c0984e5SSebastian Reichel 	return 0;
9168c0984e5SSebastian Reichel 
9178c0984e5SSebastian Reichel irq_fail:
9188c0984e5SSebastian Reichel 	if (!IS_ERR_OR_NULL(bq->usb_phy))
9198c0984e5SSebastian Reichel 		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
9208c0984e5SSebastian Reichel 
9218c0984e5SSebastian Reichel 	return ret;
9228c0984e5SSebastian Reichel }
9238c0984e5SSebastian Reichel 
9248c0984e5SSebastian Reichel static int bq25890_remove(struct i2c_client *client)
9258c0984e5SSebastian Reichel {
9268c0984e5SSebastian Reichel 	struct bq25890_device *bq = i2c_get_clientdata(client);
9278c0984e5SSebastian Reichel 
9288c0984e5SSebastian Reichel 	power_supply_unregister(bq->charger);
9298c0984e5SSebastian Reichel 
9308c0984e5SSebastian Reichel 	if (!IS_ERR_OR_NULL(bq->usb_phy))
9318c0984e5SSebastian Reichel 		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
9328c0984e5SSebastian Reichel 
9338c0984e5SSebastian Reichel 	/* reset all registers to default values */
9348c0984e5SSebastian Reichel 	bq25890_chip_reset(bq);
9358c0984e5SSebastian Reichel 
9368c0984e5SSebastian Reichel 	return 0;
9378c0984e5SSebastian Reichel }
9388c0984e5SSebastian Reichel 
9398c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP
9408c0984e5SSebastian Reichel static int bq25890_suspend(struct device *dev)
9418c0984e5SSebastian Reichel {
9428c0984e5SSebastian Reichel 	struct bq25890_device *bq = dev_get_drvdata(dev);
9438c0984e5SSebastian Reichel 
9448c0984e5SSebastian Reichel 	/*
9458c0984e5SSebastian Reichel 	 * If charger is removed, while in suspend, make sure ADC is diabled
9468c0984e5SSebastian Reichel 	 * since it consumes slightly more power.
9478c0984e5SSebastian Reichel 	 */
9488c0984e5SSebastian Reichel 	return bq25890_field_write(bq, F_CONV_START, 0);
9498c0984e5SSebastian Reichel }
9508c0984e5SSebastian Reichel 
9518c0984e5SSebastian Reichel static int bq25890_resume(struct device *dev)
9528c0984e5SSebastian Reichel {
9538c0984e5SSebastian Reichel 	int ret;
9548c0984e5SSebastian Reichel 	struct bq25890_state state;
9558c0984e5SSebastian Reichel 	struct bq25890_device *bq = dev_get_drvdata(dev);
9568c0984e5SSebastian Reichel 
9578c0984e5SSebastian Reichel 	ret = bq25890_get_chip_state(bq, &state);
9588c0984e5SSebastian Reichel 	if (ret < 0)
9598c0984e5SSebastian Reichel 		return ret;
9608c0984e5SSebastian Reichel 
9618c0984e5SSebastian Reichel 	mutex_lock(&bq->lock);
9628c0984e5SSebastian Reichel 	bq->state = state;
9638c0984e5SSebastian Reichel 	mutex_unlock(&bq->lock);
9648c0984e5SSebastian Reichel 
9658c0984e5SSebastian Reichel 	/* Re-enable ADC only if charger is plugged in. */
9668c0984e5SSebastian Reichel 	if (state.online) {
9678c0984e5SSebastian Reichel 		ret = bq25890_field_write(bq, F_CONV_START, 1);
9688c0984e5SSebastian Reichel 		if (ret < 0)
9698c0984e5SSebastian Reichel 			return ret;
9708c0984e5SSebastian Reichel 	}
9718c0984e5SSebastian Reichel 
9728c0984e5SSebastian Reichel 	/* signal userspace, maybe state changed while suspended */
9738c0984e5SSebastian Reichel 	power_supply_changed(bq->charger);
9748c0984e5SSebastian Reichel 
9758c0984e5SSebastian Reichel 	return 0;
9768c0984e5SSebastian Reichel }
9778c0984e5SSebastian Reichel #endif
9788c0984e5SSebastian Reichel 
9798c0984e5SSebastian Reichel static const struct dev_pm_ops bq25890_pm = {
9808c0984e5SSebastian Reichel 	SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
9818c0984e5SSebastian Reichel };
9828c0984e5SSebastian Reichel 
9838c0984e5SSebastian Reichel static const struct i2c_device_id bq25890_i2c_ids[] = {
9848c0984e5SSebastian Reichel 	{ "bq25890", 0 },
9858c0984e5SSebastian Reichel 	{},
9868c0984e5SSebastian Reichel };
9878c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
9888c0984e5SSebastian Reichel 
9898c0984e5SSebastian Reichel static const struct of_device_id bq25890_of_match[] = {
9908c0984e5SSebastian Reichel 	{ .compatible = "ti,bq25890", },
9918c0984e5SSebastian Reichel 	{ },
9928c0984e5SSebastian Reichel };
9938c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, bq25890_of_match);
9948c0984e5SSebastian Reichel 
9958c0984e5SSebastian Reichel static const struct acpi_device_id bq25890_acpi_match[] = {
9968c0984e5SSebastian Reichel 	{"BQ258900", 0},
9978c0984e5SSebastian Reichel 	{},
9988c0984e5SSebastian Reichel };
9998c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
10008c0984e5SSebastian Reichel 
10018c0984e5SSebastian Reichel static struct i2c_driver bq25890_driver = {
10028c0984e5SSebastian Reichel 	.driver = {
10038c0984e5SSebastian Reichel 		.name = "bq25890-charger",
10048c0984e5SSebastian Reichel 		.of_match_table = of_match_ptr(bq25890_of_match),
10058c0984e5SSebastian Reichel 		.acpi_match_table = ACPI_PTR(bq25890_acpi_match),
10068c0984e5SSebastian Reichel 		.pm = &bq25890_pm,
10078c0984e5SSebastian Reichel 	},
10088c0984e5SSebastian Reichel 	.probe = bq25890_probe,
10098c0984e5SSebastian Reichel 	.remove = bq25890_remove,
10108c0984e5SSebastian Reichel 	.id_table = bq25890_i2c_ids,
10118c0984e5SSebastian Reichel };
10128c0984e5SSebastian Reichel module_i2c_driver(bq25890_driver);
10138c0984e5SSebastian Reichel 
10148c0984e5SSebastian Reichel MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
10158c0984e5SSebastian Reichel MODULE_DESCRIPTION("bq25890 charger driver");
10168c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
1017