18c0984e5SSebastian Reichel /*
28c0984e5SSebastian Reichel  * BQ27xxx battery driver
38c0984e5SSebastian Reichel  *
48c0984e5SSebastian Reichel  * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
58c0984e5SSebastian Reichel  * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
68c0984e5SSebastian Reichel  * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
78c0984e5SSebastian Reichel  * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
88c0984e5SSebastian Reichel  *
98c0984e5SSebastian Reichel  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
108c0984e5SSebastian Reichel  *
118c0984e5SSebastian Reichel  * This package is free software; you can redistribute it and/or modify
128c0984e5SSebastian Reichel  * it under the terms of the GNU General Public License version 2 as
138c0984e5SSebastian Reichel  * published by the Free Software Foundation.
148c0984e5SSebastian Reichel  *
158c0984e5SSebastian Reichel  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
168c0984e5SSebastian Reichel  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
178c0984e5SSebastian Reichel  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
188c0984e5SSebastian Reichel  *
198c0984e5SSebastian Reichel  * Datasheets:
208c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27000
218c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27200
228c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27010
238c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27210
248c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27500
25bd28177fSChris Lapa  * http://www.ti.com/product/bq27510-g1
26698a2bf5SChris Lapa  * http://www.ti.com/product/bq27510-g2
278c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27510-g3
288c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27520-g4
2968f2a813SChris Lapa  * http://www.ti.com/product/bq27520-g1
30a5deb9a9SChris Lapa  * http://www.ti.com/product/bq27520-g2
318c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27530-g1
328c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27531-g1
338c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27541-g1
348c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27542-g1
358c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27546-g1
368c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27742-g1
378c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27545-g1
388c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27421-g1
398c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27425-g1
408c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27411-g1
418c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27621-g1
428c0984e5SSebastian Reichel  */
438c0984e5SSebastian Reichel 
448c0984e5SSebastian Reichel #include <linux/device.h>
458c0984e5SSebastian Reichel #include <linux/module.h>
461d72706fSMatt Ranostay #include <linux/mutex.h>
478c0984e5SSebastian Reichel #include <linux/param.h>
488c0984e5SSebastian Reichel #include <linux/jiffies.h>
498c0984e5SSebastian Reichel #include <linux/workqueue.h>
508c0984e5SSebastian Reichel #include <linux/delay.h>
518c0984e5SSebastian Reichel #include <linux/platform_device.h>
528c0984e5SSebastian Reichel #include <linux/power_supply.h>
538c0984e5SSebastian Reichel #include <linux/slab.h>
548c0984e5SSebastian Reichel #include <linux/of.h>
558c0984e5SSebastian Reichel 
568c0984e5SSebastian Reichel #include <linux/power/bq27xxx_battery.h>
578c0984e5SSebastian Reichel 
588c0984e5SSebastian Reichel #define DRIVER_VERSION		"1.2.0"
598c0984e5SSebastian Reichel 
608c0984e5SSebastian Reichel #define BQ27XXX_MANUFACTURER	"Texas Instruments"
618c0984e5SSebastian Reichel 
628c0984e5SSebastian Reichel /* BQ27XXX Flags */
638c0984e5SSebastian Reichel #define BQ27XXX_FLAG_DSC	BIT(0)
648c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOCF	BIT(1) /* State-of-Charge threshold final */
658c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOC1	BIT(2) /* State-of-Charge threshold 1 */
668c0984e5SSebastian Reichel #define BQ27XXX_FLAG_FC		BIT(9)
678c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OTD	BIT(14)
688c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OTC	BIT(15)
698c0984e5SSebastian Reichel #define BQ27XXX_FLAG_UT		BIT(14)
708c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OT		BIT(15)
718c0984e5SSebastian Reichel 
728c0984e5SSebastian Reichel /* BQ27000 has different layout for Flags register */
738c0984e5SSebastian Reichel #define BQ27000_FLAG_EDVF	BIT(0) /* Final End-of-Discharge-Voltage flag */
748c0984e5SSebastian Reichel #define BQ27000_FLAG_EDV1	BIT(1) /* First End-of-Discharge-Voltage flag */
758c0984e5SSebastian Reichel #define BQ27000_FLAG_CI		BIT(4) /* Capacity Inaccurate flag */
768c0984e5SSebastian Reichel #define BQ27000_FLAG_FC		BIT(5)
778c0984e5SSebastian Reichel #define BQ27000_FLAG_CHGS	BIT(7) /* Charge state flag */
788c0984e5SSebastian Reichel 
798c0984e5SSebastian Reichel #define BQ27XXX_RS			(20) /* Resistor sense mOhm */
808c0984e5SSebastian Reichel #define BQ27XXX_POWER_CONSTANT		(29200) /* 29.2 µV^2 * 1000 */
818c0984e5SSebastian Reichel #define BQ27XXX_CURRENT_CONSTANT	(3570) /* 3.57 µV * 1000 */
828c0984e5SSebastian Reichel 
838c0984e5SSebastian Reichel #define INVALID_REG_ADDR	0xff
848c0984e5SSebastian Reichel 
858c0984e5SSebastian Reichel /*
868c0984e5SSebastian Reichel  * bq27xxx_reg_index - Register names
878c0984e5SSebastian Reichel  *
888c0984e5SSebastian Reichel  * These are indexes into a device's register mapping array.
898c0984e5SSebastian Reichel  */
908c0984e5SSebastian Reichel 
918c0984e5SSebastian Reichel enum bq27xxx_reg_index {
928c0984e5SSebastian Reichel 	BQ27XXX_REG_CTRL = 0,	/* Control */
938c0984e5SSebastian Reichel 	BQ27XXX_REG_TEMP,	/* Temperature */
948c0984e5SSebastian Reichel 	BQ27XXX_REG_INT_TEMP,	/* Internal Temperature */
958c0984e5SSebastian Reichel 	BQ27XXX_REG_VOLT,	/* Voltage */
968c0984e5SSebastian Reichel 	BQ27XXX_REG_AI,		/* Average Current */
978c0984e5SSebastian Reichel 	BQ27XXX_REG_FLAGS,	/* Flags */
988c0984e5SSebastian Reichel 	BQ27XXX_REG_TTE,	/* Time-to-Empty */
998c0984e5SSebastian Reichel 	BQ27XXX_REG_TTF,	/* Time-to-Full */
1008c0984e5SSebastian Reichel 	BQ27XXX_REG_TTES,	/* Time-to-Empty Standby */
1018c0984e5SSebastian Reichel 	BQ27XXX_REG_TTECP,	/* Time-to-Empty at Constant Power */
1028c0984e5SSebastian Reichel 	BQ27XXX_REG_NAC,	/* Nominal Available Capacity */
1038c0984e5SSebastian Reichel 	BQ27XXX_REG_FCC,	/* Full Charge Capacity */
1048c0984e5SSebastian Reichel 	BQ27XXX_REG_CYCT,	/* Cycle Count */
1058c0984e5SSebastian Reichel 	BQ27XXX_REG_AE,		/* Available Energy */
1068c0984e5SSebastian Reichel 	BQ27XXX_REG_SOC,	/* State-of-Charge */
1078c0984e5SSebastian Reichel 	BQ27XXX_REG_DCAP,	/* Design Capacity */
1088c0984e5SSebastian Reichel 	BQ27XXX_REG_AP,		/* Average Power */
1098c0984e5SSebastian Reichel 	BQ27XXX_REG_MAX,	/* sentinel */
1108c0984e5SSebastian Reichel };
1118c0984e5SSebastian Reichel 
1128c0984e5SSebastian Reichel /* Register mappings */
1138c0984e5SSebastian Reichel static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = {
1148c0984e5SSebastian Reichel 	[BQ27000] = {
1158c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1168c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1178c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1188c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1198c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1208c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1218c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1228c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1238c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1248c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1258c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1268c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1278c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1288c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = 0x22,
1298c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1308c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1318c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
1328c0984e5SSebastian Reichel 	},
1338c0984e5SSebastian Reichel 	[BQ27010] = {
1348c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1358c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1368c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1378c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1388c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1398c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1408c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1418c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1428c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1438c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1448c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1458c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1468c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1478c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1488c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1498c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1508c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1518c0984e5SSebastian Reichel 	},
152818e3012SChris Lapa 	[BQ2750X] = {
1538c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1548c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1558c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
1568c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1578c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1588c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1598c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1608c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1618c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1a,
1628c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1638c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1648c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1658c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1668c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1678c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
1688c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
1698c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1708c0984e5SSebastian Reichel 	},
1716da6e4bdSChris Lapa 	[BQ2751X] = {
1723bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_CTRL] = 0x00,
1733bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TEMP] = 0x06,
1743bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_INT_TEMP] = 0x28,
1753bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_VOLT] = 0x08,
1763bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_AI] = 0x14,
1773bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_FLAGS] = 0x0a,
1783bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTE] = 0x16,
1793bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1803bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTES] = 0x1a,
1813bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1823bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_NAC] = 0x0c,
1833bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_FCC] = 0x12,
1843bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_CYCT] = 0x1e,
1853bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1863bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_SOC] = 0x20,
1873bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_DCAP] = 0x2e,
1883bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1893bee9ea1SAndrew F. Davis 	},
19032833635SChris Lapa 	[BQ27500] = {
19132833635SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
19232833635SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
19332833635SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
19432833635SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
19532833635SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
19632833635SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
19732833635SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
19832833635SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
19932833635SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
20032833635SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
20132833635SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
20232833635SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
20332833635SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
20432833635SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
20532833635SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
20632833635SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
20732833635SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
20832833635SChris Lapa 	},
209bd28177fSChris Lapa 	[BQ27510G1] = {
210bd28177fSChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
211bd28177fSChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
212bd28177fSChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
213bd28177fSChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
214bd28177fSChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
215bd28177fSChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
216bd28177fSChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
217bd28177fSChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
218bd28177fSChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
219bd28177fSChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
220bd28177fSChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
221bd28177fSChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
222bd28177fSChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
223bd28177fSChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
224bd28177fSChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
225bd28177fSChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
226bd28177fSChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
227bd28177fSChris Lapa 	},
228698a2bf5SChris Lapa 	[BQ27510G2] = {
229698a2bf5SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
230698a2bf5SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
231698a2bf5SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
232698a2bf5SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
233698a2bf5SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
234698a2bf5SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
235698a2bf5SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
236698a2bf5SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
237698a2bf5SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
238698a2bf5SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
239698a2bf5SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
240698a2bf5SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
241698a2bf5SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
242698a2bf5SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
243698a2bf5SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
244698a2bf5SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
245698a2bf5SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
246698a2bf5SChris Lapa 	},
24771375aa7SChris Lapa 	[BQ27510G3] = {
24871375aa7SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
24971375aa7SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
25071375aa7SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = 0x28,
25171375aa7SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
25271375aa7SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
25371375aa7SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
25471375aa7SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
25571375aa7SChris Lapa 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
25671375aa7SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1a,
25771375aa7SChris Lapa 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
25871375aa7SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
25971375aa7SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
26071375aa7SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x1e,
26171375aa7SChris Lapa 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
26271375aa7SChris Lapa 		[BQ27XXX_REG_SOC] = 0x20,
26371375aa7SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x2e,
26471375aa7SChris Lapa 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
26571375aa7SChris Lapa 	},
26668f2a813SChris Lapa 	[BQ27520G1] = {
26768f2a813SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
26868f2a813SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
26968f2a813SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
27068f2a813SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
27168f2a813SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
27268f2a813SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
27368f2a813SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
27468f2a813SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
27568f2a813SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
27668f2a813SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
27768f2a813SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
27868f2a813SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
27968f2a813SChris Lapa 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
28068f2a813SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
28168f2a813SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
28268f2a813SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
28368f2a813SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
28468f2a813SChris Lapa 	},
285a5deb9a9SChris Lapa 	[BQ27520G2] = {
286a5deb9a9SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
287a5deb9a9SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
288a5deb9a9SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = 0x36,
289a5deb9a9SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
290a5deb9a9SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
291a5deb9a9SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
292a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
293a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
294a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
295a5deb9a9SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
296a5deb9a9SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
297a5deb9a9SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
298a5deb9a9SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
299a5deb9a9SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
300a5deb9a9SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
301a5deb9a9SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
302a5deb9a9SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
303a5deb9a9SChris Lapa 	},
3048c0984e5SSebastian Reichel 	[BQ27530] = {
3058c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3068c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
3078c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x32,
3088c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
3098c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
3108c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
3118c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
3128c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3138c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3148c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3158c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
3168c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
3178c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
3188c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3198c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
3208c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
3218c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
3228c0984e5SSebastian Reichel 	},
3238c0984e5SSebastian Reichel 	[BQ27541] = {
3248c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3258c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
3268c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
3278c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
3288c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
3298c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
3308c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
3318c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3328c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3338c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3348c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
3358c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
3368c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
3378c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3388c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
3398c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
3408c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
3418c0984e5SSebastian Reichel 	},
3428c0984e5SSebastian Reichel 	[BQ27545] = {
3438c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3448c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
3458c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
3468c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
3478c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
3488c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
3498c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
3508c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3518c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3528c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3538c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
3548c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
3558c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
3568c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3578c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
3588c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
3598c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
3608c0984e5SSebastian Reichel 	},
3618c0984e5SSebastian Reichel 	[BQ27421] = {
3628c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3638c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x02,
3648c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x1e,
3658c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x04,
3668c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x10,
3678c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x06,
3688c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
3698c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3708c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3718c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3728c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x08,
3738c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x0e,
3748c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
3758c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3768c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x1c,
3778c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
3788c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x18,
3798c0984e5SSebastian Reichel 	},
3808c0984e5SSebastian Reichel };
3818c0984e5SSebastian Reichel 
3828c0984e5SSebastian Reichel static enum power_supply_property bq27000_battery_props[] = {
3838c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3848c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
3858c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3868c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3878c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
3888c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3898c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
3918c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
3928c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
3938c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
3948c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
3958c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
3968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
3988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ENERGY_NOW,
3998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
4008c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
4018c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
4028c0984e5SSebastian Reichel };
4038c0984e5SSebastian Reichel 
4048c0984e5SSebastian Reichel static enum power_supply_property bq27010_battery_props[] = {
4058c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
4068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
4078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
4088c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
4098c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
4108c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
4118c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
4128c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
4148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
4158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
4168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
4178c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
4188c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
4198c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
4208c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
4218c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
4228c0984e5SSebastian Reichel };
4238c0984e5SSebastian Reichel 
424818e3012SChris Lapa static enum power_supply_property bq2750x_battery_props[] = {
4258c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
4268c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
4278c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
4288c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
4298c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
4308c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
4318c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
4328c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4338c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
4348c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
4358c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
4368c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
4378c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
4388c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
4398c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
4408c0984e5SSebastian Reichel };
4418c0984e5SSebastian Reichel 
4426da6e4bdSChris Lapa static enum power_supply_property bq2751x_battery_props[] = {
4433bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_STATUS,
4443bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_PRESENT,
4453bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
4463bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CURRENT_NOW,
4473bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CAPACITY,
4483bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
4493bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_TEMP,
4503bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4513bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_TECHNOLOGY,
4523bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CHARGE_FULL,
4533bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CHARGE_NOW,
4543bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
4553bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CYCLE_COUNT,
4563bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_HEALTH,
4573bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_MANUFACTURER,
4583bee9ea1SAndrew F. Davis };
4593bee9ea1SAndrew F. Davis 
46032833635SChris Lapa static enum power_supply_property bq27500_battery_props[] = {
46132833635SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
46232833635SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
46332833635SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
46432833635SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
46532833635SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
46632833635SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
46732833635SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
46832833635SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
46932833635SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
47032833635SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
47132833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
47232833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
47332833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
47432833635SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
47532833635SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
47632833635SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
47732833635SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
47832833635SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
47932833635SChris Lapa };
48032833635SChris Lapa 
481bd28177fSChris Lapa static enum power_supply_property bq27510g1_battery_props[] = {
482bd28177fSChris Lapa 	POWER_SUPPLY_PROP_STATUS,
483bd28177fSChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
484bd28177fSChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
485bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
486bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
487bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
488bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TEMP,
489bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
490bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
491bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
492bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
493bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
494bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
495bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
496bd28177fSChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
497bd28177fSChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
498bd28177fSChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
499bd28177fSChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
500bd28177fSChris Lapa };
501bd28177fSChris Lapa 
502698a2bf5SChris Lapa static enum power_supply_property bq27510g2_battery_props[] = {
503698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
504698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
505698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
506698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
507698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
508698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
509698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
510698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
511698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
512698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
513698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
514698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
515698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
516698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
517698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
518698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
519698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
520698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
521698a2bf5SChris Lapa };
522698a2bf5SChris Lapa 
52371375aa7SChris Lapa static enum power_supply_property bq27510g3_battery_props[] = {
52471375aa7SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
52571375aa7SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
52671375aa7SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
52771375aa7SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
52871375aa7SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
52971375aa7SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
53071375aa7SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
53171375aa7SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
53271375aa7SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
53371375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
53471375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
53571375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
53671375aa7SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
53771375aa7SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
53871375aa7SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
53971375aa7SChris Lapa };
54071375aa7SChris Lapa 
54168f2a813SChris Lapa static enum power_supply_property bq27520g1_battery_props[] = {
54268f2a813SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
54368f2a813SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
54468f2a813SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
54568f2a813SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
54668f2a813SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
54768f2a813SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
54868f2a813SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
54968f2a813SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
55068f2a813SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
55168f2a813SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
55268f2a813SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
55368f2a813SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
55468f2a813SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
55568f2a813SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
55668f2a813SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
55768f2a813SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
55868f2a813SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
55968f2a813SChris Lapa };
56068f2a813SChris Lapa 
561a5deb9a9SChris Lapa static enum power_supply_property bq27520g2_battery_props[] = {
562a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
563a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
564a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
565a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
566a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
567a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
568a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
569a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
570a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
571a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
572a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
573a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
574a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
575a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
576a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
577a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
578a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
579a5deb9a9SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
580a5deb9a9SChris Lapa };
581a5deb9a9SChris Lapa 
5828c0984e5SSebastian Reichel static enum power_supply_property bq27530_battery_props[] = {
5838c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
5848c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
5858c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
5868c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
5878c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
5888c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
5898c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
5908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
5918c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
5928c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
5938c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
5948c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
5958c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
5968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
5978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5988c0984e5SSebastian Reichel };
5998c0984e5SSebastian Reichel 
6008c0984e5SSebastian Reichel static enum power_supply_property bq27541_battery_props[] = {
6018c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6028c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
6038c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6048c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
6058c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
6068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
6078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
6088c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6098c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
6108c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
6118c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
6128c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
6138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
6148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
6158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
6168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
6178c0984e5SSebastian Reichel };
6188c0984e5SSebastian Reichel 
6198c0984e5SSebastian Reichel static enum power_supply_property bq27545_battery_props[] = {
6208c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6218c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
6228c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6238c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
6248c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
6258c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
6268c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
6278c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6288c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
6298c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
6308c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
6318c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
6328c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
6338c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
6348c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
6358c0984e5SSebastian Reichel };
6368c0984e5SSebastian Reichel 
6378c0984e5SSebastian Reichel static enum power_supply_property bq27421_battery_props[] = {
6388c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6398c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
6408c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6418c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
6428c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
6438c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
6448c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
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_CHARGE_FULL_DESIGN,
6498c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
6508c0984e5SSebastian Reichel };
6518c0984e5SSebastian Reichel 
6528c0984e5SSebastian Reichel #define BQ27XXX_PROP(_id, _prop)		\
6538c0984e5SSebastian Reichel 	[_id] = {				\
6548c0984e5SSebastian Reichel 		.props = _prop,			\
6558c0984e5SSebastian Reichel 		.size = ARRAY_SIZE(_prop),	\
6568c0984e5SSebastian Reichel 	}
6578c0984e5SSebastian Reichel 
6588c0984e5SSebastian Reichel static struct {
6598c0984e5SSebastian Reichel 	enum power_supply_property *props;
6608c0984e5SSebastian Reichel 	size_t size;
6618c0984e5SSebastian Reichel } bq27xxx_battery_props[] = {
6628c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27000, bq27000_battery_props),
6638c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27010, bq27010_battery_props),
664818e3012SChris Lapa 	BQ27XXX_PROP(BQ2750X, bq2750x_battery_props),
6656da6e4bdSChris Lapa 	BQ27XXX_PROP(BQ2751X, bq2751x_battery_props),
66632833635SChris Lapa 	BQ27XXX_PROP(BQ27500, bq27500_battery_props),
667bd28177fSChris Lapa 	BQ27XXX_PROP(BQ27510G1, bq27510g1_battery_props),
668698a2bf5SChris Lapa 	BQ27XXX_PROP(BQ27510G2, bq27510g2_battery_props),
66971375aa7SChris Lapa 	BQ27XXX_PROP(BQ27510G3, bq27510g3_battery_props),
67068f2a813SChris Lapa 	BQ27XXX_PROP(BQ27520G1, bq27520g1_battery_props),
671a5deb9a9SChris Lapa 	BQ27XXX_PROP(BQ27520G2, bq27520g2_battery_props),
6728c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27530, bq27530_battery_props),
6738c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27541, bq27541_battery_props),
6748c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27545, bq27545_battery_props),
6758c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27421, bq27421_battery_props),
6768c0984e5SSebastian Reichel };
6778c0984e5SSebastian Reichel 
6781d72706fSMatt Ranostay static DEFINE_MUTEX(bq27xxx_list_lock);
6791d72706fSMatt Ranostay static LIST_HEAD(bq27xxx_battery_devices);
6801d72706fSMatt Ranostay 
6811d72706fSMatt Ranostay static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
6821d72706fSMatt Ranostay {
6831d72706fSMatt Ranostay 	struct bq27xxx_device_info *di;
684950b6c2dSMatt Ranostay 	unsigned int prev_val = *(unsigned int *) kp->arg;
6851d72706fSMatt Ranostay 	int ret;
6861d72706fSMatt Ranostay 
6871d72706fSMatt Ranostay 	ret = param_set_uint(val, kp);
688950b6c2dSMatt Ranostay 	if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
6891d72706fSMatt Ranostay 		return ret;
6901d72706fSMatt Ranostay 
6911d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
6921d72706fSMatt Ranostay 	list_for_each_entry(di, &bq27xxx_battery_devices, list) {
6931d72706fSMatt Ranostay 		cancel_delayed_work_sync(&di->work);
6941d72706fSMatt Ranostay 		schedule_delayed_work(&di->work, 0);
6951d72706fSMatt Ranostay 	}
6961d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
6971d72706fSMatt Ranostay 
6981d72706fSMatt Ranostay 	return ret;
6991d72706fSMatt Ranostay }
7001d72706fSMatt Ranostay 
7011d72706fSMatt Ranostay static const struct kernel_param_ops param_ops_poll_interval = {
7021d72706fSMatt Ranostay 	.get = param_get_uint,
7031d72706fSMatt Ranostay 	.set = poll_interval_param_set,
7041d72706fSMatt Ranostay };
7051d72706fSMatt Ranostay 
7068c0984e5SSebastian Reichel static unsigned int poll_interval = 360;
7071d72706fSMatt Ranostay module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
7088c0984e5SSebastian Reichel MODULE_PARM_DESC(poll_interval,
7098c0984e5SSebastian Reichel 		 "battery poll interval in seconds - 0 disables polling");
7108c0984e5SSebastian Reichel 
7118c0984e5SSebastian Reichel /*
7128c0984e5SSebastian Reichel  * Common code for BQ27xxx devices
7138c0984e5SSebastian Reichel  */
7148c0984e5SSebastian Reichel 
7158c0984e5SSebastian Reichel static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
7168c0984e5SSebastian Reichel 			       bool single)
7178c0984e5SSebastian Reichel {
7188c0984e5SSebastian Reichel 	/* Reports EINVAL for invalid/missing registers */
7198c0984e5SSebastian Reichel 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
7208c0984e5SSebastian Reichel 		return -EINVAL;
7218c0984e5SSebastian Reichel 
7228c0984e5SSebastian Reichel 	return di->bus.read(di, di->regs[reg_index], single);
7238c0984e5SSebastian Reichel }
7248c0984e5SSebastian Reichel 
7258c0984e5SSebastian Reichel /*
7268c0984e5SSebastian Reichel  * Return the battery State-of-Charge
7278c0984e5SSebastian Reichel  * Or < 0 if something fails.
7288c0984e5SSebastian Reichel  */
7298c0984e5SSebastian Reichel static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
7308c0984e5SSebastian Reichel {
7318c0984e5SSebastian Reichel 	int soc;
7328c0984e5SSebastian Reichel 
7338c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
7348c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
7358c0984e5SSebastian Reichel 	else
7368c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
7378c0984e5SSebastian Reichel 
7388c0984e5SSebastian Reichel 	if (soc < 0)
7398c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading State-of-Charge\n");
7408c0984e5SSebastian Reichel 
7418c0984e5SSebastian Reichel 	return soc;
7428c0984e5SSebastian Reichel }
7438c0984e5SSebastian Reichel 
7448c0984e5SSebastian Reichel /*
7458c0984e5SSebastian Reichel  * Return a battery charge value in µAh
7468c0984e5SSebastian Reichel  * Or < 0 if something fails.
7478c0984e5SSebastian Reichel  */
7488c0984e5SSebastian Reichel static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
7498c0984e5SSebastian Reichel {
7508c0984e5SSebastian Reichel 	int charge;
7518c0984e5SSebastian Reichel 
7528c0984e5SSebastian Reichel 	charge = bq27xxx_read(di, reg, false);
7538c0984e5SSebastian Reichel 	if (charge < 0) {
7548c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
7558c0984e5SSebastian Reichel 			reg, charge);
7568c0984e5SSebastian Reichel 		return charge;
7578c0984e5SSebastian Reichel 	}
7588c0984e5SSebastian Reichel 
7598c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
7608c0984e5SSebastian Reichel 		charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
7618c0984e5SSebastian Reichel 	else
7628c0984e5SSebastian Reichel 		charge *= 1000;
7638c0984e5SSebastian Reichel 
7648c0984e5SSebastian Reichel 	return charge;
7658c0984e5SSebastian Reichel }
7668c0984e5SSebastian Reichel 
7678c0984e5SSebastian Reichel /*
7688c0984e5SSebastian Reichel  * Return the battery Nominal available capacity in µAh
7698c0984e5SSebastian Reichel  * Or < 0 if something fails.
7708c0984e5SSebastian Reichel  */
7718c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
7728c0984e5SSebastian Reichel {
7738c0984e5SSebastian Reichel 	int flags;
7748c0984e5SSebastian Reichel 
7758c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
7768c0984e5SSebastian Reichel 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
7778c0984e5SSebastian Reichel 		if (flags >= 0 && (flags & BQ27000_FLAG_CI))
7788c0984e5SSebastian Reichel 			return -ENODATA;
7798c0984e5SSebastian Reichel 	}
7808c0984e5SSebastian Reichel 
7818c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
7828c0984e5SSebastian Reichel }
7838c0984e5SSebastian Reichel 
7848c0984e5SSebastian Reichel /*
7858c0984e5SSebastian Reichel  * Return the battery Full Charge Capacity in µAh
7868c0984e5SSebastian Reichel  * Or < 0 if something fails.
7878c0984e5SSebastian Reichel  */
7888c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
7898c0984e5SSebastian Reichel {
7908c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
7918c0984e5SSebastian Reichel }
7928c0984e5SSebastian Reichel 
7938c0984e5SSebastian Reichel /*
7948c0984e5SSebastian Reichel  * Return the Design Capacity in µAh
7958c0984e5SSebastian Reichel  * Or < 0 if something fails.
7968c0984e5SSebastian Reichel  */
7978c0984e5SSebastian Reichel static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
7988c0984e5SSebastian Reichel {
7998c0984e5SSebastian Reichel 	int dcap;
8008c0984e5SSebastian Reichel 
8018c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
8028c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
8038c0984e5SSebastian Reichel 	else
8048c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
8058c0984e5SSebastian Reichel 
8068c0984e5SSebastian Reichel 	if (dcap < 0) {
8078c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading initial last measured discharge\n");
8088c0984e5SSebastian Reichel 		return dcap;
8098c0984e5SSebastian Reichel 	}
8108c0984e5SSebastian Reichel 
8118c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
8128c0984e5SSebastian Reichel 		dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
8138c0984e5SSebastian Reichel 	else
8148c0984e5SSebastian Reichel 		dcap *= 1000;
8158c0984e5SSebastian Reichel 
8168c0984e5SSebastian Reichel 	return dcap;
8178c0984e5SSebastian Reichel }
8188c0984e5SSebastian Reichel 
8198c0984e5SSebastian Reichel /*
8208c0984e5SSebastian Reichel  * Return the battery Available energy in µWh
8218c0984e5SSebastian Reichel  * Or < 0 if something fails.
8228c0984e5SSebastian Reichel  */
8238c0984e5SSebastian Reichel static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
8248c0984e5SSebastian Reichel {
8258c0984e5SSebastian Reichel 	int ae;
8268c0984e5SSebastian Reichel 
8278c0984e5SSebastian Reichel 	ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
8288c0984e5SSebastian Reichel 	if (ae < 0) {
8298c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading available energy\n");
8308c0984e5SSebastian Reichel 		return ae;
8318c0984e5SSebastian Reichel 	}
8328c0984e5SSebastian Reichel 
8338c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
8348c0984e5SSebastian Reichel 		ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
8358c0984e5SSebastian Reichel 	else
8368c0984e5SSebastian Reichel 		ae *= 1000;
8378c0984e5SSebastian Reichel 
8388c0984e5SSebastian Reichel 	return ae;
8398c0984e5SSebastian Reichel }
8408c0984e5SSebastian Reichel 
8418c0984e5SSebastian Reichel /*
8428c0984e5SSebastian Reichel  * Return the battery temperature in tenths of degree Kelvin
8438c0984e5SSebastian Reichel  * Or < 0 if something fails.
8448c0984e5SSebastian Reichel  */
8458c0984e5SSebastian Reichel static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
8468c0984e5SSebastian Reichel {
8478c0984e5SSebastian Reichel 	int temp;
8488c0984e5SSebastian Reichel 
8498c0984e5SSebastian Reichel 	temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
8508c0984e5SSebastian Reichel 	if (temp < 0) {
8518c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading temperature\n");
8528c0984e5SSebastian Reichel 		return temp;
8538c0984e5SSebastian Reichel 	}
8548c0984e5SSebastian Reichel 
8558c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
8568c0984e5SSebastian Reichel 		temp = 5 * temp / 2;
8578c0984e5SSebastian Reichel 
8588c0984e5SSebastian Reichel 	return temp;
8598c0984e5SSebastian Reichel }
8608c0984e5SSebastian Reichel 
8618c0984e5SSebastian Reichel /*
8628c0984e5SSebastian Reichel  * Return the battery Cycle count total
8638c0984e5SSebastian Reichel  * Or < 0 if something fails.
8648c0984e5SSebastian Reichel  */
8658c0984e5SSebastian Reichel static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
8668c0984e5SSebastian Reichel {
8678c0984e5SSebastian Reichel 	int cyct;
8688c0984e5SSebastian Reichel 
8698c0984e5SSebastian Reichel 	cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
8708c0984e5SSebastian Reichel 	if (cyct < 0)
8718c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading cycle count total\n");
8728c0984e5SSebastian Reichel 
8738c0984e5SSebastian Reichel 	return cyct;
8748c0984e5SSebastian Reichel }
8758c0984e5SSebastian Reichel 
8768c0984e5SSebastian Reichel /*
8778c0984e5SSebastian Reichel  * Read a time register.
8788c0984e5SSebastian Reichel  * Return < 0 if something fails.
8798c0984e5SSebastian Reichel  */
8808c0984e5SSebastian Reichel static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
8818c0984e5SSebastian Reichel {
8828c0984e5SSebastian Reichel 	int tval;
8838c0984e5SSebastian Reichel 
8848c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, reg, false);
8858c0984e5SSebastian Reichel 	if (tval < 0) {
8868c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading time register %02x: %d\n",
8878c0984e5SSebastian Reichel 			reg, tval);
8888c0984e5SSebastian Reichel 		return tval;
8898c0984e5SSebastian Reichel 	}
8908c0984e5SSebastian Reichel 
8918c0984e5SSebastian Reichel 	if (tval == 65535)
8928c0984e5SSebastian Reichel 		return -ENODATA;
8938c0984e5SSebastian Reichel 
8948c0984e5SSebastian Reichel 	return tval * 60;
8958c0984e5SSebastian Reichel }
8968c0984e5SSebastian Reichel 
8978c0984e5SSebastian Reichel /*
8988c0984e5SSebastian Reichel  * Read an average power register.
8998c0984e5SSebastian Reichel  * Return < 0 if something fails.
9008c0984e5SSebastian Reichel  */
9018c0984e5SSebastian Reichel static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
9028c0984e5SSebastian Reichel {
9038c0984e5SSebastian Reichel 	int tval;
9048c0984e5SSebastian Reichel 
9058c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
9068c0984e5SSebastian Reichel 	if (tval < 0) {
9078c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading average power register  %02x: %d\n",
9088c0984e5SSebastian Reichel 			BQ27XXX_REG_AP, tval);
9098c0984e5SSebastian Reichel 		return tval;
9108c0984e5SSebastian Reichel 	}
9118c0984e5SSebastian Reichel 
9128c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
9138c0984e5SSebastian Reichel 		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
9148c0984e5SSebastian Reichel 	else
9158c0984e5SSebastian Reichel 		return tval;
9168c0984e5SSebastian Reichel }
9178c0984e5SSebastian Reichel 
9188c0984e5SSebastian Reichel /*
9198c0984e5SSebastian Reichel  * Returns true if a battery over temperature condition is detected
9208c0984e5SSebastian Reichel  */
9218c0984e5SSebastian Reichel static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
9228c0984e5SSebastian Reichel {
923e839a448SChris Lapa 	switch (di->chip) {
924818e3012SChris Lapa 	case BQ2750X:
9256da6e4bdSChris Lapa 	case BQ2751X:
92632833635SChris Lapa 	case BQ27500:
927bd28177fSChris Lapa 	case BQ27510G1:
928698a2bf5SChris Lapa 	case BQ27510G2:
92971375aa7SChris Lapa 	case BQ27510G3:
93068f2a813SChris Lapa 	case BQ27520G1:
931a5deb9a9SChris Lapa 	case BQ27520G2:
932e839a448SChris Lapa 	case BQ27541:
933e839a448SChris Lapa 	case BQ27545:
9348c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
935e839a448SChris Lapa 	case BQ27530:
936e839a448SChris Lapa 	case BQ27421:
9378c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_OT;
938e839a448SChris Lapa 	default:
9398c0984e5SSebastian Reichel 		return false;
9408c0984e5SSebastian Reichel 	}
941e839a448SChris Lapa }
9428c0984e5SSebastian Reichel 
9438c0984e5SSebastian Reichel /*
9448c0984e5SSebastian Reichel  * Returns true if a battery under temperature condition is detected
9458c0984e5SSebastian Reichel  */
9468c0984e5SSebastian Reichel static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
9478c0984e5SSebastian Reichel {
9488c0984e5SSebastian Reichel 	if (di->chip == BQ27530 || di->chip == BQ27421)
9498c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_UT;
9508c0984e5SSebastian Reichel 
9518c0984e5SSebastian Reichel 	return false;
9528c0984e5SSebastian Reichel }
9538c0984e5SSebastian Reichel 
9548c0984e5SSebastian Reichel /*
9558c0984e5SSebastian Reichel  * Returns true if a low state of charge condition is detected
9568c0984e5SSebastian Reichel  */
9578c0984e5SSebastian Reichel static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
9588c0984e5SSebastian Reichel {
9598c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
9608c0984e5SSebastian Reichel 		return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
9618c0984e5SSebastian Reichel 	else
9628c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
9638c0984e5SSebastian Reichel }
9648c0984e5SSebastian Reichel 
9658c0984e5SSebastian Reichel /*
9668c0984e5SSebastian Reichel  * Read flag register.
9678c0984e5SSebastian Reichel  * Return < 0 if something fails.
9688c0984e5SSebastian Reichel  */
9698c0984e5SSebastian Reichel static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
9708c0984e5SSebastian Reichel {
9718c0984e5SSebastian Reichel 	int flags;
972e4a404a0SH. Nikolaus Schaller 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
9738c0984e5SSebastian Reichel 
974e4a404a0SH. Nikolaus Schaller 	flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
9758c0984e5SSebastian Reichel 	if (flags < 0) {
9768c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading flag register:%d\n", flags);
9778c0984e5SSebastian Reichel 		return flags;
9788c0984e5SSebastian Reichel 	}
9798c0984e5SSebastian Reichel 
9808c0984e5SSebastian Reichel 	/* Unlikely but important to return first */
9818c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_overtemp(di, flags)))
9828c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_OVERHEAT;
9838c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_undertemp(di, flags)))
9848c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_COLD;
9858c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_dead(di, flags)))
9868c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_DEAD;
9878c0984e5SSebastian Reichel 
9888c0984e5SSebastian Reichel 	return POWER_SUPPLY_HEALTH_GOOD;
9898c0984e5SSebastian Reichel }
9908c0984e5SSebastian Reichel 
9918c0984e5SSebastian Reichel void bq27xxx_battery_update(struct bq27xxx_device_info *di)
9928c0984e5SSebastian Reichel {
9938c0984e5SSebastian Reichel 	struct bq27xxx_reg_cache cache = {0, };
9948c0984e5SSebastian Reichel 	bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
9958c0984e5SSebastian Reichel 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
9968c0984e5SSebastian Reichel 
9978c0984e5SSebastian Reichel 	cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
9988c0984e5SSebastian Reichel 	if ((cache.flags & 0xff) == 0xff)
9998c0984e5SSebastian Reichel 		cache.flags = -1; /* read error */
10008c0984e5SSebastian Reichel 	if (cache.flags >= 0) {
10018c0984e5SSebastian Reichel 		cache.temperature = bq27xxx_battery_read_temperature(di);
10028c0984e5SSebastian Reichel 		if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
10038c0984e5SSebastian Reichel 			dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
10048c0984e5SSebastian Reichel 			cache.capacity = -ENODATA;
10058c0984e5SSebastian Reichel 			cache.energy = -ENODATA;
10068c0984e5SSebastian Reichel 			cache.time_to_empty = -ENODATA;
10078c0984e5SSebastian Reichel 			cache.time_to_empty_avg = -ENODATA;
10088c0984e5SSebastian Reichel 			cache.time_to_full = -ENODATA;
10098c0984e5SSebastian Reichel 			cache.charge_full = -ENODATA;
10108c0984e5SSebastian Reichel 			cache.health = -ENODATA;
10118c0984e5SSebastian Reichel 		} else {
10128c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
10138c0984e5SSebastian Reichel 				cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
10148c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
10158c0984e5SSebastian Reichel 				cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
10168c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
10178c0984e5SSebastian Reichel 				cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
10188c0984e5SSebastian Reichel 			cache.charge_full = bq27xxx_battery_read_fcc(di);
10198c0984e5SSebastian Reichel 			cache.capacity = bq27xxx_battery_read_soc(di);
10208c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
10218c0984e5SSebastian Reichel 				cache.energy = bq27xxx_battery_read_energy(di);
10228c0984e5SSebastian Reichel 			cache.health = bq27xxx_battery_read_health(di);
10238c0984e5SSebastian Reichel 		}
10248c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
10258c0984e5SSebastian Reichel 			cache.cycle_count = bq27xxx_battery_read_cyct(di);
10268c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
10278c0984e5SSebastian Reichel 			cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
10288c0984e5SSebastian Reichel 
10298c0984e5SSebastian Reichel 		/* We only have to read charge design full once */
10308c0984e5SSebastian Reichel 		if (di->charge_design_full <= 0)
10318c0984e5SSebastian Reichel 			di->charge_design_full = bq27xxx_battery_read_dcap(di);
10328c0984e5SSebastian Reichel 	}
10338c0984e5SSebastian Reichel 
10348c0984e5SSebastian Reichel 	if (di->cache.capacity != cache.capacity)
10358c0984e5SSebastian Reichel 		power_supply_changed(di->bat);
10368c0984e5SSebastian Reichel 
10378c0984e5SSebastian Reichel 	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
10388c0984e5SSebastian Reichel 		di->cache = cache;
10398c0984e5SSebastian Reichel 
10408c0984e5SSebastian Reichel 	di->last_update = jiffies;
10418c0984e5SSebastian Reichel }
10428c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
10438c0984e5SSebastian Reichel 
10448c0984e5SSebastian Reichel static void bq27xxx_battery_poll(struct work_struct *work)
10458c0984e5SSebastian Reichel {
10468c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di =
10478c0984e5SSebastian Reichel 			container_of(work, struct bq27xxx_device_info,
10488c0984e5SSebastian Reichel 				     work.work);
10498c0984e5SSebastian Reichel 
10508c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
10518c0984e5SSebastian Reichel 
10528c0984e5SSebastian Reichel 	if (poll_interval > 0)
10538c0984e5SSebastian Reichel 		schedule_delayed_work(&di->work, poll_interval * HZ);
10548c0984e5SSebastian Reichel }
10558c0984e5SSebastian Reichel 
10568c0984e5SSebastian Reichel /*
10578c0984e5SSebastian Reichel  * Return the battery average current in µA
10588c0984e5SSebastian Reichel  * Note that current can be negative signed as well
10598c0984e5SSebastian Reichel  * Or 0 if something fails.
10608c0984e5SSebastian Reichel  */
10618c0984e5SSebastian Reichel static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
10628c0984e5SSebastian Reichel 				   union power_supply_propval *val)
10638c0984e5SSebastian Reichel {
10648c0984e5SSebastian Reichel 	int curr;
10658c0984e5SSebastian Reichel 	int flags;
10668c0984e5SSebastian Reichel 
10678c0984e5SSebastian Reichel 	curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
10688c0984e5SSebastian Reichel 	if (curr < 0) {
10698c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading current\n");
10708c0984e5SSebastian Reichel 		return curr;
10718c0984e5SSebastian Reichel 	}
10728c0984e5SSebastian Reichel 
10738c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
1074e4a404a0SH. Nikolaus Schaller 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
10758c0984e5SSebastian Reichel 		if (flags & BQ27000_FLAG_CHGS) {
10768c0984e5SSebastian Reichel 			dev_dbg(di->dev, "negative current!\n");
10778c0984e5SSebastian Reichel 			curr = -curr;
10788c0984e5SSebastian Reichel 		}
10798c0984e5SSebastian Reichel 
10808c0984e5SSebastian Reichel 		val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
10818c0984e5SSebastian Reichel 	} else {
10828c0984e5SSebastian Reichel 		/* Other gauges return signed value */
10838c0984e5SSebastian Reichel 		val->intval = (int)((s16)curr) * 1000;
10848c0984e5SSebastian Reichel 	}
10858c0984e5SSebastian Reichel 
10868c0984e5SSebastian Reichel 	return 0;
10878c0984e5SSebastian Reichel }
10888c0984e5SSebastian Reichel 
10898c0984e5SSebastian Reichel static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
10908c0984e5SSebastian Reichel 				  union power_supply_propval *val)
10918c0984e5SSebastian Reichel {
10928c0984e5SSebastian Reichel 	int status;
10938c0984e5SSebastian Reichel 
10948c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
10958c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
10968c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
10978c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_CHGS)
10988c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
10998c0984e5SSebastian Reichel 		else if (power_supply_am_i_supplied(di->bat))
11008c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
11018c0984e5SSebastian Reichel 		else
11028c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
11038c0984e5SSebastian Reichel 	} else {
11048c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
11058c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
11068c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_DSC)
11078c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
11088c0984e5SSebastian Reichel 		else
11098c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
11108c0984e5SSebastian Reichel 	}
11118c0984e5SSebastian Reichel 
11128c0984e5SSebastian Reichel 	val->intval = status;
11138c0984e5SSebastian Reichel 
11148c0984e5SSebastian Reichel 	return 0;
11158c0984e5SSebastian Reichel }
11168c0984e5SSebastian Reichel 
11178c0984e5SSebastian Reichel static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
11188c0984e5SSebastian Reichel 					  union power_supply_propval *val)
11198c0984e5SSebastian Reichel {
11208c0984e5SSebastian Reichel 	int level;
11218c0984e5SSebastian Reichel 
11228c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
11238c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
11248c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
11258c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDV1)
11268c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
11278c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDVF)
11288c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
11298c0984e5SSebastian Reichel 		else
11308c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
11318c0984e5SSebastian Reichel 	} else {
11328c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
11338c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
11348c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
11358c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
11368c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
11378c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
11388c0984e5SSebastian Reichel 		else
11398c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
11408c0984e5SSebastian Reichel 	}
11418c0984e5SSebastian Reichel 
11428c0984e5SSebastian Reichel 	val->intval = level;
11438c0984e5SSebastian Reichel 
11448c0984e5SSebastian Reichel 	return 0;
11458c0984e5SSebastian Reichel }
11468c0984e5SSebastian Reichel 
11478c0984e5SSebastian Reichel /*
11488c0984e5SSebastian Reichel  * Return the battery Voltage in millivolts
11498c0984e5SSebastian Reichel  * Or < 0 if something fails.
11508c0984e5SSebastian Reichel  */
11518c0984e5SSebastian Reichel static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
11528c0984e5SSebastian Reichel 				   union power_supply_propval *val)
11538c0984e5SSebastian Reichel {
11548c0984e5SSebastian Reichel 	int volt;
11558c0984e5SSebastian Reichel 
11568c0984e5SSebastian Reichel 	volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
11578c0984e5SSebastian Reichel 	if (volt < 0) {
11588c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading voltage\n");
11598c0984e5SSebastian Reichel 		return volt;
11608c0984e5SSebastian Reichel 	}
11618c0984e5SSebastian Reichel 
11628c0984e5SSebastian Reichel 	val->intval = volt * 1000;
11638c0984e5SSebastian Reichel 
11648c0984e5SSebastian Reichel 	return 0;
11658c0984e5SSebastian Reichel }
11668c0984e5SSebastian Reichel 
11678c0984e5SSebastian Reichel static int bq27xxx_simple_value(int value,
11688c0984e5SSebastian Reichel 				union power_supply_propval *val)
11698c0984e5SSebastian Reichel {
11708c0984e5SSebastian Reichel 	if (value < 0)
11718c0984e5SSebastian Reichel 		return value;
11728c0984e5SSebastian Reichel 
11738c0984e5SSebastian Reichel 	val->intval = value;
11748c0984e5SSebastian Reichel 
11758c0984e5SSebastian Reichel 	return 0;
11768c0984e5SSebastian Reichel }
11778c0984e5SSebastian Reichel 
11788c0984e5SSebastian Reichel static int bq27xxx_battery_get_property(struct power_supply *psy,
11798c0984e5SSebastian Reichel 					enum power_supply_property psp,
11808c0984e5SSebastian Reichel 					union power_supply_propval *val)
11818c0984e5SSebastian Reichel {
11828c0984e5SSebastian Reichel 	int ret = 0;
11838c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
11848c0984e5SSebastian Reichel 
11858c0984e5SSebastian Reichel 	mutex_lock(&di->lock);
11868c0984e5SSebastian Reichel 	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
11878c0984e5SSebastian Reichel 		cancel_delayed_work_sync(&di->work);
11888c0984e5SSebastian Reichel 		bq27xxx_battery_poll(&di->work.work);
11898c0984e5SSebastian Reichel 	}
11908c0984e5SSebastian Reichel 	mutex_unlock(&di->lock);
11918c0984e5SSebastian Reichel 
11928c0984e5SSebastian Reichel 	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
11938c0984e5SSebastian Reichel 		return -ENODEV;
11948c0984e5SSebastian Reichel 
11958c0984e5SSebastian Reichel 	switch (psp) {
11968c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
11978c0984e5SSebastian Reichel 		ret = bq27xxx_battery_status(di, val);
11988c0984e5SSebastian Reichel 		break;
11998c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
12008c0984e5SSebastian Reichel 		ret = bq27xxx_battery_voltage(di, val);
12018c0984e5SSebastian Reichel 		break;
12028c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
12038c0984e5SSebastian Reichel 		val->intval = di->cache.flags < 0 ? 0 : 1;
12048c0984e5SSebastian Reichel 		break;
12058c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
12068c0984e5SSebastian Reichel 		ret = bq27xxx_battery_current(di, val);
12078c0984e5SSebastian Reichel 		break;
12088c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
12098c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.capacity, val);
12108c0984e5SSebastian Reichel 		break;
12118c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
12128c0984e5SSebastian Reichel 		ret = bq27xxx_battery_capacity_level(di, val);
12138c0984e5SSebastian Reichel 		break;
12148c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
12158c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.temperature, val);
12168c0984e5SSebastian Reichel 		if (ret == 0)
12178c0984e5SSebastian Reichel 			val->intval -= 2731; /* convert decidegree k to c */
12188c0984e5SSebastian Reichel 		break;
12198c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
12208c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
12218c0984e5SSebastian Reichel 		break;
12228c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
12238c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
12248c0984e5SSebastian Reichel 		break;
12258c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
12268c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
12278c0984e5SSebastian Reichel 		break;
12288c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
12298c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
12308c0984e5SSebastian Reichel 		break;
12318c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
12328c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
12338c0984e5SSebastian Reichel 		break;
12348c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
12358c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.charge_full, val);
12368c0984e5SSebastian Reichel 		break;
12378c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
12388c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->charge_design_full, val);
12398c0984e5SSebastian Reichel 		break;
12408c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
12418c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
12428c0984e5SSebastian Reichel 		break;
12438c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ENERGY_NOW:
12448c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.energy, val);
12458c0984e5SSebastian Reichel 		break;
12468c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_POWER_AVG:
12478c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.power_avg, val);
12488c0984e5SSebastian Reichel 		break;
12498c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
12508c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.health, val);
12518c0984e5SSebastian Reichel 		break;
12528c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
12538c0984e5SSebastian Reichel 		val->strval = BQ27XXX_MANUFACTURER;
12548c0984e5SSebastian Reichel 		break;
12558c0984e5SSebastian Reichel 	default:
12568c0984e5SSebastian Reichel 		return -EINVAL;
12578c0984e5SSebastian Reichel 	}
12588c0984e5SSebastian Reichel 
12598c0984e5SSebastian Reichel 	return ret;
12608c0984e5SSebastian Reichel }
12618c0984e5SSebastian Reichel 
12628c0984e5SSebastian Reichel static void bq27xxx_external_power_changed(struct power_supply *psy)
12638c0984e5SSebastian Reichel {
12648c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
12658c0984e5SSebastian Reichel 
12668c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
12678c0984e5SSebastian Reichel 	schedule_delayed_work(&di->work, 0);
12688c0984e5SSebastian Reichel }
12698c0984e5SSebastian Reichel 
12708c0984e5SSebastian Reichel int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
12718c0984e5SSebastian Reichel {
12728c0984e5SSebastian Reichel 	struct power_supply_desc *psy_desc;
12738c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = { .drv_data = di, };
12748c0984e5SSebastian Reichel 
12758c0984e5SSebastian Reichel 	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
12768c0984e5SSebastian Reichel 	mutex_init(&di->lock);
12778c0984e5SSebastian Reichel 	di->regs = bq27xxx_regs[di->chip];
12788c0984e5SSebastian Reichel 
12798c0984e5SSebastian Reichel 	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
12808c0984e5SSebastian Reichel 	if (!psy_desc)
12818c0984e5SSebastian Reichel 		return -ENOMEM;
12828c0984e5SSebastian Reichel 
12838c0984e5SSebastian Reichel 	psy_desc->name = di->name;
12848c0984e5SSebastian Reichel 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
12858c0984e5SSebastian Reichel 	psy_desc->properties = bq27xxx_battery_props[di->chip].props;
12868c0984e5SSebastian Reichel 	psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
12878c0984e5SSebastian Reichel 	psy_desc->get_property = bq27xxx_battery_get_property;
12888c0984e5SSebastian Reichel 	psy_desc->external_power_changed = bq27xxx_external_power_changed;
12898c0984e5SSebastian Reichel 
12908c0984e5SSebastian Reichel 	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
12918c0984e5SSebastian Reichel 	if (IS_ERR(di->bat)) {
12928c0984e5SSebastian Reichel 		dev_err(di->dev, "failed to register battery\n");
12938c0984e5SSebastian Reichel 		return PTR_ERR(di->bat);
12948c0984e5SSebastian Reichel 	}
12958c0984e5SSebastian Reichel 
12968c0984e5SSebastian Reichel 	dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
12978c0984e5SSebastian Reichel 
12988c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
12998c0984e5SSebastian Reichel 
13001d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
13011d72706fSMatt Ranostay 	list_add(&di->list, &bq27xxx_battery_devices);
13021d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
13031d72706fSMatt Ranostay 
13048c0984e5SSebastian Reichel 	return 0;
13058c0984e5SSebastian Reichel }
13068c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
13078c0984e5SSebastian Reichel 
13088c0984e5SSebastian Reichel void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
13098c0984e5SSebastian Reichel {
13108c0984e5SSebastian Reichel 	/*
13118c0984e5SSebastian Reichel 	 * power_supply_unregister call bq27xxx_battery_get_property which
13128c0984e5SSebastian Reichel 	 * call bq27xxx_battery_poll.
13138c0984e5SSebastian Reichel 	 * Make sure that bq27xxx_battery_poll will not call
13148c0984e5SSebastian Reichel 	 * schedule_delayed_work again after unregister (which cause OOPS).
13158c0984e5SSebastian Reichel 	 */
13168c0984e5SSebastian Reichel 	poll_interval = 0;
13178c0984e5SSebastian Reichel 
13188c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
13198c0984e5SSebastian Reichel 
13208c0984e5SSebastian Reichel 	power_supply_unregister(di->bat);
13218c0984e5SSebastian Reichel 
13221d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
13231d72706fSMatt Ranostay 	list_del(&di->list);
13241d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
13251d72706fSMatt Ranostay 
13268c0984e5SSebastian Reichel 	mutex_destroy(&di->lock);
13278c0984e5SSebastian Reichel }
13288c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
13298c0984e5SSebastian Reichel 
13308c0984e5SSebastian Reichel static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
13318c0984e5SSebastian Reichel 					 bool single)
13328c0984e5SSebastian Reichel {
13338c0984e5SSebastian Reichel 	struct device *dev = di->dev;
13348c0984e5SSebastian Reichel 	struct bq27xxx_platform_data *pdata = dev->platform_data;
13358c0984e5SSebastian Reichel 	unsigned int timeout = 3;
13368c0984e5SSebastian Reichel 	int upper, lower;
13378c0984e5SSebastian Reichel 	int temp;
13388c0984e5SSebastian Reichel 
13398c0984e5SSebastian Reichel 	if (!single) {
13408c0984e5SSebastian Reichel 		/* Make sure the value has not changed in between reading the
13418c0984e5SSebastian Reichel 		 * lower and the upper part */
13428c0984e5SSebastian Reichel 		upper = pdata->read(dev, reg + 1);
13438c0984e5SSebastian Reichel 		do {
13448c0984e5SSebastian Reichel 			temp = upper;
13458c0984e5SSebastian Reichel 			if (upper < 0)
13468c0984e5SSebastian Reichel 				return upper;
13478c0984e5SSebastian Reichel 
13488c0984e5SSebastian Reichel 			lower = pdata->read(dev, reg);
13498c0984e5SSebastian Reichel 			if (lower < 0)
13508c0984e5SSebastian Reichel 				return lower;
13518c0984e5SSebastian Reichel 
13528c0984e5SSebastian Reichel 			upper = pdata->read(dev, reg + 1);
13538c0984e5SSebastian Reichel 		} while (temp != upper && --timeout);
13548c0984e5SSebastian Reichel 
13558c0984e5SSebastian Reichel 		if (timeout == 0)
13568c0984e5SSebastian Reichel 			return -EIO;
13578c0984e5SSebastian Reichel 
13588c0984e5SSebastian Reichel 		return (upper << 8) | lower;
13598c0984e5SSebastian Reichel 	}
13608c0984e5SSebastian Reichel 
13618c0984e5SSebastian Reichel 	return pdata->read(dev, reg);
13628c0984e5SSebastian Reichel }
13638c0984e5SSebastian Reichel 
13648c0984e5SSebastian Reichel static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
13658c0984e5SSebastian Reichel {
13668c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di;
13678c0984e5SSebastian Reichel 	struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
13688c0984e5SSebastian Reichel 
13698c0984e5SSebastian Reichel 	if (!pdata) {
13708c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no platform_data supplied\n");
13718c0984e5SSebastian Reichel 		return -EINVAL;
13728c0984e5SSebastian Reichel 	}
13738c0984e5SSebastian Reichel 
13748c0984e5SSebastian Reichel 	if (!pdata->read) {
13758c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no hdq read callback supplied\n");
13768c0984e5SSebastian Reichel 		return -EINVAL;
13778c0984e5SSebastian Reichel 	}
13788c0984e5SSebastian Reichel 
13798c0984e5SSebastian Reichel 	if (!pdata->chip) {
13808c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no device supplied\n");
13818c0984e5SSebastian Reichel 		return -EINVAL;
13828c0984e5SSebastian Reichel 	}
13838c0984e5SSebastian Reichel 
13848c0984e5SSebastian Reichel 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
13858c0984e5SSebastian Reichel 	if (!di)
13868c0984e5SSebastian Reichel 		return -ENOMEM;
13878c0984e5SSebastian Reichel 
13888c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, di);
13898c0984e5SSebastian Reichel 
13908c0984e5SSebastian Reichel 	di->dev = &pdev->dev;
13918c0984e5SSebastian Reichel 	di->chip = pdata->chip;
13928c0984e5SSebastian Reichel 	di->name = pdata->name ?: dev_name(&pdev->dev);
13938c0984e5SSebastian Reichel 	di->bus.read = bq27xxx_battery_platform_read;
13948c0984e5SSebastian Reichel 
13958c0984e5SSebastian Reichel 	return bq27xxx_battery_setup(di);
13968c0984e5SSebastian Reichel }
13978c0984e5SSebastian Reichel 
13988c0984e5SSebastian Reichel static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
13998c0984e5SSebastian Reichel {
14008c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
14018c0984e5SSebastian Reichel 
14028c0984e5SSebastian Reichel 	bq27xxx_battery_teardown(di);
14038c0984e5SSebastian Reichel 
14048c0984e5SSebastian Reichel 	return 0;
14058c0984e5SSebastian Reichel }
14068c0984e5SSebastian Reichel 
14078c0984e5SSebastian Reichel static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
14088c0984e5SSebastian Reichel 	{ "bq27000-battery", },
14098c0984e5SSebastian Reichel 	{ /* sentinel */ }
14108c0984e5SSebastian Reichel };
14118c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
14128c0984e5SSebastian Reichel 
14138c0984e5SSebastian Reichel #ifdef CONFIG_OF
14148c0984e5SSebastian Reichel static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
14158c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27000" },
14168c0984e5SSebastian Reichel 	{},
14178c0984e5SSebastian Reichel };
14188c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
14198c0984e5SSebastian Reichel #endif
14208c0984e5SSebastian Reichel 
14218c0984e5SSebastian Reichel static struct platform_driver bq27xxx_battery_platform_driver = {
14228c0984e5SSebastian Reichel 	.probe	= bq27xxx_battery_platform_probe,
14238c0984e5SSebastian Reichel 	.remove = bq27xxx_battery_platform_remove,
14248c0984e5SSebastian Reichel 	.driver = {
14258c0984e5SSebastian Reichel 		.name = "bq27000-battery",
14268c0984e5SSebastian Reichel 		.of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
14278c0984e5SSebastian Reichel 	},
14288c0984e5SSebastian Reichel 	.id_table = bq27xxx_battery_platform_id_table,
14298c0984e5SSebastian Reichel };
14308c0984e5SSebastian Reichel module_platform_driver(bq27xxx_battery_platform_driver);
14318c0984e5SSebastian Reichel 
14328c0984e5SSebastian Reichel MODULE_ALIAS("platform:bq27000-battery");
14338c0984e5SSebastian Reichel 
14348c0984e5SSebastian Reichel MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
14358c0984e5SSebastian Reichel MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
14368c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
1437