xref: /linux/drivers/power/supply/bq27xxx_battery.c (revision 7bf738ba110722b63e9dc8af760d3fb2aef25593)
144ff56c0SDan Murphy // SPDX-License-Identifier: GPL-2.0
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * BQ27xxx battery driver
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
68c0984e5SSebastian Reichel  * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
78c0984e5SSebastian Reichel  * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
8149ed3d4SPali Rohár  * Copyright (C) 2011 Pali Rohár <pali@kernel.org>
90670c9b3SLiam Breck  * Copyright (C) 2017 Liam Breck <kernel@networkimprov.net>
108c0984e5SSebastian Reichel  *
118c0984e5SSebastian Reichel  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
128c0984e5SSebastian Reichel  *
138c0984e5SSebastian Reichel  * Datasheets:
1481bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27000
1581bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27200
1681bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27010
1781bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27210
1881bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27500
1981bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27510-g1
2081bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27510-g2
2181bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27510-g3
2281bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27520-g1
2381bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27520-g2
2481bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27520-g3
2581bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27520-g4
2681bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27530-g1
2781bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27531-g1
2881bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27541-g1
2981bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27542-g1
3081bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27546-g1
3181bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27742-g1
3281bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27545-g1
3381bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27421-g1
3481bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27425-g1
3581bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27426
3681bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27411-g1
3781bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27441-g1
3881bd45fcSAlexander A. Klimov  * https://www.ti.com/product/bq27621-g1
396f24ff97SDan Murphy  * https://www.ti.com/product/bq27z561
40707d678aSDan Murphy  * https://www.ti.com/product/bq28z610
418c0984e5SSebastian Reichel  */
428c0984e5SSebastian Reichel 
438c0984e5SSebastian Reichel #include <linux/device.h>
448c0984e5SSebastian Reichel #include <linux/module.h>
451d72706fSMatt Ranostay #include <linux/mutex.h>
468c0984e5SSebastian Reichel #include <linux/param.h>
478c0984e5SSebastian Reichel #include <linux/jiffies.h>
488c0984e5SSebastian Reichel #include <linux/workqueue.h>
498c0984e5SSebastian Reichel #include <linux/delay.h>
508c0984e5SSebastian Reichel #include <linux/platform_device.h>
518c0984e5SSebastian Reichel #include <linux/power_supply.h>
528c0984e5SSebastian Reichel #include <linux/slab.h>
538c0984e5SSebastian Reichel #include <linux/of.h>
548c0984e5SSebastian Reichel 
558c0984e5SSebastian Reichel #include <linux/power/bq27xxx_battery.h>
568c0984e5SSebastian Reichel 
578c0984e5SSebastian Reichel #define BQ27XXX_MANUFACTURER	"Texas Instruments"
588c0984e5SSebastian Reichel 
598c0984e5SSebastian Reichel /* BQ27XXX Flags */
608c0984e5SSebastian Reichel #define BQ27XXX_FLAG_DSC	BIT(0)
618c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOCF	BIT(1) /* State-of-Charge threshold final */
628c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOC1	BIT(2) /* State-of-Charge threshold 1 */
630670c9b3SLiam Breck #define BQ27XXX_FLAG_CFGUP	BIT(4)
648c0984e5SSebastian Reichel #define BQ27XXX_FLAG_FC		BIT(9)
658c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OTD	BIT(14)
668c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OTC	BIT(15)
678c0984e5SSebastian Reichel #define BQ27XXX_FLAG_UT		BIT(14)
688c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OT		BIT(15)
698c0984e5SSebastian Reichel 
708c0984e5SSebastian Reichel /* BQ27000 has different layout for Flags register */
718c0984e5SSebastian Reichel #define BQ27000_FLAG_EDVF	BIT(0) /* Final End-of-Discharge-Voltage flag */
728c0984e5SSebastian Reichel #define BQ27000_FLAG_EDV1	BIT(1) /* First End-of-Discharge-Voltage flag */
738c0984e5SSebastian Reichel #define BQ27000_FLAG_CI		BIT(4) /* Capacity Inaccurate flag */
748c0984e5SSebastian Reichel #define BQ27000_FLAG_FC		BIT(5)
758c0984e5SSebastian Reichel #define BQ27000_FLAG_CHGS	BIT(7) /* Charge state flag */
768c0984e5SSebastian Reichel 
776f24ff97SDan Murphy /* BQ27Z561 has different layout for Flags register */
786f24ff97SDan Murphy #define BQ27Z561_FLAG_FDC	BIT(4) /* Battery fully discharged */
796f24ff97SDan Murphy #define BQ27Z561_FLAG_FC		BIT(5) /* Battery fully charged */
806f24ff97SDan Murphy #define BQ27Z561_FLAG_DIS_CH	BIT(6) /* Battery is discharging */
816f24ff97SDan Murphy 
820670c9b3SLiam Breck /* control register params */
830670c9b3SLiam Breck #define BQ27XXX_SEALED			0x20
840670c9b3SLiam Breck #define BQ27XXX_SET_CFGUPDATE		0x13
850670c9b3SLiam Breck #define BQ27XXX_SOFT_RESET		0x42
860670c9b3SLiam Breck #define BQ27XXX_RESET			0x41
870670c9b3SLiam Breck 
888c0984e5SSebastian Reichel #define BQ27XXX_RS			(20) /* Resistor sense mOhm */
898c0984e5SSebastian Reichel #define BQ27XXX_POWER_CONSTANT		(29200) /* 29.2 µV^2 * 1000 */
908c0984e5SSebastian Reichel #define BQ27XXX_CURRENT_CONSTANT	(3570) /* 3.57 µV * 1000 */
918c0984e5SSebastian Reichel 
928c0984e5SSebastian Reichel #define INVALID_REG_ADDR	0xff
938c0984e5SSebastian Reichel 
948c0984e5SSebastian Reichel /*
958c0984e5SSebastian Reichel  * bq27xxx_reg_index - Register names
968c0984e5SSebastian Reichel  *
978c0984e5SSebastian Reichel  * These are indexes into a device's register mapping array.
988c0984e5SSebastian Reichel  */
998c0984e5SSebastian Reichel 
1008c0984e5SSebastian Reichel enum bq27xxx_reg_index {
1018c0984e5SSebastian Reichel 	BQ27XXX_REG_CTRL = 0,	/* Control */
1028c0984e5SSebastian Reichel 	BQ27XXX_REG_TEMP,	/* Temperature */
1038c0984e5SSebastian Reichel 	BQ27XXX_REG_INT_TEMP,	/* Internal Temperature */
1048c0984e5SSebastian Reichel 	BQ27XXX_REG_VOLT,	/* Voltage */
1058c0984e5SSebastian Reichel 	BQ27XXX_REG_AI,		/* Average Current */
1068c0984e5SSebastian Reichel 	BQ27XXX_REG_FLAGS,	/* Flags */
1078c0984e5SSebastian Reichel 	BQ27XXX_REG_TTE,	/* Time-to-Empty */
1088c0984e5SSebastian Reichel 	BQ27XXX_REG_TTF,	/* Time-to-Full */
1098c0984e5SSebastian Reichel 	BQ27XXX_REG_TTES,	/* Time-to-Empty Standby */
1108c0984e5SSebastian Reichel 	BQ27XXX_REG_TTECP,	/* Time-to-Empty at Constant Power */
1118c0984e5SSebastian Reichel 	BQ27XXX_REG_NAC,	/* Nominal Available Capacity */
1128c0984e5SSebastian Reichel 	BQ27XXX_REG_FCC,	/* Full Charge Capacity */
1138c0984e5SSebastian Reichel 	BQ27XXX_REG_CYCT,	/* Cycle Count */
1148c0984e5SSebastian Reichel 	BQ27XXX_REG_AE,		/* Available Energy */
1158c0984e5SSebastian Reichel 	BQ27XXX_REG_SOC,	/* State-of-Charge */
1168c0984e5SSebastian Reichel 	BQ27XXX_REG_DCAP,	/* Design Capacity */
1178c0984e5SSebastian Reichel 	BQ27XXX_REG_AP,		/* Average Power */
1180670c9b3SLiam Breck 	BQ27XXX_DM_CTRL,	/* Block Data Control */
1190670c9b3SLiam Breck 	BQ27XXX_DM_CLASS,	/* Data Class */
1200670c9b3SLiam Breck 	BQ27XXX_DM_BLOCK,	/* Data Block */
1210670c9b3SLiam Breck 	BQ27XXX_DM_DATA,	/* Block Data */
1220670c9b3SLiam Breck 	BQ27XXX_DM_CKSUM,	/* Block Data Checksum */
1238c0984e5SSebastian Reichel 	BQ27XXX_REG_MAX,	/* sentinel */
1248c0984e5SSebastian Reichel };
1258c0984e5SSebastian Reichel 
1260670c9b3SLiam Breck #define BQ27XXX_DM_REG_ROWS \
1270670c9b3SLiam Breck 	[BQ27XXX_DM_CTRL] = 0x61,  \
1280670c9b3SLiam Breck 	[BQ27XXX_DM_CLASS] = 0x3e, \
1290670c9b3SLiam Breck 	[BQ27XXX_DM_BLOCK] = 0x3f, \
1300670c9b3SLiam Breck 	[BQ27XXX_DM_DATA] = 0x40,  \
1310670c9b3SLiam Breck 	[BQ27XXX_DM_CKSUM] = 0x60
1320670c9b3SLiam Breck 
1338c0984e5SSebastian Reichel /* Register mappings */
1349aade6d8SLiam Breck static u8
1359aade6d8SLiam Breck 	bq27000_regs[BQ27XXX_REG_MAX] = {
1368c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1378c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1388c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1398c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1408c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1418c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1428c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1438c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1448c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1458c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1468c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1478c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1488c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1498c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = 0x22,
1508c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1518c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1528c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
1530670c9b3SLiam Breck 		[BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
1540670c9b3SLiam Breck 		[BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
1550670c9b3SLiam Breck 		[BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
1560670c9b3SLiam Breck 		[BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
1570670c9b3SLiam Breck 		[BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
1588c0984e5SSebastian Reichel 	},
1599aade6d8SLiam Breck 	bq27010_regs[BQ27XXX_REG_MAX] = {
1608c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1618c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1628c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1638c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1648c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1658c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1668c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1678c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1688c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1698c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1708c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1718c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1728c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1738c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1748c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1758c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1768c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1770670c9b3SLiam Breck 		[BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
1780670c9b3SLiam Breck 		[BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
1790670c9b3SLiam Breck 		[BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
1800670c9b3SLiam Breck 		[BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
1810670c9b3SLiam Breck 		[BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
1828c0984e5SSebastian Reichel 	},
1839aade6d8SLiam Breck 	bq2750x_regs[BQ27XXX_REG_MAX] = {
1848c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1858c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1868c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
1878c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1888c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1898c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1908c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1918c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1928c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1a,
1938c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1948c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1958c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1968c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1978c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1988c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
1998c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
2008c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
2010670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
2028c0984e5SSebastian Reichel 	},
2031059361fSLiam Breck #define bq2751x_regs bq27510g3_regs
2041059361fSLiam Breck #define bq2752x_regs bq27510g3_regs
2059aade6d8SLiam Breck 	bq27500_regs[BQ27XXX_REG_MAX] = {
20632833635SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
20732833635SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
20832833635SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
20932833635SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
21032833635SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
21132833635SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
21232833635SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
21332833635SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
21432833635SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
21532833635SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
21632833635SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
21732833635SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
21832833635SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
21932833635SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
22032833635SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
22132833635SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
22232833635SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
2230670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
22432833635SChris Lapa 	},
2251059361fSLiam Breck #define bq27510g1_regs bq27500_regs
2261059361fSLiam Breck #define bq27510g2_regs bq27500_regs
2279aade6d8SLiam Breck 	bq27510g3_regs[BQ27XXX_REG_MAX] = {
22871375aa7SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
22971375aa7SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
23071375aa7SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = 0x28,
23171375aa7SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
23271375aa7SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
23371375aa7SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
23471375aa7SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
23571375aa7SChris Lapa 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
23671375aa7SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1a,
23771375aa7SChris Lapa 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
23871375aa7SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
23971375aa7SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
24071375aa7SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x1e,
24171375aa7SChris Lapa 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
24271375aa7SChris Lapa 		[BQ27XXX_REG_SOC] = 0x20,
24371375aa7SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x2e,
24471375aa7SChris Lapa 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
2450670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
24671375aa7SChris Lapa 	},
2479aade6d8SLiam Breck 	bq27520g1_regs[BQ27XXX_REG_MAX] = {
24868f2a813SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
24968f2a813SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
25068f2a813SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
25168f2a813SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
25268f2a813SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
25368f2a813SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
25468f2a813SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
25568f2a813SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
25668f2a813SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
25768f2a813SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
25868f2a813SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
25968f2a813SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
26068f2a813SChris Lapa 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
26168f2a813SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
26268f2a813SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
26368f2a813SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
26468f2a813SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
2650670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
26668f2a813SChris Lapa 	},
2679aade6d8SLiam Breck 	bq27520g2_regs[BQ27XXX_REG_MAX] = {
268a5deb9a9SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
269a5deb9a9SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
270a5deb9a9SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = 0x36,
271a5deb9a9SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
272a5deb9a9SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
273a5deb9a9SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
274a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
275a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
276a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
277a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
278a5deb9a9SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
279a5deb9a9SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
280a5deb9a9SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
281a5deb9a9SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
282a5deb9a9SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
283a5deb9a9SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
284a5deb9a9SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
2850670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
286a5deb9a9SChris Lapa 	},
2879aade6d8SLiam Breck 	bq27520g3_regs[BQ27XXX_REG_MAX] = {
288825e915bSChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
289825e915bSChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
290825e915bSChris Lapa 		[BQ27XXX_REG_INT_TEMP] = 0x36,
291825e915bSChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
292825e915bSChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
293825e915bSChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
294825e915bSChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
295825e915bSChris Lapa 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
296825e915bSChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
297825e915bSChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
298825e915bSChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
299825e915bSChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
300825e915bSChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
301825e915bSChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
302825e915bSChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
303825e915bSChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
304825e915bSChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
3050670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
306825e915bSChris Lapa 	},
3079aade6d8SLiam Breck 	bq27520g4_regs[BQ27XXX_REG_MAX] = {
3088835cae5SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
3098835cae5SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
3108835cae5SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = 0x28,
3118835cae5SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
3128835cae5SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
3138835cae5SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
3148835cae5SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
3158835cae5SChris Lapa 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3168835cae5SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
3178835cae5SChris Lapa 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3188835cae5SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
3198835cae5SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
3208835cae5SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x1e,
3218835cae5SChris Lapa 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3228835cae5SChris Lapa 		[BQ27XXX_REG_SOC] = 0x20,
3238835cae5SChris Lapa 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
3248835cae5SChris Lapa 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
3250670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
3268835cae5SChris Lapa 	},
32770a39e10SPavel Machek 	bq27521_regs[BQ27XXX_REG_MAX] = {
32870a39e10SPavel Machek 		[BQ27XXX_REG_CTRL] = 0x02,
32970a39e10SPavel Machek 		[BQ27XXX_REG_TEMP] = 0x0a,
33070a39e10SPavel Machek 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
33170a39e10SPavel Machek 		[BQ27XXX_REG_VOLT] = 0x0c,
33270a39e10SPavel Machek 		[BQ27XXX_REG_AI] = 0x0e,
33370a39e10SPavel Machek 		[BQ27XXX_REG_FLAGS] = 0x08,
33470a39e10SPavel Machek 		[BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
33570a39e10SPavel Machek 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
33670a39e10SPavel Machek 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
33770a39e10SPavel Machek 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
33870a39e10SPavel Machek 		[BQ27XXX_REG_NAC] = INVALID_REG_ADDR,
33970a39e10SPavel Machek 		[BQ27XXX_REG_FCC] = INVALID_REG_ADDR,
34070a39e10SPavel Machek 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
34170a39e10SPavel Machek 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
34270a39e10SPavel Machek 		[BQ27XXX_REG_SOC] = INVALID_REG_ADDR,
34370a39e10SPavel Machek 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
34470a39e10SPavel Machek 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
34570a39e10SPavel Machek 		[BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
34670a39e10SPavel Machek 		[BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
34770a39e10SPavel Machek 		[BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
34870a39e10SPavel Machek 		[BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
34970a39e10SPavel Machek 		[BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
35070a39e10SPavel Machek 	},
3519aade6d8SLiam Breck 	bq27530_regs[BQ27XXX_REG_MAX] = {
3528c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3538c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
3548c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x32,
3558c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
3568c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
3578c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
3588c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
3598c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3608c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3618c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3628c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
3638c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
3648c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
3658c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3668c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
3678c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
3688c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
3690670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
3708c0984e5SSebastian Reichel 	},
3713a731c64SLiam Breck #define bq27531_regs bq27530_regs
3729aade6d8SLiam Breck 	bq27541_regs[BQ27XXX_REG_MAX] = {
3738c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3748c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
3758c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
3768c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
3778c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
3788c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
3798c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
3808c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3818c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3828c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3838c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
3848c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
3858c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
3868c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3878c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
3888c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
3898c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
3900670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
3918c0984e5SSebastian Reichel 	},
3923a731c64SLiam Breck #define bq27542_regs bq27541_regs
3933a731c64SLiam Breck #define bq27546_regs bq27541_regs
3943a731c64SLiam Breck #define bq27742_regs bq27541_regs
3959aade6d8SLiam Breck 	bq27545_regs[BQ27XXX_REG_MAX] = {
3968c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3978c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
3988c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
3998c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
4008c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
4018c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
4028c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
4038c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
4048c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
4058c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
4068c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
4078c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
4088c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
4098c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
4108c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
4118c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
4128c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
4130670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
4148c0984e5SSebastian Reichel 	},
4159aade6d8SLiam Breck 	bq27421_regs[BQ27XXX_REG_MAX] = {
4168c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
4178c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x02,
4188c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x1e,
4198c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x04,
4208c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x10,
4218c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x06,
4228c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
4238c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
4248c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
4258c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
4268c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x08,
4278c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x0e,
4288c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
4298c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
4308c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x1c,
4318c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
4328c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x18,
4330670c9b3SLiam Breck 		BQ27XXX_DM_REG_ROWS,
4346f24ff97SDan Murphy 	},
435457b42f0SLiu Xiang #define bq27411_regs bq27421_regs
4363a731c64SLiam Breck #define bq27425_regs bq27421_regs
4375ef6a160SAndrew F. Davis #define bq27426_regs bq27421_regs
4383a731c64SLiam Breck #define bq27441_regs bq27421_regs
4393a731c64SLiam Breck #define bq27621_regs bq27421_regs
4406f24ff97SDan Murphy 	bq27z561_regs[BQ27XXX_REG_MAX] = {
4416f24ff97SDan Murphy 		[BQ27XXX_REG_CTRL] = 0x00,
4426f24ff97SDan Murphy 		[BQ27XXX_REG_TEMP] = 0x06,
4436f24ff97SDan Murphy 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
4446f24ff97SDan Murphy 		[BQ27XXX_REG_VOLT] = 0x08,
4456f24ff97SDan Murphy 		[BQ27XXX_REG_AI] = 0x14,
4466f24ff97SDan Murphy 		[BQ27XXX_REG_FLAGS] = 0x0a,
4476f24ff97SDan Murphy 		[BQ27XXX_REG_TTE] = 0x16,
4486f24ff97SDan Murphy 		[BQ27XXX_REG_TTF] = 0x18,
4496f24ff97SDan Murphy 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
4506f24ff97SDan Murphy 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
4516f24ff97SDan Murphy 		[BQ27XXX_REG_NAC] = INVALID_REG_ADDR,
4526f24ff97SDan Murphy 		[BQ27XXX_REG_FCC] = 0x12,
4536f24ff97SDan Murphy 		[BQ27XXX_REG_CYCT] = 0x2a,
4546f24ff97SDan Murphy 		[BQ27XXX_REG_AE] = 0x22,
4556f24ff97SDan Murphy 		[BQ27XXX_REG_SOC] = 0x2c,
4566f24ff97SDan Murphy 		[BQ27XXX_REG_DCAP] = 0x3c,
4576f24ff97SDan Murphy 		[BQ27XXX_REG_AP] = 0x22,
4586f24ff97SDan Murphy 		BQ27XXX_DM_REG_ROWS,
459707d678aSDan Murphy 	},
460707d678aSDan Murphy 	bq28z610_regs[BQ27XXX_REG_MAX] = {
461707d678aSDan Murphy 		[BQ27XXX_REG_CTRL] = 0x00,
462707d678aSDan Murphy 		[BQ27XXX_REG_TEMP] = 0x06,
463707d678aSDan Murphy 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
464707d678aSDan Murphy 		[BQ27XXX_REG_VOLT] = 0x08,
465707d678aSDan Murphy 		[BQ27XXX_REG_AI] = 0x14,
466707d678aSDan Murphy 		[BQ27XXX_REG_FLAGS] = 0x0a,
467707d678aSDan Murphy 		[BQ27XXX_REG_TTE] = 0x16,
468707d678aSDan Murphy 		[BQ27XXX_REG_TTF] = 0x18,
469707d678aSDan Murphy 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
470707d678aSDan Murphy 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
471707d678aSDan Murphy 		[BQ27XXX_REG_NAC] = INVALID_REG_ADDR,
472707d678aSDan Murphy 		[BQ27XXX_REG_FCC] = 0x12,
473707d678aSDan Murphy 		[BQ27XXX_REG_CYCT] = 0x2a,
474707d678aSDan Murphy 		[BQ27XXX_REG_AE] = 0x22,
475707d678aSDan Murphy 		[BQ27XXX_REG_SOC] = 0x2c,
476707d678aSDan Murphy 		[BQ27XXX_REG_DCAP] = 0x3c,
477707d678aSDan Murphy 		[BQ27XXX_REG_AP] = 0x22,
478707d678aSDan Murphy 		BQ27XXX_DM_REG_ROWS,
4796f24ff97SDan Murphy 	};
4808c0984e5SSebastian Reichel 
4819aade6d8SLiam Breck static enum power_supply_property bq27000_props[] = {
4828c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
4838c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
4848c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
4858c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
4868c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
4878c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
4888c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
4898c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
4918c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
4928c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
4938c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
4948c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
4958c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
4968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
4978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ENERGY_NOW,
4988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
4998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
5008c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5018c0984e5SSebastian Reichel };
5028c0984e5SSebastian Reichel 
5039aade6d8SLiam Breck static enum power_supply_property bq27010_props[] = {
5048c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
5058c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
5068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
5078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
5088c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
5098c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
5108c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
5118c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
5128c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
5138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
5148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
5158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
5168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
5178c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
5188c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
5198c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
5208c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5218c0984e5SSebastian Reichel };
5228c0984e5SSebastian Reichel 
5231059361fSLiam Breck #define bq2750x_props bq27510g3_props
5241059361fSLiam Breck #define bq2751x_props bq27510g3_props
5251059361fSLiam Breck #define bq2752x_props bq27510g3_props
5263bee9ea1SAndrew F. Davis 
5279aade6d8SLiam Breck static enum power_supply_property bq27500_props[] = {
52832833635SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
52932833635SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
53032833635SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
53132833635SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
53232833635SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
53332833635SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
53432833635SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
53532833635SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
53632833635SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
53732833635SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
53832833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
53932833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
54032833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
54132833635SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
54232833635SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
54332833635SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
54432833635SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
54532833635SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
54632833635SChris Lapa };
5471059361fSLiam Breck #define bq27510g1_props bq27500_props
5481059361fSLiam Breck #define bq27510g2_props bq27500_props
549698a2bf5SChris Lapa 
5509aade6d8SLiam Breck static enum power_supply_property bq27510g3_props[] = {
55171375aa7SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
55271375aa7SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
55371375aa7SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
55471375aa7SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
55571375aa7SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
55671375aa7SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
55771375aa7SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
55871375aa7SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
55971375aa7SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
56071375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
56171375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
56271375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
56371375aa7SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
56471375aa7SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
56571375aa7SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
56671375aa7SChris Lapa };
56771375aa7SChris Lapa 
5689aade6d8SLiam Breck static enum power_supply_property bq27520g1_props[] = {
56968f2a813SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
57068f2a813SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
57168f2a813SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
57268f2a813SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
57368f2a813SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
57468f2a813SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
57568f2a813SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
57668f2a813SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
57768f2a813SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
57868f2a813SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
57968f2a813SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
58068f2a813SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
58168f2a813SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
58268f2a813SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
58368f2a813SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
58468f2a813SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
58568f2a813SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
58668f2a813SChris Lapa };
58768f2a813SChris Lapa 
5881059361fSLiam Breck #define bq27520g2_props bq27500_props
589a5deb9a9SChris Lapa 
5909aade6d8SLiam Breck static enum power_supply_property bq27520g3_props[] = {
591825e915bSChris Lapa 	POWER_SUPPLY_PROP_STATUS,
592825e915bSChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
593825e915bSChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
594825e915bSChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
595825e915bSChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
596825e915bSChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
597825e915bSChris Lapa 	POWER_SUPPLY_PROP_TEMP,
598825e915bSChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
599825e915bSChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
600825e915bSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
601825e915bSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
602825e915bSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
603825e915bSChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
604825e915bSChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
605825e915bSChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
606825e915bSChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
607825e915bSChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
608825e915bSChris Lapa };
609825e915bSChris Lapa 
6109aade6d8SLiam Breck static enum power_supply_property bq27520g4_props[] = {
6118835cae5SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
6128835cae5SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
6138835cae5SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6148835cae5SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
6158835cae5SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
6168835cae5SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
6178835cae5SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
6188835cae5SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6198835cae5SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
6208835cae5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
6218835cae5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
6228835cae5SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
6238835cae5SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
6248835cae5SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
6258835cae5SChris Lapa };
6268835cae5SChris Lapa 
62770a39e10SPavel Machek static enum power_supply_property bq27521_props[] = {
62870a39e10SPavel Machek 	POWER_SUPPLY_PROP_STATUS,
62970a39e10SPavel Machek 	POWER_SUPPLY_PROP_PRESENT,
63070a39e10SPavel Machek 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
63170a39e10SPavel Machek 	POWER_SUPPLY_PROP_CURRENT_NOW,
63270a39e10SPavel Machek 	POWER_SUPPLY_PROP_TEMP,
63370a39e10SPavel Machek 	POWER_SUPPLY_PROP_TECHNOLOGY,
63470a39e10SPavel Machek };
63570a39e10SPavel Machek 
6369aade6d8SLiam Breck static enum power_supply_property bq27530_props[] = {
6378c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6388c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
6398c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6408c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
6418c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
6428c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
6438c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
6448c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6458c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
6468c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
6478c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
6488c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
6498c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
6508c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
6518c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
6528c0984e5SSebastian Reichel };
6533a731c64SLiam Breck #define bq27531_props bq27530_props
6548c0984e5SSebastian Reichel 
6559aade6d8SLiam Breck static enum power_supply_property bq27541_props[] = {
6568c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6578c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
6588c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6598c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
6608c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
6618c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
6628c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
6638c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6648c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
6658c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
6668c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
6678c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
6688c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
6698c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
6708c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
6718c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
6728c0984e5SSebastian Reichel };
6733a731c64SLiam Breck #define bq27542_props bq27541_props
6743a731c64SLiam Breck #define bq27546_props bq27541_props
6753a731c64SLiam Breck #define bq27742_props bq27541_props
6768c0984e5SSebastian Reichel 
6779aade6d8SLiam Breck static enum power_supply_property bq27545_props[] = {
6788c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6798c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
6808c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6818c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
6828c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
6838c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
6848c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
6858c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6868c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
6878c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
6888c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
6898c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
6908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
6918c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
6928c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
6938c0984e5SSebastian Reichel };
6948c0984e5SSebastian Reichel 
6959aade6d8SLiam Breck static enum power_supply_property bq27421_props[] = {
6968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
6988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
7008c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
7018c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
7028c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
7038c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
7048c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
7058c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
7068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
7078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
7088c0984e5SSebastian Reichel };
709457b42f0SLiu Xiang #define bq27411_props bq27421_props
7103a731c64SLiam Breck #define bq27425_props bq27421_props
7115ef6a160SAndrew F. Davis #define bq27426_props bq27421_props
7123a731c64SLiam Breck #define bq27441_props bq27421_props
7133a731c64SLiam Breck #define bq27621_props bq27421_props
7148c0984e5SSebastian Reichel 
7156f24ff97SDan Murphy static enum power_supply_property bq27z561_props[] = {
7166f24ff97SDan Murphy 	POWER_SUPPLY_PROP_STATUS,
7176f24ff97SDan Murphy 	POWER_SUPPLY_PROP_PRESENT,
7186f24ff97SDan Murphy 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
7196f24ff97SDan Murphy 	POWER_SUPPLY_PROP_CURRENT_NOW,
7206f24ff97SDan Murphy 	POWER_SUPPLY_PROP_CAPACITY,
7216f24ff97SDan Murphy 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
7226f24ff97SDan Murphy 	POWER_SUPPLY_PROP_TEMP,
7236f24ff97SDan Murphy 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
7246f24ff97SDan Murphy 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
7256f24ff97SDan Murphy 	POWER_SUPPLY_PROP_TECHNOLOGY,
7266f24ff97SDan Murphy 	POWER_SUPPLY_PROP_CHARGE_FULL,
7276f24ff97SDan Murphy 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
7286f24ff97SDan Murphy 	POWER_SUPPLY_PROP_CYCLE_COUNT,
7296f24ff97SDan Murphy 	POWER_SUPPLY_PROP_POWER_AVG,
7306f24ff97SDan Murphy 	POWER_SUPPLY_PROP_HEALTH,
7316f24ff97SDan Murphy 	POWER_SUPPLY_PROP_MANUFACTURER,
7326f24ff97SDan Murphy };
7336f24ff97SDan Murphy 
734707d678aSDan Murphy static enum power_supply_property bq28z610_props[] = {
735707d678aSDan Murphy 	POWER_SUPPLY_PROP_STATUS,
736707d678aSDan Murphy 	POWER_SUPPLY_PROP_PRESENT,
737707d678aSDan Murphy 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
738707d678aSDan Murphy 	POWER_SUPPLY_PROP_CURRENT_NOW,
739707d678aSDan Murphy 	POWER_SUPPLY_PROP_CAPACITY,
740707d678aSDan Murphy 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
741707d678aSDan Murphy 	POWER_SUPPLY_PROP_TEMP,
742707d678aSDan Murphy 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
743707d678aSDan Murphy 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
744707d678aSDan Murphy 	POWER_SUPPLY_PROP_TECHNOLOGY,
745707d678aSDan Murphy 	POWER_SUPPLY_PROP_CHARGE_FULL,
746707d678aSDan Murphy 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
747707d678aSDan Murphy 	POWER_SUPPLY_PROP_CYCLE_COUNT,
748707d678aSDan Murphy 	POWER_SUPPLY_PROP_POWER_AVG,
749707d678aSDan Murphy 	POWER_SUPPLY_PROP_HEALTH,
750707d678aSDan Murphy 	POWER_SUPPLY_PROP_MANUFACTURER,
751707d678aSDan Murphy };
752707d678aSDan Murphy 
75305045379SLiam Breck struct bq27xxx_dm_reg {
75405045379SLiam Breck 	u8 subclass_id;
75505045379SLiam Breck 	u8 offset;
75605045379SLiam Breck 	u8 bytes;
75705045379SLiam Breck 	u16 min, max;
75805045379SLiam Breck };
75905045379SLiam Breck 
76005045379SLiam Breck enum bq27xxx_dm_reg_id {
76105045379SLiam Breck 	BQ27XXX_DM_DESIGN_CAPACITY = 0,
76205045379SLiam Breck 	BQ27XXX_DM_DESIGN_ENERGY,
76305045379SLiam Breck 	BQ27XXX_DM_TERMINATE_VOLTAGE,
76405045379SLiam Breck };
76505045379SLiam Breck 
76605045379SLiam Breck #define bq27000_dm_regs 0
76705045379SLiam Breck #define bq27010_dm_regs 0
76805045379SLiam Breck #define bq2750x_dm_regs 0
76905045379SLiam Breck #define bq2751x_dm_regs 0
77005045379SLiam Breck #define bq2752x_dm_regs 0
77105045379SLiam Breck 
77205045379SLiam Breck #if 0 /* not yet tested */
77305045379SLiam Breck static struct bq27xxx_dm_reg bq27500_dm_regs[] = {
77405045379SLiam Breck 	[BQ27XXX_DM_DESIGN_CAPACITY]   = { 48, 10, 2,    0, 65535 },
77505045379SLiam Breck 	[BQ27XXX_DM_DESIGN_ENERGY]     = { }, /* missing on chip */
77605045379SLiam Breck 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 48, 2, 1000, 32767 },
77705045379SLiam Breck };
77805045379SLiam Breck #else
77905045379SLiam Breck #define bq27500_dm_regs 0
78005045379SLiam Breck #endif
78105045379SLiam Breck 
78205045379SLiam Breck /* todo create data memory definitions from datasheets and test on chips */
78305045379SLiam Breck #define bq27510g1_dm_regs 0
78405045379SLiam Breck #define bq27510g2_dm_regs 0
78505045379SLiam Breck #define bq27510g3_dm_regs 0
78605045379SLiam Breck #define bq27520g1_dm_regs 0
78705045379SLiam Breck #define bq27520g2_dm_regs 0
78805045379SLiam Breck #define bq27520g3_dm_regs 0
78905045379SLiam Breck #define bq27520g4_dm_regs 0
79070a39e10SPavel Machek #define bq27521_dm_regs 0
79105045379SLiam Breck #define bq27530_dm_regs 0
79205045379SLiam Breck #define bq27531_dm_regs 0
79305045379SLiam Breck #define bq27541_dm_regs 0
79405045379SLiam Breck #define bq27542_dm_regs 0
79505045379SLiam Breck #define bq27546_dm_regs 0
79605045379SLiam Breck #define bq27742_dm_regs 0
79705045379SLiam Breck 
79805045379SLiam Breck #if 0 /* not yet tested */
79905045379SLiam Breck static struct bq27xxx_dm_reg bq27545_dm_regs[] = {
80005045379SLiam Breck 	[BQ27XXX_DM_DESIGN_CAPACITY]   = { 48, 23, 2,    0, 32767 },
80105045379SLiam Breck 	[BQ27XXX_DM_DESIGN_ENERGY]     = { 48, 25, 2,    0, 32767 },
80205045379SLiam Breck 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 67, 2, 2800,  3700 },
80305045379SLiam Breck };
80405045379SLiam Breck #else
80505045379SLiam Breck #define bq27545_dm_regs 0
80605045379SLiam Breck #endif
80705045379SLiam Breck 
808457b42f0SLiu Xiang static struct bq27xxx_dm_reg bq27411_dm_regs[] = {
809457b42f0SLiu Xiang 	[BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 10, 2,    0, 32767 },
810457b42f0SLiu Xiang 	[BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 12, 2,    0, 32767 },
811457b42f0SLiu Xiang 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 16, 2, 2800,  3700 },
812457b42f0SLiu Xiang };
813457b42f0SLiu Xiang 
81405045379SLiam Breck static struct bq27xxx_dm_reg bq27421_dm_regs[] = {
81505045379SLiam Breck 	[BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 10, 2,    0,  8000 },
81605045379SLiam Breck 	[BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 12, 2,    0, 32767 },
81705045379SLiam Breck 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 16, 2, 2500,  3700 },
81805045379SLiam Breck };
81905045379SLiam Breck 
82005045379SLiam Breck static struct bq27xxx_dm_reg bq27425_dm_regs[] = {
82105045379SLiam Breck 	[BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 12, 2,    0, 32767 },
82205045379SLiam Breck 	[BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 14, 2,    0, 32767 },
82305045379SLiam Breck 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 18, 2, 2800,  3700 },
82405045379SLiam Breck };
82505045379SLiam Breck 
8265ef6a160SAndrew F. Davis static struct bq27xxx_dm_reg bq27426_dm_regs[] = {
8275ef6a160SAndrew F. Davis 	[BQ27XXX_DM_DESIGN_CAPACITY]   = { 82,  6, 2,    0,  8000 },
8285ef6a160SAndrew F. Davis 	[BQ27XXX_DM_DESIGN_ENERGY]     = { 82,  8, 2,    0, 32767 },
8295ef6a160SAndrew F. Davis 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 10, 2, 2500,  3700 },
8305ef6a160SAndrew F. Davis };
8315ef6a160SAndrew F. Davis 
83205045379SLiam Breck #if 0 /* not yet tested */
83305045379SLiam Breck #define bq27441_dm_regs bq27421_dm_regs
83405045379SLiam Breck #else
83505045379SLiam Breck #define bq27441_dm_regs 0
83605045379SLiam Breck #endif
83705045379SLiam Breck 
83805045379SLiam Breck #if 0 /* not yet tested */
83905045379SLiam Breck static struct bq27xxx_dm_reg bq27621_dm_regs[] = {
84005045379SLiam Breck 	[BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 3, 2,    0,  8000 },
84105045379SLiam Breck 	[BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 5, 2,    0, 32767 },
84205045379SLiam Breck 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 9, 2, 2500,  3700 },
84305045379SLiam Breck };
84405045379SLiam Breck #else
84505045379SLiam Breck #define bq27621_dm_regs 0
84605045379SLiam Breck #endif
84705045379SLiam Breck 
8486f24ff97SDan Murphy #define bq27z561_dm_regs 0
849707d678aSDan Murphy #define bq28z610_dm_regs 0
8506f24ff97SDan Murphy 
8513a731c64SLiam Breck #define BQ27XXX_O_ZERO	0x00000001
85270a39e10SPavel Machek #define BQ27XXX_O_OTDC	0x00000002 /* has OTC/OTD overtemperature flags */
85370a39e10SPavel Machek #define BQ27XXX_O_UTOT  0x00000004 /* has OT overtemperature flag */
85405045379SLiam Breck #define BQ27XXX_O_CFGUP	0x00000008
85505045379SLiam Breck #define BQ27XXX_O_RAM	0x00000010
8566f24ff97SDan Murphy #define BQ27Z561_O_BITS	0x00000020
8573a731c64SLiam Breck 
85805045379SLiam Breck #define BQ27XXX_DATA(ref, key, opt) {		\
8593a731c64SLiam Breck 	.opts = (opt),				\
86005045379SLiam Breck 	.unseal_key = key,			\
8619aade6d8SLiam Breck 	.regs  = ref##_regs,			\
86205045379SLiam Breck 	.dm_regs = ref##_dm_regs,		\
8639aade6d8SLiam Breck 	.props = ref##_props,			\
8649aade6d8SLiam Breck 	.props_size = ARRAY_SIZE(ref##_props) }
8658c0984e5SSebastian Reichel 
8668c0984e5SSebastian Reichel static struct {
8673a731c64SLiam Breck 	u32 opts;
86805045379SLiam Breck 	u32 unseal_key;
8699aade6d8SLiam Breck 	u8 *regs;
87005045379SLiam Breck 	struct bq27xxx_dm_reg *dm_regs;
8718c0984e5SSebastian Reichel 	enum power_supply_property *props;
8729aade6d8SLiam Breck 	size_t props_size;
8739aade6d8SLiam Breck } bq27xxx_chip_data[] = {
87405045379SLiam Breck 	[BQ27000]   = BQ27XXX_DATA(bq27000,   0         , BQ27XXX_O_ZERO),
87505045379SLiam Breck 	[BQ27010]   = BQ27XXX_DATA(bq27010,   0         , BQ27XXX_O_ZERO),
87605045379SLiam Breck 	[BQ2750X]   = BQ27XXX_DATA(bq2750x,   0         , BQ27XXX_O_OTDC),
87705045379SLiam Breck 	[BQ2751X]   = BQ27XXX_DATA(bq2751x,   0         , BQ27XXX_O_OTDC),
87805045379SLiam Breck 	[BQ2752X]   = BQ27XXX_DATA(bq2752x,   0         , BQ27XXX_O_OTDC),
87905045379SLiam Breck 	[BQ27500]   = BQ27XXX_DATA(bq27500,   0x04143672, BQ27XXX_O_OTDC),
88005045379SLiam Breck 	[BQ27510G1] = BQ27XXX_DATA(bq27510g1, 0         , BQ27XXX_O_OTDC),
88105045379SLiam Breck 	[BQ27510G2] = BQ27XXX_DATA(bq27510g2, 0         , BQ27XXX_O_OTDC),
88205045379SLiam Breck 	[BQ27510G3] = BQ27XXX_DATA(bq27510g3, 0         , BQ27XXX_O_OTDC),
88305045379SLiam Breck 	[BQ27520G1] = BQ27XXX_DATA(bq27520g1, 0         , BQ27XXX_O_OTDC),
88405045379SLiam Breck 	[BQ27520G2] = BQ27XXX_DATA(bq27520g2, 0         , BQ27XXX_O_OTDC),
88505045379SLiam Breck 	[BQ27520G3] = BQ27XXX_DATA(bq27520g3, 0         , BQ27XXX_O_OTDC),
88605045379SLiam Breck 	[BQ27520G4] = BQ27XXX_DATA(bq27520g4, 0         , BQ27XXX_O_OTDC),
88770a39e10SPavel Machek 	[BQ27521]   = BQ27XXX_DATA(bq27521,   0         , 0),
88805045379SLiam Breck 	[BQ27530]   = BQ27XXX_DATA(bq27530,   0         , BQ27XXX_O_UTOT),
88905045379SLiam Breck 	[BQ27531]   = BQ27XXX_DATA(bq27531,   0         , BQ27XXX_O_UTOT),
89005045379SLiam Breck 	[BQ27541]   = BQ27XXX_DATA(bq27541,   0         , BQ27XXX_O_OTDC),
89105045379SLiam Breck 	[BQ27542]   = BQ27XXX_DATA(bq27542,   0         , BQ27XXX_O_OTDC),
89205045379SLiam Breck 	[BQ27546]   = BQ27XXX_DATA(bq27546,   0         , BQ27XXX_O_OTDC),
89305045379SLiam Breck 	[BQ27742]   = BQ27XXX_DATA(bq27742,   0         , BQ27XXX_O_OTDC),
89405045379SLiam Breck 	[BQ27545]   = BQ27XXX_DATA(bq27545,   0x04143672, BQ27XXX_O_OTDC),
895457b42f0SLiu Xiang 	[BQ27411]   = BQ27XXX_DATA(bq27411,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
89605045379SLiam Breck 	[BQ27421]   = BQ27XXX_DATA(bq27421,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
89705045379SLiam Breck 	[BQ27425]   = BQ27XXX_DATA(bq27425,   0x04143672, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP),
8985ef6a160SAndrew F. Davis 	[BQ27426]   = BQ27XXX_DATA(bq27426,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
89905045379SLiam Breck 	[BQ27441]   = BQ27XXX_DATA(bq27441,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
90005045379SLiam Breck 	[BQ27621]   = BQ27XXX_DATA(bq27621,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
9016f24ff97SDan Murphy 	[BQ27Z561]  = BQ27XXX_DATA(bq27z561,  0         , BQ27Z561_O_BITS),
902707d678aSDan Murphy 	[BQ28Z610]  = BQ27XXX_DATA(bq28z610,  0         , BQ27Z561_O_BITS),
9038c0984e5SSebastian Reichel };
9048c0984e5SSebastian Reichel 
9051d72706fSMatt Ranostay static DEFINE_MUTEX(bq27xxx_list_lock);
9061d72706fSMatt Ranostay static LIST_HEAD(bq27xxx_battery_devices);
9071d72706fSMatt Ranostay 
9080670c9b3SLiam Breck #define BQ27XXX_MSLEEP(i) usleep_range((i)*1000, (i)*1000+500)
9090670c9b3SLiam Breck 
9100670c9b3SLiam Breck #define BQ27XXX_DM_SZ	32
9110670c9b3SLiam Breck 
9120670c9b3SLiam Breck /**
9130670c9b3SLiam Breck  * struct bq27xxx_dm_buf - chip data memory buffer
9140670c9b3SLiam Breck  * @class: data memory subclass_id
9150670c9b3SLiam Breck  * @block: data memory block number
9160670c9b3SLiam Breck  * @data: data from/for the block
9170670c9b3SLiam Breck  * @has_data: true if data has been filled by read
9180670c9b3SLiam Breck  * @dirty: true if data has changed since last read/write
9190670c9b3SLiam Breck  *
9200670c9b3SLiam Breck  * Encapsulates info required to manage chip data memory blocks.
9210670c9b3SLiam Breck  */
9220670c9b3SLiam Breck struct bq27xxx_dm_buf {
9230670c9b3SLiam Breck 	u8 class;
9240670c9b3SLiam Breck 	u8 block;
9250670c9b3SLiam Breck 	u8 data[BQ27XXX_DM_SZ];
9260670c9b3SLiam Breck 	bool has_data, dirty;
9270670c9b3SLiam Breck };
9280670c9b3SLiam Breck 
929ccce4409SLiam Breck #define BQ27XXX_DM_BUF(di, i) { \
930ccce4409SLiam Breck 	.class = (di)->dm_regs[i].subclass_id, \
931ccce4409SLiam Breck 	.block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
932ccce4409SLiam Breck }
933ccce4409SLiam Breck 
934ccce4409SLiam Breck static inline u16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf,
935ccce4409SLiam Breck 				      struct bq27xxx_dm_reg *reg)
936ccce4409SLiam Breck {
937ccce4409SLiam Breck 	if (buf->class == reg->subclass_id &&
938ccce4409SLiam Breck 	    buf->block == reg->offset / BQ27XXX_DM_SZ)
939ccce4409SLiam Breck 		return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ);
940ccce4409SLiam Breck 
941ccce4409SLiam Breck 	return NULL;
942ccce4409SLiam Breck }
943ccce4409SLiam Breck 
944ccce4409SLiam Breck static const char * const bq27xxx_dm_reg_name[] = {
945ccce4409SLiam Breck 	[BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
946ccce4409SLiam Breck 	[BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
947ccce4409SLiam Breck 	[BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
948ccce4409SLiam Breck };
949ccce4409SLiam Breck 
950ccce4409SLiam Breck 
951ccce4409SLiam Breck static bool bq27xxx_dt_to_nvm = true;
952ccce4409SLiam Breck module_param_named(dt_monitored_battery_updates_nvm, bq27xxx_dt_to_nvm, bool, 0444);
953ccce4409SLiam Breck MODULE_PARM_DESC(dt_monitored_battery_updates_nvm,
954ccce4409SLiam Breck 	"Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
955ccce4409SLiam Breck 	"Users must set this =0 when installing a different type of battery!\n"
956ccce4409SLiam Breck 	"Default is =1."
957ccce4409SLiam Breck #ifndef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
958ccce4409SLiam Breck 	"\nSetting this affects future kernel updates, not the current configuration."
959ccce4409SLiam Breck #endif
960ccce4409SLiam Breck );
9610670c9b3SLiam Breck 
9621d72706fSMatt Ranostay static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
9631d72706fSMatt Ranostay {
9641d72706fSMatt Ranostay 	struct bq27xxx_device_info *di;
965950b6c2dSMatt Ranostay 	unsigned int prev_val = *(unsigned int *) kp->arg;
9661d72706fSMatt Ranostay 	int ret;
9671d72706fSMatt Ranostay 
9681d72706fSMatt Ranostay 	ret = param_set_uint(val, kp);
969950b6c2dSMatt Ranostay 	if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
9701d72706fSMatt Ranostay 		return ret;
9711d72706fSMatt Ranostay 
9721d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
9731d72706fSMatt Ranostay 	list_for_each_entry(di, &bq27xxx_battery_devices, list) {
9741d72706fSMatt Ranostay 		cancel_delayed_work_sync(&di->work);
9751d72706fSMatt Ranostay 		schedule_delayed_work(&di->work, 0);
9761d72706fSMatt Ranostay 	}
9771d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
9781d72706fSMatt Ranostay 
9791d72706fSMatt Ranostay 	return ret;
9801d72706fSMatt Ranostay }
9811d72706fSMatt Ranostay 
9821d72706fSMatt Ranostay static const struct kernel_param_ops param_ops_poll_interval = {
9831d72706fSMatt Ranostay 	.get = param_get_uint,
9841d72706fSMatt Ranostay 	.set = poll_interval_param_set,
9851d72706fSMatt Ranostay };
9861d72706fSMatt Ranostay 
9878c0984e5SSebastian Reichel static unsigned int poll_interval = 360;
9881d72706fSMatt Ranostay module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
9898c0984e5SSebastian Reichel MODULE_PARM_DESC(poll_interval,
9908c0984e5SSebastian Reichel 		 "battery poll interval in seconds - 0 disables polling");
9918c0984e5SSebastian Reichel 
9928c0984e5SSebastian Reichel /*
9938c0984e5SSebastian Reichel  * Common code for BQ27xxx devices
9948c0984e5SSebastian Reichel  */
9958c0984e5SSebastian Reichel 
9968c0984e5SSebastian Reichel static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
9978c0984e5SSebastian Reichel 			       bool single)
9988c0984e5SSebastian Reichel {
99914073f66SMatt Ranostay 	int ret;
100014073f66SMatt Ranostay 
10018c0984e5SSebastian Reichel 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
10028c0984e5SSebastian Reichel 		return -EINVAL;
10038c0984e5SSebastian Reichel 
100414073f66SMatt Ranostay 	ret = di->bus.read(di, di->regs[reg_index], single);
100514073f66SMatt Ranostay 	if (ret < 0)
100614073f66SMatt Ranostay 		dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n",
100714073f66SMatt Ranostay 			di->regs[reg_index], reg_index);
100814073f66SMatt Ranostay 
100914073f66SMatt Ranostay 	return ret;
101014073f66SMatt Ranostay }
101114073f66SMatt Ranostay 
101214073f66SMatt Ranostay static inline int bq27xxx_write(struct bq27xxx_device_info *di, int reg_index,
101314073f66SMatt Ranostay 				u16 value, bool single)
101414073f66SMatt Ranostay {
101514073f66SMatt Ranostay 	int ret;
101614073f66SMatt Ranostay 
101714073f66SMatt Ranostay 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
101814073f66SMatt Ranostay 		return -EINVAL;
101914073f66SMatt Ranostay 
102014073f66SMatt Ranostay 	if (!di->bus.write)
102114073f66SMatt Ranostay 		return -EPERM;
102214073f66SMatt Ranostay 
102314073f66SMatt Ranostay 	ret = di->bus.write(di, di->regs[reg_index], value, single);
102414073f66SMatt Ranostay 	if (ret < 0)
102514073f66SMatt Ranostay 		dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n",
102614073f66SMatt Ranostay 			di->regs[reg_index], reg_index);
102714073f66SMatt Ranostay 
102814073f66SMatt Ranostay 	return ret;
102914073f66SMatt Ranostay }
103014073f66SMatt Ranostay 
103114073f66SMatt Ranostay static inline int bq27xxx_read_block(struct bq27xxx_device_info *di, int reg_index,
103214073f66SMatt Ranostay 				     u8 *data, int len)
103314073f66SMatt Ranostay {
103414073f66SMatt Ranostay 	int ret;
103514073f66SMatt Ranostay 
103614073f66SMatt Ranostay 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
103714073f66SMatt Ranostay 		return -EINVAL;
103814073f66SMatt Ranostay 
103914073f66SMatt Ranostay 	if (!di->bus.read_bulk)
104014073f66SMatt Ranostay 		return -EPERM;
104114073f66SMatt Ranostay 
104214073f66SMatt Ranostay 	ret = di->bus.read_bulk(di, di->regs[reg_index], data, len);
104314073f66SMatt Ranostay 	if (ret < 0)
104414073f66SMatt Ranostay 		dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n",
104514073f66SMatt Ranostay 			di->regs[reg_index], reg_index);
104614073f66SMatt Ranostay 
104714073f66SMatt Ranostay 	return ret;
104814073f66SMatt Ranostay }
104914073f66SMatt Ranostay 
105014073f66SMatt Ranostay static inline int bq27xxx_write_block(struct bq27xxx_device_info *di, int reg_index,
105114073f66SMatt Ranostay 				      u8 *data, int len)
105214073f66SMatt Ranostay {
105314073f66SMatt Ranostay 	int ret;
105414073f66SMatt Ranostay 
105514073f66SMatt Ranostay 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
105614073f66SMatt Ranostay 		return -EINVAL;
105714073f66SMatt Ranostay 
105814073f66SMatt Ranostay 	if (!di->bus.write_bulk)
105914073f66SMatt Ranostay 		return -EPERM;
106014073f66SMatt Ranostay 
106114073f66SMatt Ranostay 	ret = di->bus.write_bulk(di, di->regs[reg_index], data, len);
106214073f66SMatt Ranostay 	if (ret < 0)
106314073f66SMatt Ranostay 		dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n",
106414073f66SMatt Ranostay 			di->regs[reg_index], reg_index);
106514073f66SMatt Ranostay 
106614073f66SMatt Ranostay 	return ret;
10678c0984e5SSebastian Reichel }
10688c0984e5SSebastian Reichel 
10690670c9b3SLiam Breck static int bq27xxx_battery_seal(struct bq27xxx_device_info *di)
10700670c9b3SLiam Breck {
10710670c9b3SLiam Breck 	int ret;
10720670c9b3SLiam Breck 
10730670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_SEALED, false);
10740670c9b3SLiam Breck 	if (ret < 0) {
10750670c9b3SLiam Breck 		dev_err(di->dev, "bus error on seal: %d\n", ret);
10760670c9b3SLiam Breck 		return ret;
10770670c9b3SLiam Breck 	}
10780670c9b3SLiam Breck 
10790670c9b3SLiam Breck 	return 0;
10800670c9b3SLiam Breck }
10810670c9b3SLiam Breck 
10820670c9b3SLiam Breck static int bq27xxx_battery_unseal(struct bq27xxx_device_info *di)
10830670c9b3SLiam Breck {
10840670c9b3SLiam Breck 	int ret;
10850670c9b3SLiam Breck 
10860670c9b3SLiam Breck 	if (di->unseal_key == 0) {
10870670c9b3SLiam Breck 		dev_err(di->dev, "unseal failed due to missing key\n");
10880670c9b3SLiam Breck 		return -EINVAL;
10890670c9b3SLiam Breck 	}
10900670c9b3SLiam Breck 
10910670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false);
10920670c9b3SLiam Breck 	if (ret < 0)
10930670c9b3SLiam Breck 		goto out;
10940670c9b3SLiam Breck 
10950670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false);
10960670c9b3SLiam Breck 	if (ret < 0)
10970670c9b3SLiam Breck 		goto out;
10980670c9b3SLiam Breck 
10990670c9b3SLiam Breck 	return 0;
11000670c9b3SLiam Breck 
11010670c9b3SLiam Breck out:
11020670c9b3SLiam Breck 	dev_err(di->dev, "bus error on unseal: %d\n", ret);
11030670c9b3SLiam Breck 	return ret;
11040670c9b3SLiam Breck }
11050670c9b3SLiam Breck 
11060670c9b3SLiam Breck static u8 bq27xxx_battery_checksum_dm_block(struct bq27xxx_dm_buf *buf)
11070670c9b3SLiam Breck {
11080670c9b3SLiam Breck 	u16 sum = 0;
11090670c9b3SLiam Breck 	int i;
11100670c9b3SLiam Breck 
11110670c9b3SLiam Breck 	for (i = 0; i < BQ27XXX_DM_SZ; i++)
11120670c9b3SLiam Breck 		sum += buf->data[i];
11130670c9b3SLiam Breck 	sum &= 0xff;
11140670c9b3SLiam Breck 
11150670c9b3SLiam Breck 	return 0xff - sum;
11160670c9b3SLiam Breck }
11170670c9b3SLiam Breck 
11180670c9b3SLiam Breck static int bq27xxx_battery_read_dm_block(struct bq27xxx_device_info *di,
11190670c9b3SLiam Breck 					 struct bq27xxx_dm_buf *buf)
11200670c9b3SLiam Breck {
11210670c9b3SLiam Breck 	int ret;
11220670c9b3SLiam Breck 
11230670c9b3SLiam Breck 	buf->has_data = false;
11240670c9b3SLiam Breck 
11250670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
11260670c9b3SLiam Breck 	if (ret < 0)
11270670c9b3SLiam Breck 		goto out;
11280670c9b3SLiam Breck 
11290670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
11300670c9b3SLiam Breck 	if (ret < 0)
11310670c9b3SLiam Breck 		goto out;
11320670c9b3SLiam Breck 
11330670c9b3SLiam Breck 	BQ27XXX_MSLEEP(1);
11340670c9b3SLiam Breck 
11350670c9b3SLiam Breck 	ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
11360670c9b3SLiam Breck 	if (ret < 0)
11370670c9b3SLiam Breck 		goto out;
11380670c9b3SLiam Breck 
11390670c9b3SLiam Breck 	ret = bq27xxx_read(di, BQ27XXX_DM_CKSUM, true);
11400670c9b3SLiam Breck 	if (ret < 0)
11410670c9b3SLiam Breck 		goto out;
11420670c9b3SLiam Breck 
11430670c9b3SLiam Breck 	if ((u8)ret != bq27xxx_battery_checksum_dm_block(buf)) {
11440670c9b3SLiam Breck 		ret = -EINVAL;
11450670c9b3SLiam Breck 		goto out;
11460670c9b3SLiam Breck 	}
11470670c9b3SLiam Breck 
11480670c9b3SLiam Breck 	buf->has_data = true;
11490670c9b3SLiam Breck 	buf->dirty = false;
11500670c9b3SLiam Breck 
11510670c9b3SLiam Breck 	return 0;
11520670c9b3SLiam Breck 
11530670c9b3SLiam Breck out:
11540670c9b3SLiam Breck 	dev_err(di->dev, "bus error reading chip memory: %d\n", ret);
11550670c9b3SLiam Breck 	return ret;
11560670c9b3SLiam Breck }
11570670c9b3SLiam Breck 
1158ccce4409SLiam Breck static void bq27xxx_battery_update_dm_block(struct bq27xxx_device_info *di,
1159ccce4409SLiam Breck 					    struct bq27xxx_dm_buf *buf,
1160ccce4409SLiam Breck 					    enum bq27xxx_dm_reg_id reg_id,
1161ccce4409SLiam Breck 					    unsigned int val)
1162ccce4409SLiam Breck {
1163ccce4409SLiam Breck 	struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
1164ccce4409SLiam Breck 	const char *str = bq27xxx_dm_reg_name[reg_id];
1165ccce4409SLiam Breck 	u16 *prev = bq27xxx_dm_reg_ptr(buf, reg);
1166ccce4409SLiam Breck 
1167ccce4409SLiam Breck 	if (prev == NULL) {
1168ccce4409SLiam Breck 		dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
1169ccce4409SLiam Breck 		return;
1170ccce4409SLiam Breck 	}
1171ccce4409SLiam Breck 
1172ccce4409SLiam Breck 	if (reg->bytes != 2) {
1173ccce4409SLiam Breck 		dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
1174ccce4409SLiam Breck 		return;
1175ccce4409SLiam Breck 	}
1176ccce4409SLiam Breck 
1177ccce4409SLiam Breck 	if (!buf->has_data)
1178ccce4409SLiam Breck 		return;
1179ccce4409SLiam Breck 
1180ccce4409SLiam Breck 	if (be16_to_cpup(prev) == val) {
1181ccce4409SLiam Breck 		dev_info(di->dev, "%s has %u\n", str, val);
1182ccce4409SLiam Breck 		return;
1183ccce4409SLiam Breck 	}
1184ccce4409SLiam Breck 
1185ccce4409SLiam Breck #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
118605045379SLiam Breck 	if (!(di->opts & BQ27XXX_O_RAM) && !bq27xxx_dt_to_nvm) {
1187ccce4409SLiam Breck #else
118805045379SLiam Breck 	if (!(di->opts & BQ27XXX_O_RAM)) {
1189ccce4409SLiam Breck #endif
1190ccce4409SLiam Breck 		/* devicetree and NVM differ; defer to NVM */
1191ccce4409SLiam Breck 		dev_warn(di->dev, "%s has %u; update to %u disallowed "
1192ccce4409SLiam Breck #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
1193ccce4409SLiam Breck 			 "by dt_monitored_battery_updates_nvm=0"
1194ccce4409SLiam Breck #else
1195ccce4409SLiam Breck 			 "for flash/NVM data memory"
1196ccce4409SLiam Breck #endif
1197ccce4409SLiam Breck 			 "\n", str, be16_to_cpup(prev), val);
1198ccce4409SLiam Breck 		return;
1199ccce4409SLiam Breck 	}
1200ccce4409SLiam Breck 
1201ccce4409SLiam Breck 	dev_info(di->dev, "update %s to %u\n", str, val);
1202ccce4409SLiam Breck 
1203ccce4409SLiam Breck 	*prev = cpu_to_be16(val);
1204ccce4409SLiam Breck 	buf->dirty = true;
1205ccce4409SLiam Breck }
1206ccce4409SLiam Breck 
12070670c9b3SLiam Breck static int bq27xxx_battery_cfgupdate_priv(struct bq27xxx_device_info *di, bool active)
12080670c9b3SLiam Breck {
12090670c9b3SLiam Breck 	const int limit = 100;
12100670c9b3SLiam Breck 	u16 cmd = active ? BQ27XXX_SET_CFGUPDATE : BQ27XXX_SOFT_RESET;
12110670c9b3SLiam Breck 	int ret, try = limit;
12120670c9b3SLiam Breck 
12130670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, cmd, false);
12140670c9b3SLiam Breck 	if (ret < 0)
12150670c9b3SLiam Breck 		return ret;
12160670c9b3SLiam Breck 
12170670c9b3SLiam Breck 	do {
12180670c9b3SLiam Breck 		BQ27XXX_MSLEEP(25);
12190670c9b3SLiam Breck 		ret = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
12200670c9b3SLiam Breck 		if (ret < 0)
12210670c9b3SLiam Breck 			return ret;
12220670c9b3SLiam Breck 	} while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try);
12230670c9b3SLiam Breck 
122405045379SLiam Breck 	if (!try && di->chip != BQ27425) { // 425 has a bug
12250670c9b3SLiam Breck 		dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active);
12260670c9b3SLiam Breck 		return -EINVAL;
12270670c9b3SLiam Breck 	}
12280670c9b3SLiam Breck 
12290670c9b3SLiam Breck 	if (limit - try > 3)
12300670c9b3SLiam Breck 		dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try);
12310670c9b3SLiam Breck 
12320670c9b3SLiam Breck 	return 0;
12330670c9b3SLiam Breck }
12340670c9b3SLiam Breck 
12350670c9b3SLiam Breck static inline int bq27xxx_battery_set_cfgupdate(struct bq27xxx_device_info *di)
12360670c9b3SLiam Breck {
12370670c9b3SLiam Breck 	int ret = bq27xxx_battery_cfgupdate_priv(di, true);
12380670c9b3SLiam Breck 	if (ret < 0 && ret != -EINVAL)
12390670c9b3SLiam Breck 		dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret);
12400670c9b3SLiam Breck 
12410670c9b3SLiam Breck 	return ret;
12420670c9b3SLiam Breck }
12430670c9b3SLiam Breck 
12440670c9b3SLiam Breck static inline int bq27xxx_battery_soft_reset(struct bq27xxx_device_info *di)
12450670c9b3SLiam Breck {
12460670c9b3SLiam Breck 	int ret = bq27xxx_battery_cfgupdate_priv(di, false);
12470670c9b3SLiam Breck 	if (ret < 0 && ret != -EINVAL)
12480670c9b3SLiam Breck 		dev_err(di->dev, "bus error on soft_reset: %d\n", ret);
12490670c9b3SLiam Breck 
12500670c9b3SLiam Breck 	return ret;
12510670c9b3SLiam Breck }
12520670c9b3SLiam Breck 
12530670c9b3SLiam Breck static int bq27xxx_battery_write_dm_block(struct bq27xxx_device_info *di,
12540670c9b3SLiam Breck 					  struct bq27xxx_dm_buf *buf)
12550670c9b3SLiam Breck {
125605045379SLiam Breck 	bool cfgup = di->opts & BQ27XXX_O_CFGUP;
12570670c9b3SLiam Breck 	int ret;
12580670c9b3SLiam Breck 
12590670c9b3SLiam Breck 	if (!buf->dirty)
12600670c9b3SLiam Breck 		return 0;
12610670c9b3SLiam Breck 
12620670c9b3SLiam Breck 	if (cfgup) {
12630670c9b3SLiam Breck 		ret = bq27xxx_battery_set_cfgupdate(di);
12640670c9b3SLiam Breck 		if (ret < 0)
12650670c9b3SLiam Breck 			return ret;
12660670c9b3SLiam Breck 	}
12670670c9b3SLiam Breck 
12680670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_DM_CTRL, 0, true);
12690670c9b3SLiam Breck 	if (ret < 0)
12700670c9b3SLiam Breck 		goto out;
12710670c9b3SLiam Breck 
12720670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
12730670c9b3SLiam Breck 	if (ret < 0)
12740670c9b3SLiam Breck 		goto out;
12750670c9b3SLiam Breck 
12760670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
12770670c9b3SLiam Breck 	if (ret < 0)
12780670c9b3SLiam Breck 		goto out;
12790670c9b3SLiam Breck 
12800670c9b3SLiam Breck 	BQ27XXX_MSLEEP(1);
12810670c9b3SLiam Breck 
12820670c9b3SLiam Breck 	ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
12830670c9b3SLiam Breck 	if (ret < 0)
12840670c9b3SLiam Breck 		goto out;
12850670c9b3SLiam Breck 
12860670c9b3SLiam Breck 	ret = bq27xxx_write(di, BQ27XXX_DM_CKSUM,
12870670c9b3SLiam Breck 			    bq27xxx_battery_checksum_dm_block(buf), true);
12880670c9b3SLiam Breck 	if (ret < 0)
12890670c9b3SLiam Breck 		goto out;
12900670c9b3SLiam Breck 
12910670c9b3SLiam Breck 	/* DO NOT read BQ27XXX_DM_CKSUM here to verify it! That may cause NVM
12920670c9b3SLiam Breck 	 * corruption on the '425 chip (and perhaps others), which can damage
12930670c9b3SLiam Breck 	 * the chip.
12940670c9b3SLiam Breck 	 */
12950670c9b3SLiam Breck 
12960670c9b3SLiam Breck 	if (cfgup) {
12970670c9b3SLiam Breck 		BQ27XXX_MSLEEP(1);
12980670c9b3SLiam Breck 		ret = bq27xxx_battery_soft_reset(di);
12990670c9b3SLiam Breck 		if (ret < 0)
13000670c9b3SLiam Breck 			return ret;
13010670c9b3SLiam Breck 	} else {
13020670c9b3SLiam Breck 		BQ27XXX_MSLEEP(100); /* flash DM updates in <100ms */
13030670c9b3SLiam Breck 	}
13040670c9b3SLiam Breck 
13050670c9b3SLiam Breck 	buf->dirty = false;
13060670c9b3SLiam Breck 
13070670c9b3SLiam Breck 	return 0;
13080670c9b3SLiam Breck 
13090670c9b3SLiam Breck out:
13100670c9b3SLiam Breck 	if (cfgup)
13110670c9b3SLiam Breck 		bq27xxx_battery_soft_reset(di);
13120670c9b3SLiam Breck 
13130670c9b3SLiam Breck 	dev_err(di->dev, "bus error writing chip memory: %d\n", ret);
13140670c9b3SLiam Breck 	return ret;
13150670c9b3SLiam Breck }
13160670c9b3SLiam Breck 
1317ccce4409SLiam Breck static void bq27xxx_battery_set_config(struct bq27xxx_device_info *di,
1318ccce4409SLiam Breck 				       struct power_supply_battery_info *info)
1319ccce4409SLiam Breck {
1320ccce4409SLiam Breck 	struct bq27xxx_dm_buf bd = BQ27XXX_DM_BUF(di, BQ27XXX_DM_DESIGN_CAPACITY);
1321ccce4409SLiam Breck 	struct bq27xxx_dm_buf bt = BQ27XXX_DM_BUF(di, BQ27XXX_DM_TERMINATE_VOLTAGE);
1322ccce4409SLiam Breck 	bool updated;
1323ccce4409SLiam Breck 
1324ccce4409SLiam Breck 	if (bq27xxx_battery_unseal(di) < 0)
1325ccce4409SLiam Breck 		return;
1326ccce4409SLiam Breck 
1327ccce4409SLiam Breck 	if (info->charge_full_design_uah != -EINVAL &&
1328ccce4409SLiam Breck 	    info->energy_full_design_uwh != -EINVAL) {
1329ccce4409SLiam Breck 		bq27xxx_battery_read_dm_block(di, &bd);
1330ccce4409SLiam Breck 		/* assume design energy & capacity are in same block */
1331ccce4409SLiam Breck 		bq27xxx_battery_update_dm_block(di, &bd,
1332ccce4409SLiam Breck 					BQ27XXX_DM_DESIGN_CAPACITY,
1333ccce4409SLiam Breck 					info->charge_full_design_uah / 1000);
1334ccce4409SLiam Breck 		bq27xxx_battery_update_dm_block(di, &bd,
1335ccce4409SLiam Breck 					BQ27XXX_DM_DESIGN_ENERGY,
1336ccce4409SLiam Breck 					info->energy_full_design_uwh / 1000);
1337ccce4409SLiam Breck 	}
1338ccce4409SLiam Breck 
1339ccce4409SLiam Breck 	if (info->voltage_min_design_uv != -EINVAL) {
1340ccce4409SLiam Breck 		bool same = bd.class == bt.class && bd.block == bt.block;
1341ccce4409SLiam Breck 		if (!same)
1342ccce4409SLiam Breck 			bq27xxx_battery_read_dm_block(di, &bt);
1343ccce4409SLiam Breck 		bq27xxx_battery_update_dm_block(di, same ? &bd : &bt,
1344ccce4409SLiam Breck 					BQ27XXX_DM_TERMINATE_VOLTAGE,
1345ccce4409SLiam Breck 					info->voltage_min_design_uv / 1000);
1346ccce4409SLiam Breck 	}
1347ccce4409SLiam Breck 
1348ccce4409SLiam Breck 	updated = bd.dirty || bt.dirty;
1349ccce4409SLiam Breck 
1350ccce4409SLiam Breck 	bq27xxx_battery_write_dm_block(di, &bd);
1351ccce4409SLiam Breck 	bq27xxx_battery_write_dm_block(di, &bt);
1352ccce4409SLiam Breck 
1353ccce4409SLiam Breck 	bq27xxx_battery_seal(di);
1354ccce4409SLiam Breck 
135505045379SLiam Breck 	if (updated && !(di->opts & BQ27XXX_O_CFGUP)) {
1356ccce4409SLiam Breck 		bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_RESET, false);
1357ccce4409SLiam Breck 		BQ27XXX_MSLEEP(300); /* reset time is not documented */
1358ccce4409SLiam Breck 	}
1359ccce4409SLiam Breck 	/* assume bq27xxx_battery_update() is called hereafter */
1360ccce4409SLiam Breck }
1361ccce4409SLiam Breck 
1362ccce4409SLiam Breck static void bq27xxx_battery_settings(struct bq27xxx_device_info *di)
1363ccce4409SLiam Breck {
1364ccce4409SLiam Breck 	struct power_supply_battery_info info = {};
1365ccce4409SLiam Breck 	unsigned int min, max;
1366ccce4409SLiam Breck 
1367ccce4409SLiam Breck 	if (power_supply_get_battery_info(di->bat, &info) < 0)
1368ccce4409SLiam Breck 		return;
1369ccce4409SLiam Breck 
1370ccce4409SLiam Breck 	if (!di->dm_regs) {
1371ccce4409SLiam Breck 		dev_warn(di->dev, "data memory update not supported for chip\n");
1372ccce4409SLiam Breck 		return;
1373ccce4409SLiam Breck 	}
1374ccce4409SLiam Breck 
1375ccce4409SLiam Breck 	if (info.energy_full_design_uwh != info.charge_full_design_uah) {
1376ccce4409SLiam Breck 		if (info.energy_full_design_uwh == -EINVAL)
1377ccce4409SLiam Breck 			dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
1378ccce4409SLiam Breck 		else if (info.charge_full_design_uah == -EINVAL)
1379ccce4409SLiam Breck 			dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
1380ccce4409SLiam Breck 	}
1381ccce4409SLiam Breck 
1382ccce4409SLiam Breck 	/* assume min == 0 */
1383ccce4409SLiam Breck 	max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
1384ccce4409SLiam Breck 	if (info.energy_full_design_uwh > max * 1000) {
1385ccce4409SLiam Breck 		dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
1386ccce4409SLiam Breck 			info.energy_full_design_uwh);
1387ccce4409SLiam Breck 		info.energy_full_design_uwh = -EINVAL;
1388ccce4409SLiam Breck 	}
1389ccce4409SLiam Breck 
1390ccce4409SLiam Breck 	/* assume min == 0 */
1391ccce4409SLiam Breck 	max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
1392ccce4409SLiam Breck 	if (info.charge_full_design_uah > max * 1000) {
1393ccce4409SLiam Breck 		dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
1394ccce4409SLiam Breck 			info.charge_full_design_uah);
1395ccce4409SLiam Breck 		info.charge_full_design_uah = -EINVAL;
1396ccce4409SLiam Breck 	}
1397ccce4409SLiam Breck 
1398ccce4409SLiam Breck 	min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
1399ccce4409SLiam Breck 	max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
1400ccce4409SLiam Breck 	if ((info.voltage_min_design_uv < min * 1000 ||
1401ccce4409SLiam Breck 	     info.voltage_min_design_uv > max * 1000) &&
1402ccce4409SLiam Breck 	     info.voltage_min_design_uv != -EINVAL) {
1403ccce4409SLiam Breck 		dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
1404ccce4409SLiam Breck 			info.voltage_min_design_uv);
1405ccce4409SLiam Breck 		info.voltage_min_design_uv = -EINVAL;
1406ccce4409SLiam Breck 	}
1407ccce4409SLiam Breck 
1408ccce4409SLiam Breck 	if ((info.energy_full_design_uwh != -EINVAL &&
1409ccce4409SLiam Breck 	     info.charge_full_design_uah != -EINVAL) ||
1410ccce4409SLiam Breck 	     info.voltage_min_design_uv  != -EINVAL)
1411ccce4409SLiam Breck 		bq27xxx_battery_set_config(di, &info);
1412ccce4409SLiam Breck }
1413ccce4409SLiam Breck 
14148c0984e5SSebastian Reichel /*
14158c0984e5SSebastian Reichel  * Return the battery State-of-Charge
14168c0984e5SSebastian Reichel  * Or < 0 if something fails.
14178c0984e5SSebastian Reichel  */
14188c0984e5SSebastian Reichel static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
14198c0984e5SSebastian Reichel {
14208c0984e5SSebastian Reichel 	int soc;
14218c0984e5SSebastian Reichel 
14223a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
14238c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
14248c0984e5SSebastian Reichel 	else
14258c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
14268c0984e5SSebastian Reichel 
14278c0984e5SSebastian Reichel 	if (soc < 0)
14288c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading State-of-Charge\n");
14298c0984e5SSebastian Reichel 
14308c0984e5SSebastian Reichel 	return soc;
14318c0984e5SSebastian Reichel }
14328c0984e5SSebastian Reichel 
14338c0984e5SSebastian Reichel /*
14348c0984e5SSebastian Reichel  * Return a battery charge value in µAh
14358c0984e5SSebastian Reichel  * Or < 0 if something fails.
14368c0984e5SSebastian Reichel  */
14378c0984e5SSebastian Reichel static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
14388c0984e5SSebastian Reichel {
14398c0984e5SSebastian Reichel 	int charge;
14408c0984e5SSebastian Reichel 
14418c0984e5SSebastian Reichel 	charge = bq27xxx_read(di, reg, false);
14428c0984e5SSebastian Reichel 	if (charge < 0) {
14438c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
14448c0984e5SSebastian Reichel 			reg, charge);
14458c0984e5SSebastian Reichel 		return charge;
14468c0984e5SSebastian Reichel 	}
14478c0984e5SSebastian Reichel 
14483a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
14498c0984e5SSebastian Reichel 		charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
14508c0984e5SSebastian Reichel 	else
14518c0984e5SSebastian Reichel 		charge *= 1000;
14528c0984e5SSebastian Reichel 
14538c0984e5SSebastian Reichel 	return charge;
14548c0984e5SSebastian Reichel }
14558c0984e5SSebastian Reichel 
14568c0984e5SSebastian Reichel /*
14578c0984e5SSebastian Reichel  * Return the battery Nominal available capacity in µAh
14588c0984e5SSebastian Reichel  * Or < 0 if something fails.
14598c0984e5SSebastian Reichel  */
14608c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
14618c0984e5SSebastian Reichel {
14628c0984e5SSebastian Reichel 	int flags;
14638c0984e5SSebastian Reichel 
14643a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO) {
14658c0984e5SSebastian Reichel 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
14668c0984e5SSebastian Reichel 		if (flags >= 0 && (flags & BQ27000_FLAG_CI))
14678c0984e5SSebastian Reichel 			return -ENODATA;
14688c0984e5SSebastian Reichel 	}
14698c0984e5SSebastian Reichel 
14708c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
14718c0984e5SSebastian Reichel }
14728c0984e5SSebastian Reichel 
14738c0984e5SSebastian Reichel /*
14748c0984e5SSebastian Reichel  * Return the battery Full Charge Capacity in µAh
14758c0984e5SSebastian Reichel  * Or < 0 if something fails.
14768c0984e5SSebastian Reichel  */
14778c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
14788c0984e5SSebastian Reichel {
14798c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
14808c0984e5SSebastian Reichel }
14818c0984e5SSebastian Reichel 
14828c0984e5SSebastian Reichel /*
14838c0984e5SSebastian Reichel  * Return the Design Capacity in µAh
14848c0984e5SSebastian Reichel  * Or < 0 if something fails.
14858c0984e5SSebastian Reichel  */
14868c0984e5SSebastian Reichel static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
14878c0984e5SSebastian Reichel {
14888c0984e5SSebastian Reichel 	int dcap;
14898c0984e5SSebastian Reichel 
14903a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
14918c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
14928c0984e5SSebastian Reichel 	else
14938c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
14948c0984e5SSebastian Reichel 
14958c0984e5SSebastian Reichel 	if (dcap < 0) {
14968c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading initial last measured discharge\n");
14978c0984e5SSebastian Reichel 		return dcap;
14988c0984e5SSebastian Reichel 	}
14998c0984e5SSebastian Reichel 
15003a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
15018c0984e5SSebastian Reichel 		dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
15028c0984e5SSebastian Reichel 	else
15038c0984e5SSebastian Reichel 		dcap *= 1000;
15048c0984e5SSebastian Reichel 
15058c0984e5SSebastian Reichel 	return dcap;
15068c0984e5SSebastian Reichel }
15078c0984e5SSebastian Reichel 
15088c0984e5SSebastian Reichel /*
15098c0984e5SSebastian Reichel  * Return the battery Available energy in µWh
15108c0984e5SSebastian Reichel  * Or < 0 if something fails.
15118c0984e5SSebastian Reichel  */
15128c0984e5SSebastian Reichel static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
15138c0984e5SSebastian Reichel {
15148c0984e5SSebastian Reichel 	int ae;
15158c0984e5SSebastian Reichel 
15168c0984e5SSebastian Reichel 	ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
15178c0984e5SSebastian Reichel 	if (ae < 0) {
15188c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading available energy\n");
15198c0984e5SSebastian Reichel 		return ae;
15208c0984e5SSebastian Reichel 	}
15218c0984e5SSebastian Reichel 
15223a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
15238c0984e5SSebastian Reichel 		ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
15248c0984e5SSebastian Reichel 	else
15258c0984e5SSebastian Reichel 		ae *= 1000;
15268c0984e5SSebastian Reichel 
15278c0984e5SSebastian Reichel 	return ae;
15288c0984e5SSebastian Reichel }
15298c0984e5SSebastian Reichel 
15308c0984e5SSebastian Reichel /*
15318c0984e5SSebastian Reichel  * Return the battery temperature in tenths of degree Kelvin
15328c0984e5SSebastian Reichel  * Or < 0 if something fails.
15338c0984e5SSebastian Reichel  */
15348c0984e5SSebastian Reichel static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
15358c0984e5SSebastian Reichel {
15368c0984e5SSebastian Reichel 	int temp;
15378c0984e5SSebastian Reichel 
15388c0984e5SSebastian Reichel 	temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
15398c0984e5SSebastian Reichel 	if (temp < 0) {
15408c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading temperature\n");
15418c0984e5SSebastian Reichel 		return temp;
15428c0984e5SSebastian Reichel 	}
15438c0984e5SSebastian Reichel 
15443a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
15458c0984e5SSebastian Reichel 		temp = 5 * temp / 2;
15468c0984e5SSebastian Reichel 
15478c0984e5SSebastian Reichel 	return temp;
15488c0984e5SSebastian Reichel }
15498c0984e5SSebastian Reichel 
15508c0984e5SSebastian Reichel /*
15518c0984e5SSebastian Reichel  * Return the battery Cycle count total
15528c0984e5SSebastian Reichel  * Or < 0 if something fails.
15538c0984e5SSebastian Reichel  */
15548c0984e5SSebastian Reichel static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
15558c0984e5SSebastian Reichel {
15568c0984e5SSebastian Reichel 	int cyct;
15578c0984e5SSebastian Reichel 
15588c0984e5SSebastian Reichel 	cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
15598c0984e5SSebastian Reichel 	if (cyct < 0)
15608c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading cycle count total\n");
15618c0984e5SSebastian Reichel 
15628c0984e5SSebastian Reichel 	return cyct;
15638c0984e5SSebastian Reichel }
15648c0984e5SSebastian Reichel 
15658c0984e5SSebastian Reichel /*
15668c0984e5SSebastian Reichel  * Read a time register.
15678c0984e5SSebastian Reichel  * Return < 0 if something fails.
15688c0984e5SSebastian Reichel  */
15698c0984e5SSebastian Reichel static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
15708c0984e5SSebastian Reichel {
15718c0984e5SSebastian Reichel 	int tval;
15728c0984e5SSebastian Reichel 
15738c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, reg, false);
15748c0984e5SSebastian Reichel 	if (tval < 0) {
15758c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading time register %02x: %d\n",
15768c0984e5SSebastian Reichel 			reg, tval);
15778c0984e5SSebastian Reichel 		return tval;
15788c0984e5SSebastian Reichel 	}
15798c0984e5SSebastian Reichel 
15808c0984e5SSebastian Reichel 	if (tval == 65535)
15818c0984e5SSebastian Reichel 		return -ENODATA;
15828c0984e5SSebastian Reichel 
15838c0984e5SSebastian Reichel 	return tval * 60;
15848c0984e5SSebastian Reichel }
15858c0984e5SSebastian Reichel 
15868c0984e5SSebastian Reichel /*
15878c0984e5SSebastian Reichel  * Read an average power register.
15888c0984e5SSebastian Reichel  * Return < 0 if something fails.
15898c0984e5SSebastian Reichel  */
15908c0984e5SSebastian Reichel static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
15918c0984e5SSebastian Reichel {
15928c0984e5SSebastian Reichel 	int tval;
15938c0984e5SSebastian Reichel 
15948c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
15958c0984e5SSebastian Reichel 	if (tval < 0) {
15968c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading average power register  %02x: %d\n",
15978c0984e5SSebastian Reichel 			BQ27XXX_REG_AP, tval);
15988c0984e5SSebastian Reichel 		return tval;
15998c0984e5SSebastian Reichel 	}
16008c0984e5SSebastian Reichel 
16013a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
16028c0984e5SSebastian Reichel 		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
16038c0984e5SSebastian Reichel 	else
16048c0984e5SSebastian Reichel 		return tval;
16058c0984e5SSebastian Reichel }
16068c0984e5SSebastian Reichel 
16078c0984e5SSebastian Reichel /*
16088c0984e5SSebastian Reichel  * Returns true if a battery over temperature condition is detected
16098c0984e5SSebastian Reichel  */
16108c0984e5SSebastian Reichel static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
16118c0984e5SSebastian Reichel {
16123a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_OTDC)
16138c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
16143a731c64SLiam Breck         if (di->opts & BQ27XXX_O_UTOT)
16158c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_OT;
16163a731c64SLiam Breck 
16178c0984e5SSebastian Reichel 	return false;
16188c0984e5SSebastian Reichel }
16198c0984e5SSebastian Reichel 
16208c0984e5SSebastian Reichel /*
16218c0984e5SSebastian Reichel  * Returns true if a battery under temperature condition is detected
16228c0984e5SSebastian Reichel  */
16238c0984e5SSebastian Reichel static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
16248c0984e5SSebastian Reichel {
16253a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_UTOT)
16268c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_UT;
16278c0984e5SSebastian Reichel 
16288c0984e5SSebastian Reichel 	return false;
16298c0984e5SSebastian Reichel }
16308c0984e5SSebastian Reichel 
16318c0984e5SSebastian Reichel /*
16328c0984e5SSebastian Reichel  * Returns true if a low state of charge condition is detected
16338c0984e5SSebastian Reichel  */
16348c0984e5SSebastian Reichel static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
16358c0984e5SSebastian Reichel {
16363a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO)
16378c0984e5SSebastian Reichel 		return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
16386f24ff97SDan Murphy 	else if (di->opts & BQ27Z561_O_BITS)
16396f24ff97SDan Murphy 		return flags & BQ27Z561_FLAG_FDC;
16408c0984e5SSebastian Reichel 	else
16418c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
16428c0984e5SSebastian Reichel }
16438c0984e5SSebastian Reichel 
16448c0984e5SSebastian Reichel static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
16458c0984e5SSebastian Reichel {
16468c0984e5SSebastian Reichel 	/* Unlikely but important to return first */
16479b2c945fSArthur Demchenkov 	if (unlikely(bq27xxx_battery_overtemp(di, di->cache.flags)))
16488c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_OVERHEAT;
16499b2c945fSArthur Demchenkov 	if (unlikely(bq27xxx_battery_undertemp(di, di->cache.flags)))
16508c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_COLD;
16519b2c945fSArthur Demchenkov 	if (unlikely(bq27xxx_battery_dead(di, di->cache.flags)))
16528c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_DEAD;
16538c0984e5SSebastian Reichel 
16548c0984e5SSebastian Reichel 	return POWER_SUPPLY_HEALTH_GOOD;
16558c0984e5SSebastian Reichel }
16568c0984e5SSebastian Reichel 
16578c0984e5SSebastian Reichel void bq27xxx_battery_update(struct bq27xxx_device_info *di)
16588c0984e5SSebastian Reichel {
16598c0984e5SSebastian Reichel 	struct bq27xxx_reg_cache cache = {0, };
16603a731c64SLiam Breck 	bool has_ci_flag = di->opts & BQ27XXX_O_ZERO;
16613a731c64SLiam Breck 	bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
16628c0984e5SSebastian Reichel 
16638c0984e5SSebastian Reichel 	cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
16648c0984e5SSebastian Reichel 	if ((cache.flags & 0xff) == 0xff)
16658c0984e5SSebastian Reichel 		cache.flags = -1; /* read error */
16668c0984e5SSebastian Reichel 	if (cache.flags >= 0) {
16678c0984e5SSebastian Reichel 		cache.temperature = bq27xxx_battery_read_temperature(di);
16688c0984e5SSebastian Reichel 		if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
16698c0984e5SSebastian Reichel 			dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
16708c0984e5SSebastian Reichel 			cache.capacity = -ENODATA;
16718c0984e5SSebastian Reichel 			cache.energy = -ENODATA;
16728c0984e5SSebastian Reichel 			cache.time_to_empty = -ENODATA;
16738c0984e5SSebastian Reichel 			cache.time_to_empty_avg = -ENODATA;
16748c0984e5SSebastian Reichel 			cache.time_to_full = -ENODATA;
16758c0984e5SSebastian Reichel 			cache.charge_full = -ENODATA;
16768c0984e5SSebastian Reichel 			cache.health = -ENODATA;
16778c0984e5SSebastian Reichel 		} else {
16788c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
16798c0984e5SSebastian Reichel 				cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
16808c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
16818c0984e5SSebastian Reichel 				cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
16828c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
16838c0984e5SSebastian Reichel 				cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
16846f24ff97SDan Murphy 
16858c0984e5SSebastian Reichel 			cache.charge_full = bq27xxx_battery_read_fcc(di);
16868c0984e5SSebastian Reichel 			cache.capacity = bq27xxx_battery_read_soc(di);
16878c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
16888c0984e5SSebastian Reichel 				cache.energy = bq27xxx_battery_read_energy(di);
16899b2c945fSArthur Demchenkov 			di->cache.flags = cache.flags;
16908c0984e5SSebastian Reichel 			cache.health = bq27xxx_battery_read_health(di);
16918c0984e5SSebastian Reichel 		}
16928c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
16938c0984e5SSebastian Reichel 			cache.cycle_count = bq27xxx_battery_read_cyct(di);
16948c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
16958c0984e5SSebastian Reichel 			cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
16968c0984e5SSebastian Reichel 
16978c0984e5SSebastian Reichel 		/* We only have to read charge design full once */
16988c0984e5SSebastian Reichel 		if (di->charge_design_full <= 0)
16998c0984e5SSebastian Reichel 			di->charge_design_full = bq27xxx_battery_read_dcap(di);
17008c0984e5SSebastian Reichel 	}
17018c0984e5SSebastian Reichel 
1702243f8ffcSKrzysztof Kozlowski 	if ((di->cache.capacity != cache.capacity) ||
1703243f8ffcSKrzysztof Kozlowski 	    (di->cache.flags != cache.flags))
17048c0984e5SSebastian Reichel 		power_supply_changed(di->bat);
17058c0984e5SSebastian Reichel 
17068c0984e5SSebastian Reichel 	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
17078c0984e5SSebastian Reichel 		di->cache = cache;
17088c0984e5SSebastian Reichel 
17098c0984e5SSebastian Reichel 	di->last_update = jiffies;
17108c0984e5SSebastian Reichel }
17118c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
17128c0984e5SSebastian Reichel 
17138c0984e5SSebastian Reichel static void bq27xxx_battery_poll(struct work_struct *work)
17148c0984e5SSebastian Reichel {
17158c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di =
17168c0984e5SSebastian Reichel 			container_of(work, struct bq27xxx_device_info,
17178c0984e5SSebastian Reichel 				     work.work);
17188c0984e5SSebastian Reichel 
17198c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
17208c0984e5SSebastian Reichel 
17218c0984e5SSebastian Reichel 	if (poll_interval > 0)
17228c0984e5SSebastian Reichel 		schedule_delayed_work(&di->work, poll_interval * HZ);
17238c0984e5SSebastian Reichel }
17248c0984e5SSebastian Reichel 
17258c0984e5SSebastian Reichel /*
17268c0984e5SSebastian Reichel  * Return the battery average current in µA
17278c0984e5SSebastian Reichel  * Note that current can be negative signed as well
17288c0984e5SSebastian Reichel  * Or 0 if something fails.
17298c0984e5SSebastian Reichel  */
17308c0984e5SSebastian Reichel static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
17318c0984e5SSebastian Reichel 				   union power_supply_propval *val)
17328c0984e5SSebastian Reichel {
17338c0984e5SSebastian Reichel 	int curr;
17348c0984e5SSebastian Reichel 	int flags;
17358c0984e5SSebastian Reichel 
17368c0984e5SSebastian Reichel 	curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
17378c0984e5SSebastian Reichel 	if (curr < 0) {
17388c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading current\n");
17398c0984e5SSebastian Reichel 		return curr;
17408c0984e5SSebastian Reichel 	}
17418c0984e5SSebastian Reichel 
17423a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO) {
1743e4a404a0SH. Nikolaus Schaller 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
17448c0984e5SSebastian Reichel 		if (flags & BQ27000_FLAG_CHGS) {
17458c0984e5SSebastian Reichel 			dev_dbg(di->dev, "negative current!\n");
17468c0984e5SSebastian Reichel 			curr = -curr;
17478c0984e5SSebastian Reichel 		}
17488c0984e5SSebastian Reichel 
17498c0984e5SSebastian Reichel 		val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
17508c0984e5SSebastian Reichel 	} else {
17518c0984e5SSebastian Reichel 		/* Other gauges return signed value */
17528c0984e5SSebastian Reichel 		val->intval = (int)((s16)curr) * 1000;
17538c0984e5SSebastian Reichel 	}
17548c0984e5SSebastian Reichel 
17558c0984e5SSebastian Reichel 	return 0;
17568c0984e5SSebastian Reichel }
17578c0984e5SSebastian Reichel 
17588c0984e5SSebastian Reichel static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
17598c0984e5SSebastian Reichel 				  union power_supply_propval *val)
17608c0984e5SSebastian Reichel {
17618c0984e5SSebastian Reichel 	int status;
17628c0984e5SSebastian Reichel 
17633a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO) {
17648c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
17658c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
17668c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_CHGS)
17678c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
17688c0984e5SSebastian Reichel 		else
17698c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
17706f24ff97SDan Murphy 	} else if (di->opts & BQ27Z561_O_BITS) {
17716f24ff97SDan Murphy 		if (di->cache.flags & BQ27Z561_FLAG_FC)
17726f24ff97SDan Murphy 			status = POWER_SUPPLY_STATUS_FULL;
17736f24ff97SDan Murphy 		else if (di->cache.flags & BQ27Z561_FLAG_DIS_CH)
17746f24ff97SDan Murphy 			status = POWER_SUPPLY_STATUS_DISCHARGING;
17756f24ff97SDan Murphy 		else
17766f24ff97SDan Murphy 			status = POWER_SUPPLY_STATUS_CHARGING;
17778c0984e5SSebastian Reichel 	} else {
17788c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
17798c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
17808c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_DSC)
17818c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
17828c0984e5SSebastian Reichel 		else
17838c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
17848c0984e5SSebastian Reichel 	}
17858c0984e5SSebastian Reichel 
1786*7bf738baSKrzysztof Kozlowski 	if ((status == POWER_SUPPLY_STATUS_DISCHARGING) &&
1787*7bf738baSKrzysztof Kozlowski 	    (power_supply_am_i_supplied(di->bat) > 0))
1788*7bf738baSKrzysztof Kozlowski 		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1789*7bf738baSKrzysztof Kozlowski 
17908c0984e5SSebastian Reichel 	val->intval = status;
17918c0984e5SSebastian Reichel 
17928c0984e5SSebastian Reichel 	return 0;
17938c0984e5SSebastian Reichel }
17948c0984e5SSebastian Reichel 
17958c0984e5SSebastian Reichel static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
17968c0984e5SSebastian Reichel 					  union power_supply_propval *val)
17978c0984e5SSebastian Reichel {
17988c0984e5SSebastian Reichel 	int level;
17998c0984e5SSebastian Reichel 
18003a731c64SLiam Breck 	if (di->opts & BQ27XXX_O_ZERO) {
18018c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
18028c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
18038c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDV1)
18048c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
18058c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDVF)
18068c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
18078c0984e5SSebastian Reichel 		else
18088c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
18096f24ff97SDan Murphy 	} else if (di->opts & BQ27Z561_O_BITS) {
18106f24ff97SDan Murphy 		if (di->cache.flags & BQ27Z561_FLAG_FC)
18116f24ff97SDan Murphy 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
18126f24ff97SDan Murphy 		else if (di->cache.flags & BQ27Z561_FLAG_FDC)
18136f24ff97SDan Murphy 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
18146f24ff97SDan Murphy 		else
18156f24ff97SDan Murphy 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
18168c0984e5SSebastian Reichel 	} else {
18178c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
18188c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
18198c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
18208c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
18218c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
18228c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
18238c0984e5SSebastian Reichel 		else
18248c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
18258c0984e5SSebastian Reichel 	}
18268c0984e5SSebastian Reichel 
18278c0984e5SSebastian Reichel 	val->intval = level;
18288c0984e5SSebastian Reichel 
18298c0984e5SSebastian Reichel 	return 0;
18308c0984e5SSebastian Reichel }
18318c0984e5SSebastian Reichel 
18328c0984e5SSebastian Reichel /*
18338c0984e5SSebastian Reichel  * Return the battery Voltage in millivolts
18348c0984e5SSebastian Reichel  * Or < 0 if something fails.
18358c0984e5SSebastian Reichel  */
18368c0984e5SSebastian Reichel static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
18378c0984e5SSebastian Reichel 				   union power_supply_propval *val)
18388c0984e5SSebastian Reichel {
18398c0984e5SSebastian Reichel 	int volt;
18408c0984e5SSebastian Reichel 
18418c0984e5SSebastian Reichel 	volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
18428c0984e5SSebastian Reichel 	if (volt < 0) {
18438c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading voltage\n");
18448c0984e5SSebastian Reichel 		return volt;
18458c0984e5SSebastian Reichel 	}
18468c0984e5SSebastian Reichel 
18478c0984e5SSebastian Reichel 	val->intval = volt * 1000;
18488c0984e5SSebastian Reichel 
18498c0984e5SSebastian Reichel 	return 0;
18508c0984e5SSebastian Reichel }
18518c0984e5SSebastian Reichel 
18528c0984e5SSebastian Reichel static int bq27xxx_simple_value(int value,
18538c0984e5SSebastian Reichel 				union power_supply_propval *val)
18548c0984e5SSebastian Reichel {
18558c0984e5SSebastian Reichel 	if (value < 0)
18568c0984e5SSebastian Reichel 		return value;
18578c0984e5SSebastian Reichel 
18588c0984e5SSebastian Reichel 	val->intval = value;
18598c0984e5SSebastian Reichel 
18608c0984e5SSebastian Reichel 	return 0;
18618c0984e5SSebastian Reichel }
18628c0984e5SSebastian Reichel 
18638c0984e5SSebastian Reichel static int bq27xxx_battery_get_property(struct power_supply *psy,
18648c0984e5SSebastian Reichel 					enum power_supply_property psp,
18658c0984e5SSebastian Reichel 					union power_supply_propval *val)
18668c0984e5SSebastian Reichel {
18678c0984e5SSebastian Reichel 	int ret = 0;
18688c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
18698c0984e5SSebastian Reichel 
18708c0984e5SSebastian Reichel 	mutex_lock(&di->lock);
18718c0984e5SSebastian Reichel 	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
18728c0984e5SSebastian Reichel 		cancel_delayed_work_sync(&di->work);
18738c0984e5SSebastian Reichel 		bq27xxx_battery_poll(&di->work.work);
18748c0984e5SSebastian Reichel 	}
18758c0984e5SSebastian Reichel 	mutex_unlock(&di->lock);
18768c0984e5SSebastian Reichel 
18778c0984e5SSebastian Reichel 	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
18788c0984e5SSebastian Reichel 		return -ENODEV;
18798c0984e5SSebastian Reichel 
18808c0984e5SSebastian Reichel 	switch (psp) {
18818c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
18828c0984e5SSebastian Reichel 		ret = bq27xxx_battery_status(di, val);
18838c0984e5SSebastian Reichel 		break;
18848c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
18858c0984e5SSebastian Reichel 		ret = bq27xxx_battery_voltage(di, val);
18868c0984e5SSebastian Reichel 		break;
18878c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
18888c0984e5SSebastian Reichel 		val->intval = di->cache.flags < 0 ? 0 : 1;
18898c0984e5SSebastian Reichel 		break;
18908c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
18918c0984e5SSebastian Reichel 		ret = bq27xxx_battery_current(di, val);
18928c0984e5SSebastian Reichel 		break;
18938c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
18948c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.capacity, val);
18958c0984e5SSebastian Reichel 		break;
18968c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
18978c0984e5SSebastian Reichel 		ret = bq27xxx_battery_capacity_level(di, val);
18988c0984e5SSebastian Reichel 		break;
18998c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
19008c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.temperature, val);
19018c0984e5SSebastian Reichel 		if (ret == 0)
19028c0984e5SSebastian Reichel 			val->intval -= 2731; /* convert decidegree k to c */
19038c0984e5SSebastian Reichel 		break;
19048c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
19058c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
19068c0984e5SSebastian Reichel 		break;
19078c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
19088c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
19098c0984e5SSebastian Reichel 		break;
19108c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
19118c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
19128c0984e5SSebastian Reichel 		break;
19138c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
19148c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
19158c0984e5SSebastian Reichel 		break;
19168c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
19178c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
19188c0984e5SSebastian Reichel 		break;
19198c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
19208c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.charge_full, val);
19218c0984e5SSebastian Reichel 		break;
19228c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
19238c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->charge_design_full, val);
19248c0984e5SSebastian Reichel 		break;
1925ccce4409SLiam Breck 	/*
1926ccce4409SLiam Breck 	 * TODO: Implement these to make registers set from
1927ccce4409SLiam Breck 	 * power_supply_battery_info visible in sysfs.
1928ccce4409SLiam Breck 	 */
1929ccce4409SLiam Breck 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
1930ccce4409SLiam Breck 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1931ccce4409SLiam Breck 		return -EINVAL;
19328c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
19338c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
19348c0984e5SSebastian Reichel 		break;
19358c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ENERGY_NOW:
19368c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.energy, val);
19378c0984e5SSebastian Reichel 		break;
19388c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_POWER_AVG:
19398c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.power_avg, val);
19408c0984e5SSebastian Reichel 		break;
19418c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
19428c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.health, val);
19438c0984e5SSebastian Reichel 		break;
19448c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
19458c0984e5SSebastian Reichel 		val->strval = BQ27XXX_MANUFACTURER;
19468c0984e5SSebastian Reichel 		break;
19478c0984e5SSebastian Reichel 	default:
19488c0984e5SSebastian Reichel 		return -EINVAL;
19498c0984e5SSebastian Reichel 	}
19508c0984e5SSebastian Reichel 
19518c0984e5SSebastian Reichel 	return ret;
19528c0984e5SSebastian Reichel }
19538c0984e5SSebastian Reichel 
19548c0984e5SSebastian Reichel static void bq27xxx_external_power_changed(struct power_supply *psy)
19558c0984e5SSebastian Reichel {
19568c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
19578c0984e5SSebastian Reichel 
19588c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
19598c0984e5SSebastian Reichel 	schedule_delayed_work(&di->work, 0);
19608c0984e5SSebastian Reichel }
19618c0984e5SSebastian Reichel 
19628c0984e5SSebastian Reichel int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
19638c0984e5SSebastian Reichel {
19648c0984e5SSebastian Reichel 	struct power_supply_desc *psy_desc;
1965ccce4409SLiam Breck 	struct power_supply_config psy_cfg = {
1966ccce4409SLiam Breck 		.of_node = di->dev->of_node,
1967ccce4409SLiam Breck 		.drv_data = di,
1968ccce4409SLiam Breck 	};
19698c0984e5SSebastian Reichel 
19708c0984e5SSebastian Reichel 	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
19718c0984e5SSebastian Reichel 	mutex_init(&di->lock);
19723a731c64SLiam Breck 
19739aade6d8SLiam Breck 	di->regs       = bq27xxx_chip_data[di->chip].regs;
197405045379SLiam Breck 	di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
197505045379SLiam Breck 	di->dm_regs    = bq27xxx_chip_data[di->chip].dm_regs;
19763a731c64SLiam Breck 	di->opts       = bq27xxx_chip_data[di->chip].opts;
19778c0984e5SSebastian Reichel 
19788c0984e5SSebastian Reichel 	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
19798c0984e5SSebastian Reichel 	if (!psy_desc)
19808c0984e5SSebastian Reichel 		return -ENOMEM;
19818c0984e5SSebastian Reichel 
19828c0984e5SSebastian Reichel 	psy_desc->name = di->name;
19838c0984e5SSebastian Reichel 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
19849aade6d8SLiam Breck 	psy_desc->properties = bq27xxx_chip_data[di->chip].props;
19859aade6d8SLiam Breck 	psy_desc->num_properties = bq27xxx_chip_data[di->chip].props_size;
19868c0984e5SSebastian Reichel 	psy_desc->get_property = bq27xxx_battery_get_property;
19878c0984e5SSebastian Reichel 	psy_desc->external_power_changed = bq27xxx_external_power_changed;
19888c0984e5SSebastian Reichel 
19898c0984e5SSebastian Reichel 	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
19904024810cSKrzysztof Kozlowski 	if (IS_ERR(di->bat))
19914024810cSKrzysztof Kozlowski 		return dev_err_probe(di->dev, PTR_ERR(di->bat),
19924024810cSKrzysztof Kozlowski 				     "failed to register battery\n");
19938c0984e5SSebastian Reichel 
1994ccce4409SLiam Breck 	bq27xxx_battery_settings(di);
19958c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
19968c0984e5SSebastian Reichel 
19971d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
19981d72706fSMatt Ranostay 	list_add(&di->list, &bq27xxx_battery_devices);
19991d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
20001d72706fSMatt Ranostay 
20018c0984e5SSebastian Reichel 	return 0;
20028c0984e5SSebastian Reichel }
20038c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
20048c0984e5SSebastian Reichel 
20058c0984e5SSebastian Reichel void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
20068c0984e5SSebastian Reichel {
20078c0984e5SSebastian Reichel 	/*
20088c0984e5SSebastian Reichel 	 * power_supply_unregister call bq27xxx_battery_get_property which
20098c0984e5SSebastian Reichel 	 * call bq27xxx_battery_poll.
20108c0984e5SSebastian Reichel 	 * Make sure that bq27xxx_battery_poll will not call
20118c0984e5SSebastian Reichel 	 * schedule_delayed_work again after unregister (which cause OOPS).
20128c0984e5SSebastian Reichel 	 */
20138c0984e5SSebastian Reichel 	poll_interval = 0;
20148c0984e5SSebastian Reichel 
20158c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
20168c0984e5SSebastian Reichel 
20178c0984e5SSebastian Reichel 	power_supply_unregister(di->bat);
20188c0984e5SSebastian Reichel 
20191d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
20201d72706fSMatt Ranostay 	list_del(&di->list);
20211d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
20221d72706fSMatt Ranostay 
20238c0984e5SSebastian Reichel 	mutex_destroy(&di->lock);
20248c0984e5SSebastian Reichel }
20258c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
20268c0984e5SSebastian Reichel 
20278c0984e5SSebastian Reichel MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
20288c0984e5SSebastian Reichel MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
20298c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
2030