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
298c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27530-g1
308c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27531-g1
318c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27541-g1
328c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27542-g1
338c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27546-g1
348c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27742-g1
358c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27545-g1
368c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27421-g1
378c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27425-g1
388c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27411-g1
398c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27621-g1
408c0984e5SSebastian Reichel  */
418c0984e5SSebastian Reichel 
428c0984e5SSebastian Reichel #include <linux/device.h>
438c0984e5SSebastian Reichel #include <linux/module.h>
441d72706fSMatt Ranostay #include <linux/mutex.h>
458c0984e5SSebastian Reichel #include <linux/param.h>
468c0984e5SSebastian Reichel #include <linux/jiffies.h>
478c0984e5SSebastian Reichel #include <linux/workqueue.h>
488c0984e5SSebastian Reichel #include <linux/delay.h>
498c0984e5SSebastian Reichel #include <linux/platform_device.h>
508c0984e5SSebastian Reichel #include <linux/power_supply.h>
518c0984e5SSebastian Reichel #include <linux/slab.h>
528c0984e5SSebastian Reichel #include <linux/of.h>
538c0984e5SSebastian Reichel 
548c0984e5SSebastian Reichel #include <linux/power/bq27xxx_battery.h>
558c0984e5SSebastian Reichel 
568c0984e5SSebastian Reichel #define DRIVER_VERSION		"1.2.0"
578c0984e5SSebastian Reichel 
588c0984e5SSebastian Reichel #define BQ27XXX_MANUFACTURER	"Texas Instruments"
598c0984e5SSebastian Reichel 
608c0984e5SSebastian Reichel /* BQ27XXX Flags */
618c0984e5SSebastian Reichel #define BQ27XXX_FLAG_DSC	BIT(0)
628c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOCF	BIT(1) /* State-of-Charge threshold final */
638c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOC1	BIT(2) /* State-of-Charge threshold 1 */
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 
778c0984e5SSebastian Reichel #define BQ27XXX_RS			(20) /* Resistor sense mOhm */
788c0984e5SSebastian Reichel #define BQ27XXX_POWER_CONSTANT		(29200) /* 29.2 µV^2 * 1000 */
798c0984e5SSebastian Reichel #define BQ27XXX_CURRENT_CONSTANT	(3570) /* 3.57 µV * 1000 */
808c0984e5SSebastian Reichel 
818c0984e5SSebastian Reichel #define INVALID_REG_ADDR	0xff
828c0984e5SSebastian Reichel 
838c0984e5SSebastian Reichel /*
848c0984e5SSebastian Reichel  * bq27xxx_reg_index - Register names
858c0984e5SSebastian Reichel  *
868c0984e5SSebastian Reichel  * These are indexes into a device's register mapping array.
878c0984e5SSebastian Reichel  */
888c0984e5SSebastian Reichel 
898c0984e5SSebastian Reichel enum bq27xxx_reg_index {
908c0984e5SSebastian Reichel 	BQ27XXX_REG_CTRL = 0,	/* Control */
918c0984e5SSebastian Reichel 	BQ27XXX_REG_TEMP,	/* Temperature */
928c0984e5SSebastian Reichel 	BQ27XXX_REG_INT_TEMP,	/* Internal Temperature */
938c0984e5SSebastian Reichel 	BQ27XXX_REG_VOLT,	/* Voltage */
948c0984e5SSebastian Reichel 	BQ27XXX_REG_AI,		/* Average Current */
958c0984e5SSebastian Reichel 	BQ27XXX_REG_FLAGS,	/* Flags */
968c0984e5SSebastian Reichel 	BQ27XXX_REG_TTE,	/* Time-to-Empty */
978c0984e5SSebastian Reichel 	BQ27XXX_REG_TTF,	/* Time-to-Full */
988c0984e5SSebastian Reichel 	BQ27XXX_REG_TTES,	/* Time-to-Empty Standby */
998c0984e5SSebastian Reichel 	BQ27XXX_REG_TTECP,	/* Time-to-Empty at Constant Power */
1008c0984e5SSebastian Reichel 	BQ27XXX_REG_NAC,	/* Nominal Available Capacity */
1018c0984e5SSebastian Reichel 	BQ27XXX_REG_FCC,	/* Full Charge Capacity */
1028c0984e5SSebastian Reichel 	BQ27XXX_REG_CYCT,	/* Cycle Count */
1038c0984e5SSebastian Reichel 	BQ27XXX_REG_AE,		/* Available Energy */
1048c0984e5SSebastian Reichel 	BQ27XXX_REG_SOC,	/* State-of-Charge */
1058c0984e5SSebastian Reichel 	BQ27XXX_REG_DCAP,	/* Design Capacity */
1068c0984e5SSebastian Reichel 	BQ27XXX_REG_AP,		/* Average Power */
1078c0984e5SSebastian Reichel 	BQ27XXX_REG_MAX,	/* sentinel */
1088c0984e5SSebastian Reichel };
1098c0984e5SSebastian Reichel 
1108c0984e5SSebastian Reichel /* Register mappings */
1118c0984e5SSebastian Reichel static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = {
1128c0984e5SSebastian Reichel 	[BQ27000] = {
1138c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1148c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1158c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1168c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1178c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1188c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1198c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1208c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1218c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1228c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1238c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1248c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1258c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1268c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = 0x22,
1278c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1288c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1298c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
1308c0984e5SSebastian Reichel 	},
1318c0984e5SSebastian Reichel 	[BQ27010] = {
1328c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1338c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1348c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1358c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1368c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1378c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1388c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1398c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1408c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1418c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1428c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1438c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1448c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1458c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1468c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1478c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1488c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1498c0984e5SSebastian Reichel 	},
150818e3012SChris Lapa 	[BQ2750X] = {
1518c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1528c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1538c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
1548c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1558c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1568c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1578c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1588c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1598c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1a,
1608c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1618c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1628c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1638c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1648c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1658c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
1668c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
1678c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1688c0984e5SSebastian Reichel 	},
1696da6e4bdSChris Lapa 	[BQ2751X] = {
1703bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_CTRL] = 0x00,
1713bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TEMP] = 0x06,
1723bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_INT_TEMP] = 0x28,
1733bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_VOLT] = 0x08,
1743bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_AI] = 0x14,
1753bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_FLAGS] = 0x0a,
1763bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTE] = 0x16,
1773bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1783bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTES] = 0x1a,
1793bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1803bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_NAC] = 0x0c,
1813bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_FCC] = 0x12,
1823bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_CYCT] = 0x1e,
1833bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1843bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_SOC] = 0x20,
1853bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_DCAP] = 0x2e,
1863bee9ea1SAndrew F. Davis 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1873bee9ea1SAndrew F. Davis 	},
18832833635SChris Lapa 	[BQ27500] = {
18932833635SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
19032833635SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
19132833635SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
19232833635SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
19332833635SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
19432833635SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
19532833635SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
19632833635SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
19732833635SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
19832833635SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
19932833635SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
20032833635SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
20132833635SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
20232833635SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
20332833635SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
20432833635SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
20532833635SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
20632833635SChris Lapa 	},
207bd28177fSChris Lapa 	[BQ27510G1] = {
208bd28177fSChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
209bd28177fSChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
210bd28177fSChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
211bd28177fSChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
212bd28177fSChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
213bd28177fSChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
214bd28177fSChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
215bd28177fSChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
216bd28177fSChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
217bd28177fSChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
218bd28177fSChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
219bd28177fSChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
220bd28177fSChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
221bd28177fSChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
222bd28177fSChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
223bd28177fSChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
224bd28177fSChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
225bd28177fSChris Lapa 	},
226698a2bf5SChris Lapa 	[BQ27510G2] = {
227698a2bf5SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
228698a2bf5SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
229698a2bf5SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
230698a2bf5SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
231698a2bf5SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
232698a2bf5SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
233698a2bf5SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
234698a2bf5SChris Lapa 		[BQ27XXX_REG_TTF] = 0x18,
235698a2bf5SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1c,
236698a2bf5SChris Lapa 		[BQ27XXX_REG_TTECP] = 0x26,
237698a2bf5SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
238698a2bf5SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
239698a2bf5SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x2a,
240698a2bf5SChris Lapa 		[BQ27XXX_REG_AE] = 0x22,
241698a2bf5SChris Lapa 		[BQ27XXX_REG_SOC] = 0x2c,
242698a2bf5SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x3c,
243698a2bf5SChris Lapa 		[BQ27XXX_REG_AP] = 0x24,
244698a2bf5SChris Lapa 	},
24571375aa7SChris Lapa 	[BQ27510G3] = {
24671375aa7SChris Lapa 		[BQ27XXX_REG_CTRL] = 0x00,
24771375aa7SChris Lapa 		[BQ27XXX_REG_TEMP] = 0x06,
24871375aa7SChris Lapa 		[BQ27XXX_REG_INT_TEMP] = 0x28,
24971375aa7SChris Lapa 		[BQ27XXX_REG_VOLT] = 0x08,
25071375aa7SChris Lapa 		[BQ27XXX_REG_AI] = 0x14,
25171375aa7SChris Lapa 		[BQ27XXX_REG_FLAGS] = 0x0a,
25271375aa7SChris Lapa 		[BQ27XXX_REG_TTE] = 0x16,
25371375aa7SChris Lapa 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
25471375aa7SChris Lapa 		[BQ27XXX_REG_TTES] = 0x1a,
25571375aa7SChris Lapa 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
25671375aa7SChris Lapa 		[BQ27XXX_REG_NAC] = 0x0c,
25771375aa7SChris Lapa 		[BQ27XXX_REG_FCC] = 0x12,
25871375aa7SChris Lapa 		[BQ27XXX_REG_CYCT] = 0x1e,
25971375aa7SChris Lapa 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
26071375aa7SChris Lapa 		[BQ27XXX_REG_SOC] = 0x20,
26171375aa7SChris Lapa 		[BQ27XXX_REG_DCAP] = 0x2e,
26271375aa7SChris Lapa 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
26371375aa7SChris Lapa 	},
2648c0984e5SSebastian Reichel 	[BQ27530] = {
2658c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
2668c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
2678c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x32,
2688c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
2698c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
2708c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
2718c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
2728c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
2738c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
2748c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
2758c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
2768c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
2778c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
2788c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
2798c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
2808c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
2818c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
2828c0984e5SSebastian Reichel 	},
2838c0984e5SSebastian Reichel 	[BQ27541] = {
2848c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
2858c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
2868c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
2878c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
2888c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
2898c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
2908c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
2918c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
2928c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
2938c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
2948c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
2958c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
2968c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
2978c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
2988c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
2998c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
3008c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
3018c0984e5SSebastian Reichel 	},
3028c0984e5SSebastian Reichel 	[BQ27545] = {
3038c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3048c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
3058c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
3068c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
3078c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
3088c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
3098c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
3108c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3118c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3128c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3138c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
3148c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
3158c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
3168c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3178c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
3188c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
3198c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
3208c0984e5SSebastian Reichel 	},
3218c0984e5SSebastian Reichel 	[BQ27421] = {
3228c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
3238c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x02,
3248c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x1e,
3258c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x04,
3268c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x10,
3278c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x06,
3288c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
3298c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
3308c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
3318c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
3328c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x08,
3338c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x0e,
3348c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
3358c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
3368c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x1c,
3378c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
3388c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x18,
3398c0984e5SSebastian Reichel 	},
3408c0984e5SSebastian Reichel };
3418c0984e5SSebastian Reichel 
3428c0984e5SSebastian Reichel static enum power_supply_property bq27000_battery_props[] = {
3438c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3448c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
3458c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3468c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3478c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
3488c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3498c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3508c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
3518c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
3528c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
3538c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
3548c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
3558c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
3568c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3578c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
3588c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ENERGY_NOW,
3598c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
3608c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
3618c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
3628c0984e5SSebastian Reichel };
3638c0984e5SSebastian Reichel 
3648c0984e5SSebastian Reichel static enum power_supply_property bq27010_battery_props[] = {
3658c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3668c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
3678c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3688c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3698c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
3708c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3718c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3728c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
3738c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
3748c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
3758c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
3768c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
3778c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
3788c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3798c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
3808c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
3818c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
3828c0984e5SSebastian Reichel };
3838c0984e5SSebastian Reichel 
384818e3012SChris Lapa static enum power_supply_property bq2750x_battery_props[] = {
3858c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3868c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
3878c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3888c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3898c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
3908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3918c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3928c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_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_HEALTH,
3998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
4008c0984e5SSebastian Reichel };
4018c0984e5SSebastian Reichel 
4026da6e4bdSChris Lapa static enum power_supply_property bq2751x_battery_props[] = {
4033bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_STATUS,
4043bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_PRESENT,
4053bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
4063bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CURRENT_NOW,
4073bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CAPACITY,
4083bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
4093bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_TEMP,
4103bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4113bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_TECHNOLOGY,
4123bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CHARGE_FULL,
4133bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CHARGE_NOW,
4143bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
4153bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_CYCLE_COUNT,
4163bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_HEALTH,
4173bee9ea1SAndrew F. Davis 	POWER_SUPPLY_PROP_MANUFACTURER,
4183bee9ea1SAndrew F. Davis };
4193bee9ea1SAndrew F. Davis 
42032833635SChris Lapa static enum power_supply_property bq27500_battery_props[] = {
42132833635SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
42232833635SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
42332833635SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
42432833635SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
42532833635SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
42632833635SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
42732833635SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
42832833635SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
42932833635SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
43032833635SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
43132833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
43232833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
43332833635SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
43432833635SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
43532833635SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
43632833635SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
43732833635SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
43832833635SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
43932833635SChris Lapa };
44032833635SChris Lapa 
441bd28177fSChris Lapa static enum power_supply_property bq27510g1_battery_props[] = {
442bd28177fSChris Lapa 	POWER_SUPPLY_PROP_STATUS,
443bd28177fSChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
444bd28177fSChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
445bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
446bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
447bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
448bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TEMP,
449bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
450bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
451bd28177fSChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
452bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
453bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
454bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
455bd28177fSChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
456bd28177fSChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
457bd28177fSChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
458bd28177fSChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
459bd28177fSChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
460bd28177fSChris Lapa };
461bd28177fSChris Lapa 
462698a2bf5SChris Lapa static enum power_supply_property bq27510g2_battery_props[] = {
463698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
464698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
465698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
466698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
467698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
468698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
469698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
470698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
471698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
472698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
473698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
474698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
475698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
476698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
477698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_ENERGY_NOW,
478698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_POWER_AVG,
479698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
480698a2bf5SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
481698a2bf5SChris Lapa };
482698a2bf5SChris Lapa 
48371375aa7SChris Lapa static enum power_supply_property bq27510g3_battery_props[] = {
48471375aa7SChris Lapa 	POWER_SUPPLY_PROP_STATUS,
48571375aa7SChris Lapa 	POWER_SUPPLY_PROP_PRESENT,
48671375aa7SChris Lapa 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
48771375aa7SChris Lapa 	POWER_SUPPLY_PROP_CURRENT_NOW,
48871375aa7SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY,
48971375aa7SChris Lapa 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
49071375aa7SChris Lapa 	POWER_SUPPLY_PROP_TEMP,
49171375aa7SChris Lapa 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
49271375aa7SChris Lapa 	POWER_SUPPLY_PROP_TECHNOLOGY,
49371375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL,
49471375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_NOW,
49571375aa7SChris Lapa 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
49671375aa7SChris Lapa 	POWER_SUPPLY_PROP_CYCLE_COUNT,
49771375aa7SChris Lapa 	POWER_SUPPLY_PROP_HEALTH,
49871375aa7SChris Lapa 	POWER_SUPPLY_PROP_MANUFACTURER,
49971375aa7SChris Lapa };
50071375aa7SChris Lapa 
5018c0984e5SSebastian Reichel static enum power_supply_property bq27530_battery_props[] = {
5028c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
5038c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
5048c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
5058c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
5068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
5078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
5088c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
5098c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
5108c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
5118c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
5128c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
5138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
5148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
5158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
5168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5178c0984e5SSebastian Reichel };
5188c0984e5SSebastian Reichel 
5198c0984e5SSebastian Reichel static enum power_supply_property bq27541_battery_props[] = {
5208c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
5218c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
5228c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
5238c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
5248c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
5258c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
5268c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
5278c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
5288c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
5298c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
5308c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
5318c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
5328c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
5338c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
5348c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
5358c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5368c0984e5SSebastian Reichel };
5378c0984e5SSebastian Reichel 
5388c0984e5SSebastian Reichel static enum power_supply_property bq27545_battery_props[] = {
5398c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
5408c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
5418c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
5428c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
5438c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
5448c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
5458c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
5468c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
5478c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
5488c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
5498c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
5508c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
5518c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
5528c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
5538c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5548c0984e5SSebastian Reichel };
5558c0984e5SSebastian Reichel 
5568c0984e5SSebastian Reichel static enum power_supply_property bq27421_battery_props[] = {
5578c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
5588c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
5598c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
5608c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
5618c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
5628c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
5638c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
5648c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
5658c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
5668c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
5678c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
5688c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5698c0984e5SSebastian Reichel };
5708c0984e5SSebastian Reichel 
5718c0984e5SSebastian Reichel #define BQ27XXX_PROP(_id, _prop)		\
5728c0984e5SSebastian Reichel 	[_id] = {				\
5738c0984e5SSebastian Reichel 		.props = _prop,			\
5748c0984e5SSebastian Reichel 		.size = ARRAY_SIZE(_prop),	\
5758c0984e5SSebastian Reichel 	}
5768c0984e5SSebastian Reichel 
5778c0984e5SSebastian Reichel static struct {
5788c0984e5SSebastian Reichel 	enum power_supply_property *props;
5798c0984e5SSebastian Reichel 	size_t size;
5808c0984e5SSebastian Reichel } bq27xxx_battery_props[] = {
5818c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27000, bq27000_battery_props),
5828c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27010, bq27010_battery_props),
583818e3012SChris Lapa 	BQ27XXX_PROP(BQ2750X, bq2750x_battery_props),
5846da6e4bdSChris Lapa 	BQ27XXX_PROP(BQ2751X, bq2751x_battery_props),
58532833635SChris Lapa 	BQ27XXX_PROP(BQ27500, bq27500_battery_props),
586bd28177fSChris Lapa 	BQ27XXX_PROP(BQ27510G1, bq27510g1_battery_props),
587698a2bf5SChris Lapa 	BQ27XXX_PROP(BQ27510G2, bq27510g2_battery_props),
58871375aa7SChris Lapa 	BQ27XXX_PROP(BQ27510G3, bq27510g3_battery_props),
5898c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27530, bq27530_battery_props),
5908c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27541, bq27541_battery_props),
5918c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27545, bq27545_battery_props),
5928c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27421, bq27421_battery_props),
5938c0984e5SSebastian Reichel };
5948c0984e5SSebastian Reichel 
5951d72706fSMatt Ranostay static DEFINE_MUTEX(bq27xxx_list_lock);
5961d72706fSMatt Ranostay static LIST_HEAD(bq27xxx_battery_devices);
5971d72706fSMatt Ranostay 
5981d72706fSMatt Ranostay static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
5991d72706fSMatt Ranostay {
6001d72706fSMatt Ranostay 	struct bq27xxx_device_info *di;
601950b6c2dSMatt Ranostay 	unsigned int prev_val = *(unsigned int *) kp->arg;
6021d72706fSMatt Ranostay 	int ret;
6031d72706fSMatt Ranostay 
6041d72706fSMatt Ranostay 	ret = param_set_uint(val, kp);
605950b6c2dSMatt Ranostay 	if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
6061d72706fSMatt Ranostay 		return ret;
6071d72706fSMatt Ranostay 
6081d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
6091d72706fSMatt Ranostay 	list_for_each_entry(di, &bq27xxx_battery_devices, list) {
6101d72706fSMatt Ranostay 		cancel_delayed_work_sync(&di->work);
6111d72706fSMatt Ranostay 		schedule_delayed_work(&di->work, 0);
6121d72706fSMatt Ranostay 	}
6131d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
6141d72706fSMatt Ranostay 
6151d72706fSMatt Ranostay 	return ret;
6161d72706fSMatt Ranostay }
6171d72706fSMatt Ranostay 
6181d72706fSMatt Ranostay static const struct kernel_param_ops param_ops_poll_interval = {
6191d72706fSMatt Ranostay 	.get = param_get_uint,
6201d72706fSMatt Ranostay 	.set = poll_interval_param_set,
6211d72706fSMatt Ranostay };
6221d72706fSMatt Ranostay 
6238c0984e5SSebastian Reichel static unsigned int poll_interval = 360;
6241d72706fSMatt Ranostay module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
6258c0984e5SSebastian Reichel MODULE_PARM_DESC(poll_interval,
6268c0984e5SSebastian Reichel 		 "battery poll interval in seconds - 0 disables polling");
6278c0984e5SSebastian Reichel 
6288c0984e5SSebastian Reichel /*
6298c0984e5SSebastian Reichel  * Common code for BQ27xxx devices
6308c0984e5SSebastian Reichel  */
6318c0984e5SSebastian Reichel 
6328c0984e5SSebastian Reichel static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
6338c0984e5SSebastian Reichel 			       bool single)
6348c0984e5SSebastian Reichel {
6358c0984e5SSebastian Reichel 	/* Reports EINVAL for invalid/missing registers */
6368c0984e5SSebastian Reichel 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
6378c0984e5SSebastian Reichel 		return -EINVAL;
6388c0984e5SSebastian Reichel 
6398c0984e5SSebastian Reichel 	return di->bus.read(di, di->regs[reg_index], single);
6408c0984e5SSebastian Reichel }
6418c0984e5SSebastian Reichel 
6428c0984e5SSebastian Reichel /*
6438c0984e5SSebastian Reichel  * Return the battery State-of-Charge
6448c0984e5SSebastian Reichel  * Or < 0 if something fails.
6458c0984e5SSebastian Reichel  */
6468c0984e5SSebastian Reichel static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
6478c0984e5SSebastian Reichel {
6488c0984e5SSebastian Reichel 	int soc;
6498c0984e5SSebastian Reichel 
6508c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
6518c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
6528c0984e5SSebastian Reichel 	else
6538c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
6548c0984e5SSebastian Reichel 
6558c0984e5SSebastian Reichel 	if (soc < 0)
6568c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading State-of-Charge\n");
6578c0984e5SSebastian Reichel 
6588c0984e5SSebastian Reichel 	return soc;
6598c0984e5SSebastian Reichel }
6608c0984e5SSebastian Reichel 
6618c0984e5SSebastian Reichel /*
6628c0984e5SSebastian Reichel  * Return a battery charge value in µAh
6638c0984e5SSebastian Reichel  * Or < 0 if something fails.
6648c0984e5SSebastian Reichel  */
6658c0984e5SSebastian Reichel static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
6668c0984e5SSebastian Reichel {
6678c0984e5SSebastian Reichel 	int charge;
6688c0984e5SSebastian Reichel 
6698c0984e5SSebastian Reichel 	charge = bq27xxx_read(di, reg, false);
6708c0984e5SSebastian Reichel 	if (charge < 0) {
6718c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
6728c0984e5SSebastian Reichel 			reg, charge);
6738c0984e5SSebastian Reichel 		return charge;
6748c0984e5SSebastian Reichel 	}
6758c0984e5SSebastian Reichel 
6768c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
6778c0984e5SSebastian Reichel 		charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
6788c0984e5SSebastian Reichel 	else
6798c0984e5SSebastian Reichel 		charge *= 1000;
6808c0984e5SSebastian Reichel 
6818c0984e5SSebastian Reichel 	return charge;
6828c0984e5SSebastian Reichel }
6838c0984e5SSebastian Reichel 
6848c0984e5SSebastian Reichel /*
6858c0984e5SSebastian Reichel  * Return the battery Nominal available capacity in µAh
6868c0984e5SSebastian Reichel  * Or < 0 if something fails.
6878c0984e5SSebastian Reichel  */
6888c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
6898c0984e5SSebastian Reichel {
6908c0984e5SSebastian Reichel 	int flags;
6918c0984e5SSebastian Reichel 
6928c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
6938c0984e5SSebastian Reichel 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
6948c0984e5SSebastian Reichel 		if (flags >= 0 && (flags & BQ27000_FLAG_CI))
6958c0984e5SSebastian Reichel 			return -ENODATA;
6968c0984e5SSebastian Reichel 	}
6978c0984e5SSebastian Reichel 
6988c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
6998c0984e5SSebastian Reichel }
7008c0984e5SSebastian Reichel 
7018c0984e5SSebastian Reichel /*
7028c0984e5SSebastian Reichel  * Return the battery Full Charge Capacity in µAh
7038c0984e5SSebastian Reichel  * Or < 0 if something fails.
7048c0984e5SSebastian Reichel  */
7058c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
7068c0984e5SSebastian Reichel {
7078c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
7088c0984e5SSebastian Reichel }
7098c0984e5SSebastian Reichel 
7108c0984e5SSebastian Reichel /*
7118c0984e5SSebastian Reichel  * Return the Design Capacity in µAh
7128c0984e5SSebastian Reichel  * Or < 0 if something fails.
7138c0984e5SSebastian Reichel  */
7148c0984e5SSebastian Reichel static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
7158c0984e5SSebastian Reichel {
7168c0984e5SSebastian Reichel 	int dcap;
7178c0984e5SSebastian Reichel 
7188c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
7198c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
7208c0984e5SSebastian Reichel 	else
7218c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
7228c0984e5SSebastian Reichel 
7238c0984e5SSebastian Reichel 	if (dcap < 0) {
7248c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading initial last measured discharge\n");
7258c0984e5SSebastian Reichel 		return dcap;
7268c0984e5SSebastian Reichel 	}
7278c0984e5SSebastian Reichel 
7288c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
7298c0984e5SSebastian Reichel 		dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
7308c0984e5SSebastian Reichel 	else
7318c0984e5SSebastian Reichel 		dcap *= 1000;
7328c0984e5SSebastian Reichel 
7338c0984e5SSebastian Reichel 	return dcap;
7348c0984e5SSebastian Reichel }
7358c0984e5SSebastian Reichel 
7368c0984e5SSebastian Reichel /*
7378c0984e5SSebastian Reichel  * Return the battery Available energy in µWh
7388c0984e5SSebastian Reichel  * Or < 0 if something fails.
7398c0984e5SSebastian Reichel  */
7408c0984e5SSebastian Reichel static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
7418c0984e5SSebastian Reichel {
7428c0984e5SSebastian Reichel 	int ae;
7438c0984e5SSebastian Reichel 
7448c0984e5SSebastian Reichel 	ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
7458c0984e5SSebastian Reichel 	if (ae < 0) {
7468c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading available energy\n");
7478c0984e5SSebastian Reichel 		return ae;
7488c0984e5SSebastian Reichel 	}
7498c0984e5SSebastian Reichel 
7508c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
7518c0984e5SSebastian Reichel 		ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
7528c0984e5SSebastian Reichel 	else
7538c0984e5SSebastian Reichel 		ae *= 1000;
7548c0984e5SSebastian Reichel 
7558c0984e5SSebastian Reichel 	return ae;
7568c0984e5SSebastian Reichel }
7578c0984e5SSebastian Reichel 
7588c0984e5SSebastian Reichel /*
7598c0984e5SSebastian Reichel  * Return the battery temperature in tenths of degree Kelvin
7608c0984e5SSebastian Reichel  * Or < 0 if something fails.
7618c0984e5SSebastian Reichel  */
7628c0984e5SSebastian Reichel static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
7638c0984e5SSebastian Reichel {
7648c0984e5SSebastian Reichel 	int temp;
7658c0984e5SSebastian Reichel 
7668c0984e5SSebastian Reichel 	temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
7678c0984e5SSebastian Reichel 	if (temp < 0) {
7688c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading temperature\n");
7698c0984e5SSebastian Reichel 		return temp;
7708c0984e5SSebastian Reichel 	}
7718c0984e5SSebastian Reichel 
7728c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
7738c0984e5SSebastian Reichel 		temp = 5 * temp / 2;
7748c0984e5SSebastian Reichel 
7758c0984e5SSebastian Reichel 	return temp;
7768c0984e5SSebastian Reichel }
7778c0984e5SSebastian Reichel 
7788c0984e5SSebastian Reichel /*
7798c0984e5SSebastian Reichel  * Return the battery Cycle count total
7808c0984e5SSebastian Reichel  * Or < 0 if something fails.
7818c0984e5SSebastian Reichel  */
7828c0984e5SSebastian Reichel static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
7838c0984e5SSebastian Reichel {
7848c0984e5SSebastian Reichel 	int cyct;
7858c0984e5SSebastian Reichel 
7868c0984e5SSebastian Reichel 	cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
7878c0984e5SSebastian Reichel 	if (cyct < 0)
7888c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading cycle count total\n");
7898c0984e5SSebastian Reichel 
7908c0984e5SSebastian Reichel 	return cyct;
7918c0984e5SSebastian Reichel }
7928c0984e5SSebastian Reichel 
7938c0984e5SSebastian Reichel /*
7948c0984e5SSebastian Reichel  * Read a time register.
7958c0984e5SSebastian Reichel  * Return < 0 if something fails.
7968c0984e5SSebastian Reichel  */
7978c0984e5SSebastian Reichel static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
7988c0984e5SSebastian Reichel {
7998c0984e5SSebastian Reichel 	int tval;
8008c0984e5SSebastian Reichel 
8018c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, reg, false);
8028c0984e5SSebastian Reichel 	if (tval < 0) {
8038c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading time register %02x: %d\n",
8048c0984e5SSebastian Reichel 			reg, tval);
8058c0984e5SSebastian Reichel 		return tval;
8068c0984e5SSebastian Reichel 	}
8078c0984e5SSebastian Reichel 
8088c0984e5SSebastian Reichel 	if (tval == 65535)
8098c0984e5SSebastian Reichel 		return -ENODATA;
8108c0984e5SSebastian Reichel 
8118c0984e5SSebastian Reichel 	return tval * 60;
8128c0984e5SSebastian Reichel }
8138c0984e5SSebastian Reichel 
8148c0984e5SSebastian Reichel /*
8158c0984e5SSebastian Reichel  * Read an average power register.
8168c0984e5SSebastian Reichel  * Return < 0 if something fails.
8178c0984e5SSebastian Reichel  */
8188c0984e5SSebastian Reichel static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
8198c0984e5SSebastian Reichel {
8208c0984e5SSebastian Reichel 	int tval;
8218c0984e5SSebastian Reichel 
8228c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
8238c0984e5SSebastian Reichel 	if (tval < 0) {
8248c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading average power register  %02x: %d\n",
8258c0984e5SSebastian Reichel 			BQ27XXX_REG_AP, tval);
8268c0984e5SSebastian Reichel 		return tval;
8278c0984e5SSebastian Reichel 	}
8288c0984e5SSebastian Reichel 
8298c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
8308c0984e5SSebastian Reichel 		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
8318c0984e5SSebastian Reichel 	else
8328c0984e5SSebastian Reichel 		return tval;
8338c0984e5SSebastian Reichel }
8348c0984e5SSebastian Reichel 
8358c0984e5SSebastian Reichel /*
8368c0984e5SSebastian Reichel  * Returns true if a battery over temperature condition is detected
8378c0984e5SSebastian Reichel  */
8388c0984e5SSebastian Reichel static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
8398c0984e5SSebastian Reichel {
840e839a448SChris Lapa 	switch (di->chip) {
841818e3012SChris Lapa 	case BQ2750X:
8426da6e4bdSChris Lapa 	case BQ2751X:
84332833635SChris Lapa 	case BQ27500:
844bd28177fSChris Lapa 	case BQ27510G1:
845698a2bf5SChris Lapa 	case BQ27510G2:
84671375aa7SChris Lapa 	case BQ27510G3:
847e839a448SChris Lapa 	case BQ27541:
848e839a448SChris Lapa 	case BQ27545:
8498c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
850e839a448SChris Lapa 	case BQ27530:
851e839a448SChris Lapa 	case BQ27421:
8528c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_OT;
853e839a448SChris Lapa 	default:
8548c0984e5SSebastian Reichel 		return false;
8558c0984e5SSebastian Reichel 	}
856e839a448SChris Lapa }
8578c0984e5SSebastian Reichel 
8588c0984e5SSebastian Reichel /*
8598c0984e5SSebastian Reichel  * Returns true if a battery under temperature condition is detected
8608c0984e5SSebastian Reichel  */
8618c0984e5SSebastian Reichel static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
8628c0984e5SSebastian Reichel {
8638c0984e5SSebastian Reichel 	if (di->chip == BQ27530 || di->chip == BQ27421)
8648c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_UT;
8658c0984e5SSebastian Reichel 
8668c0984e5SSebastian Reichel 	return false;
8678c0984e5SSebastian Reichel }
8688c0984e5SSebastian Reichel 
8698c0984e5SSebastian Reichel /*
8708c0984e5SSebastian Reichel  * Returns true if a low state of charge condition is detected
8718c0984e5SSebastian Reichel  */
8728c0984e5SSebastian Reichel static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
8738c0984e5SSebastian Reichel {
8748c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
8758c0984e5SSebastian Reichel 		return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
8768c0984e5SSebastian Reichel 	else
8778c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
8788c0984e5SSebastian Reichel }
8798c0984e5SSebastian Reichel 
8808c0984e5SSebastian Reichel /*
8818c0984e5SSebastian Reichel  * Read flag register.
8828c0984e5SSebastian Reichel  * Return < 0 if something fails.
8838c0984e5SSebastian Reichel  */
8848c0984e5SSebastian Reichel static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
8858c0984e5SSebastian Reichel {
8868c0984e5SSebastian Reichel 	int flags;
887e4a404a0SH. Nikolaus Schaller 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
8888c0984e5SSebastian Reichel 
889e4a404a0SH. Nikolaus Schaller 	flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
8908c0984e5SSebastian Reichel 	if (flags < 0) {
8918c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading flag register:%d\n", flags);
8928c0984e5SSebastian Reichel 		return flags;
8938c0984e5SSebastian Reichel 	}
8948c0984e5SSebastian Reichel 
8958c0984e5SSebastian Reichel 	/* Unlikely but important to return first */
8968c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_overtemp(di, flags)))
8978c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_OVERHEAT;
8988c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_undertemp(di, flags)))
8998c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_COLD;
9008c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_dead(di, flags)))
9018c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_DEAD;
9028c0984e5SSebastian Reichel 
9038c0984e5SSebastian Reichel 	return POWER_SUPPLY_HEALTH_GOOD;
9048c0984e5SSebastian Reichel }
9058c0984e5SSebastian Reichel 
9068c0984e5SSebastian Reichel void bq27xxx_battery_update(struct bq27xxx_device_info *di)
9078c0984e5SSebastian Reichel {
9088c0984e5SSebastian Reichel 	struct bq27xxx_reg_cache cache = {0, };
9098c0984e5SSebastian Reichel 	bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
9108c0984e5SSebastian Reichel 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
9118c0984e5SSebastian Reichel 
9128c0984e5SSebastian Reichel 	cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
9138c0984e5SSebastian Reichel 	if ((cache.flags & 0xff) == 0xff)
9148c0984e5SSebastian Reichel 		cache.flags = -1; /* read error */
9158c0984e5SSebastian Reichel 	if (cache.flags >= 0) {
9168c0984e5SSebastian Reichel 		cache.temperature = bq27xxx_battery_read_temperature(di);
9178c0984e5SSebastian Reichel 		if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
9188c0984e5SSebastian Reichel 			dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
9198c0984e5SSebastian Reichel 			cache.capacity = -ENODATA;
9208c0984e5SSebastian Reichel 			cache.energy = -ENODATA;
9218c0984e5SSebastian Reichel 			cache.time_to_empty = -ENODATA;
9228c0984e5SSebastian Reichel 			cache.time_to_empty_avg = -ENODATA;
9238c0984e5SSebastian Reichel 			cache.time_to_full = -ENODATA;
9248c0984e5SSebastian Reichel 			cache.charge_full = -ENODATA;
9258c0984e5SSebastian Reichel 			cache.health = -ENODATA;
9268c0984e5SSebastian Reichel 		} else {
9278c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
9288c0984e5SSebastian Reichel 				cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
9298c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
9308c0984e5SSebastian Reichel 				cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
9318c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
9328c0984e5SSebastian Reichel 				cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
9338c0984e5SSebastian Reichel 			cache.charge_full = bq27xxx_battery_read_fcc(di);
9348c0984e5SSebastian Reichel 			cache.capacity = bq27xxx_battery_read_soc(di);
9358c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
9368c0984e5SSebastian Reichel 				cache.energy = bq27xxx_battery_read_energy(di);
9378c0984e5SSebastian Reichel 			cache.health = bq27xxx_battery_read_health(di);
9388c0984e5SSebastian Reichel 		}
9398c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
9408c0984e5SSebastian Reichel 			cache.cycle_count = bq27xxx_battery_read_cyct(di);
9418c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
9428c0984e5SSebastian Reichel 			cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
9438c0984e5SSebastian Reichel 
9448c0984e5SSebastian Reichel 		/* We only have to read charge design full once */
9458c0984e5SSebastian Reichel 		if (di->charge_design_full <= 0)
9468c0984e5SSebastian Reichel 			di->charge_design_full = bq27xxx_battery_read_dcap(di);
9478c0984e5SSebastian Reichel 	}
9488c0984e5SSebastian Reichel 
9498c0984e5SSebastian Reichel 	if (di->cache.capacity != cache.capacity)
9508c0984e5SSebastian Reichel 		power_supply_changed(di->bat);
9518c0984e5SSebastian Reichel 
9528c0984e5SSebastian Reichel 	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
9538c0984e5SSebastian Reichel 		di->cache = cache;
9548c0984e5SSebastian Reichel 
9558c0984e5SSebastian Reichel 	di->last_update = jiffies;
9568c0984e5SSebastian Reichel }
9578c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
9588c0984e5SSebastian Reichel 
9598c0984e5SSebastian Reichel static void bq27xxx_battery_poll(struct work_struct *work)
9608c0984e5SSebastian Reichel {
9618c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di =
9628c0984e5SSebastian Reichel 			container_of(work, struct bq27xxx_device_info,
9638c0984e5SSebastian Reichel 				     work.work);
9648c0984e5SSebastian Reichel 
9658c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
9668c0984e5SSebastian Reichel 
9678c0984e5SSebastian Reichel 	if (poll_interval > 0)
9688c0984e5SSebastian Reichel 		schedule_delayed_work(&di->work, poll_interval * HZ);
9698c0984e5SSebastian Reichel }
9708c0984e5SSebastian Reichel 
9718c0984e5SSebastian Reichel /*
9728c0984e5SSebastian Reichel  * Return the battery average current in µA
9738c0984e5SSebastian Reichel  * Note that current can be negative signed as well
9748c0984e5SSebastian Reichel  * Or 0 if something fails.
9758c0984e5SSebastian Reichel  */
9768c0984e5SSebastian Reichel static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
9778c0984e5SSebastian Reichel 				   union power_supply_propval *val)
9788c0984e5SSebastian Reichel {
9798c0984e5SSebastian Reichel 	int curr;
9808c0984e5SSebastian Reichel 	int flags;
9818c0984e5SSebastian Reichel 
9828c0984e5SSebastian Reichel 	curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
9838c0984e5SSebastian Reichel 	if (curr < 0) {
9848c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading current\n");
9858c0984e5SSebastian Reichel 		return curr;
9868c0984e5SSebastian Reichel 	}
9878c0984e5SSebastian Reichel 
9888c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
989e4a404a0SH. Nikolaus Schaller 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
9908c0984e5SSebastian Reichel 		if (flags & BQ27000_FLAG_CHGS) {
9918c0984e5SSebastian Reichel 			dev_dbg(di->dev, "negative current!\n");
9928c0984e5SSebastian Reichel 			curr = -curr;
9938c0984e5SSebastian Reichel 		}
9948c0984e5SSebastian Reichel 
9958c0984e5SSebastian Reichel 		val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
9968c0984e5SSebastian Reichel 	} else {
9978c0984e5SSebastian Reichel 		/* Other gauges return signed value */
9988c0984e5SSebastian Reichel 		val->intval = (int)((s16)curr) * 1000;
9998c0984e5SSebastian Reichel 	}
10008c0984e5SSebastian Reichel 
10018c0984e5SSebastian Reichel 	return 0;
10028c0984e5SSebastian Reichel }
10038c0984e5SSebastian Reichel 
10048c0984e5SSebastian Reichel static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
10058c0984e5SSebastian Reichel 				  union power_supply_propval *val)
10068c0984e5SSebastian Reichel {
10078c0984e5SSebastian Reichel 	int status;
10088c0984e5SSebastian Reichel 
10098c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
10108c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
10118c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
10128c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_CHGS)
10138c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
10148c0984e5SSebastian Reichel 		else if (power_supply_am_i_supplied(di->bat))
10158c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
10168c0984e5SSebastian Reichel 		else
10178c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
10188c0984e5SSebastian Reichel 	} else {
10198c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
10208c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
10218c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_DSC)
10228c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
10238c0984e5SSebastian Reichel 		else
10248c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
10258c0984e5SSebastian Reichel 	}
10268c0984e5SSebastian Reichel 
10278c0984e5SSebastian Reichel 	val->intval = status;
10288c0984e5SSebastian Reichel 
10298c0984e5SSebastian Reichel 	return 0;
10308c0984e5SSebastian Reichel }
10318c0984e5SSebastian Reichel 
10328c0984e5SSebastian Reichel static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
10338c0984e5SSebastian Reichel 					  union power_supply_propval *val)
10348c0984e5SSebastian Reichel {
10358c0984e5SSebastian Reichel 	int level;
10368c0984e5SSebastian Reichel 
10378c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
10388c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
10398c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
10408c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDV1)
10418c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
10428c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDVF)
10438c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
10448c0984e5SSebastian Reichel 		else
10458c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
10468c0984e5SSebastian Reichel 	} else {
10478c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
10488c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
10498c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
10508c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
10518c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
10528c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
10538c0984e5SSebastian Reichel 		else
10548c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
10558c0984e5SSebastian Reichel 	}
10568c0984e5SSebastian Reichel 
10578c0984e5SSebastian Reichel 	val->intval = level;
10588c0984e5SSebastian Reichel 
10598c0984e5SSebastian Reichel 	return 0;
10608c0984e5SSebastian Reichel }
10618c0984e5SSebastian Reichel 
10628c0984e5SSebastian Reichel /*
10638c0984e5SSebastian Reichel  * Return the battery Voltage in millivolts
10648c0984e5SSebastian Reichel  * Or < 0 if something fails.
10658c0984e5SSebastian Reichel  */
10668c0984e5SSebastian Reichel static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
10678c0984e5SSebastian Reichel 				   union power_supply_propval *val)
10688c0984e5SSebastian Reichel {
10698c0984e5SSebastian Reichel 	int volt;
10708c0984e5SSebastian Reichel 
10718c0984e5SSebastian Reichel 	volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
10728c0984e5SSebastian Reichel 	if (volt < 0) {
10738c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading voltage\n");
10748c0984e5SSebastian Reichel 		return volt;
10758c0984e5SSebastian Reichel 	}
10768c0984e5SSebastian Reichel 
10778c0984e5SSebastian Reichel 	val->intval = volt * 1000;
10788c0984e5SSebastian Reichel 
10798c0984e5SSebastian Reichel 	return 0;
10808c0984e5SSebastian Reichel }
10818c0984e5SSebastian Reichel 
10828c0984e5SSebastian Reichel static int bq27xxx_simple_value(int value,
10838c0984e5SSebastian Reichel 				union power_supply_propval *val)
10848c0984e5SSebastian Reichel {
10858c0984e5SSebastian Reichel 	if (value < 0)
10868c0984e5SSebastian Reichel 		return value;
10878c0984e5SSebastian Reichel 
10888c0984e5SSebastian Reichel 	val->intval = value;
10898c0984e5SSebastian Reichel 
10908c0984e5SSebastian Reichel 	return 0;
10918c0984e5SSebastian Reichel }
10928c0984e5SSebastian Reichel 
10938c0984e5SSebastian Reichel static int bq27xxx_battery_get_property(struct power_supply *psy,
10948c0984e5SSebastian Reichel 					enum power_supply_property psp,
10958c0984e5SSebastian Reichel 					union power_supply_propval *val)
10968c0984e5SSebastian Reichel {
10978c0984e5SSebastian Reichel 	int ret = 0;
10988c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
10998c0984e5SSebastian Reichel 
11008c0984e5SSebastian Reichel 	mutex_lock(&di->lock);
11018c0984e5SSebastian Reichel 	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
11028c0984e5SSebastian Reichel 		cancel_delayed_work_sync(&di->work);
11038c0984e5SSebastian Reichel 		bq27xxx_battery_poll(&di->work.work);
11048c0984e5SSebastian Reichel 	}
11058c0984e5SSebastian Reichel 	mutex_unlock(&di->lock);
11068c0984e5SSebastian Reichel 
11078c0984e5SSebastian Reichel 	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
11088c0984e5SSebastian Reichel 		return -ENODEV;
11098c0984e5SSebastian Reichel 
11108c0984e5SSebastian Reichel 	switch (psp) {
11118c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
11128c0984e5SSebastian Reichel 		ret = bq27xxx_battery_status(di, val);
11138c0984e5SSebastian Reichel 		break;
11148c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
11158c0984e5SSebastian Reichel 		ret = bq27xxx_battery_voltage(di, val);
11168c0984e5SSebastian Reichel 		break;
11178c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
11188c0984e5SSebastian Reichel 		val->intval = di->cache.flags < 0 ? 0 : 1;
11198c0984e5SSebastian Reichel 		break;
11208c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
11218c0984e5SSebastian Reichel 		ret = bq27xxx_battery_current(di, val);
11228c0984e5SSebastian Reichel 		break;
11238c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
11248c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.capacity, val);
11258c0984e5SSebastian Reichel 		break;
11268c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
11278c0984e5SSebastian Reichel 		ret = bq27xxx_battery_capacity_level(di, val);
11288c0984e5SSebastian Reichel 		break;
11298c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
11308c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.temperature, val);
11318c0984e5SSebastian Reichel 		if (ret == 0)
11328c0984e5SSebastian Reichel 			val->intval -= 2731; /* convert decidegree k to c */
11338c0984e5SSebastian Reichel 		break;
11348c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
11358c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
11368c0984e5SSebastian Reichel 		break;
11378c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
11388c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
11398c0984e5SSebastian Reichel 		break;
11408c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
11418c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
11428c0984e5SSebastian Reichel 		break;
11438c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
11448c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
11458c0984e5SSebastian Reichel 		break;
11468c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
11478c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
11488c0984e5SSebastian Reichel 		break;
11498c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
11508c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.charge_full, val);
11518c0984e5SSebastian Reichel 		break;
11528c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
11538c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->charge_design_full, val);
11548c0984e5SSebastian Reichel 		break;
11558c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
11568c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
11578c0984e5SSebastian Reichel 		break;
11588c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ENERGY_NOW:
11598c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.energy, val);
11608c0984e5SSebastian Reichel 		break;
11618c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_POWER_AVG:
11628c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.power_avg, val);
11638c0984e5SSebastian Reichel 		break;
11648c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
11658c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.health, val);
11668c0984e5SSebastian Reichel 		break;
11678c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
11688c0984e5SSebastian Reichel 		val->strval = BQ27XXX_MANUFACTURER;
11698c0984e5SSebastian Reichel 		break;
11708c0984e5SSebastian Reichel 	default:
11718c0984e5SSebastian Reichel 		return -EINVAL;
11728c0984e5SSebastian Reichel 	}
11738c0984e5SSebastian Reichel 
11748c0984e5SSebastian Reichel 	return ret;
11758c0984e5SSebastian Reichel }
11768c0984e5SSebastian Reichel 
11778c0984e5SSebastian Reichel static void bq27xxx_external_power_changed(struct power_supply *psy)
11788c0984e5SSebastian Reichel {
11798c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
11808c0984e5SSebastian Reichel 
11818c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
11828c0984e5SSebastian Reichel 	schedule_delayed_work(&di->work, 0);
11838c0984e5SSebastian Reichel }
11848c0984e5SSebastian Reichel 
11858c0984e5SSebastian Reichel int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
11868c0984e5SSebastian Reichel {
11878c0984e5SSebastian Reichel 	struct power_supply_desc *psy_desc;
11888c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = { .drv_data = di, };
11898c0984e5SSebastian Reichel 
11908c0984e5SSebastian Reichel 	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
11918c0984e5SSebastian Reichel 	mutex_init(&di->lock);
11928c0984e5SSebastian Reichel 	di->regs = bq27xxx_regs[di->chip];
11938c0984e5SSebastian Reichel 
11948c0984e5SSebastian Reichel 	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
11958c0984e5SSebastian Reichel 	if (!psy_desc)
11968c0984e5SSebastian Reichel 		return -ENOMEM;
11978c0984e5SSebastian Reichel 
11988c0984e5SSebastian Reichel 	psy_desc->name = di->name;
11998c0984e5SSebastian Reichel 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
12008c0984e5SSebastian Reichel 	psy_desc->properties = bq27xxx_battery_props[di->chip].props;
12018c0984e5SSebastian Reichel 	psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
12028c0984e5SSebastian Reichel 	psy_desc->get_property = bq27xxx_battery_get_property;
12038c0984e5SSebastian Reichel 	psy_desc->external_power_changed = bq27xxx_external_power_changed;
12048c0984e5SSebastian Reichel 
12058c0984e5SSebastian Reichel 	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
12068c0984e5SSebastian Reichel 	if (IS_ERR(di->bat)) {
12078c0984e5SSebastian Reichel 		dev_err(di->dev, "failed to register battery\n");
12088c0984e5SSebastian Reichel 		return PTR_ERR(di->bat);
12098c0984e5SSebastian Reichel 	}
12108c0984e5SSebastian Reichel 
12118c0984e5SSebastian Reichel 	dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
12128c0984e5SSebastian Reichel 
12138c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
12148c0984e5SSebastian Reichel 
12151d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
12161d72706fSMatt Ranostay 	list_add(&di->list, &bq27xxx_battery_devices);
12171d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
12181d72706fSMatt Ranostay 
12198c0984e5SSebastian Reichel 	return 0;
12208c0984e5SSebastian Reichel }
12218c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
12228c0984e5SSebastian Reichel 
12238c0984e5SSebastian Reichel void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
12248c0984e5SSebastian Reichel {
12258c0984e5SSebastian Reichel 	/*
12268c0984e5SSebastian Reichel 	 * power_supply_unregister call bq27xxx_battery_get_property which
12278c0984e5SSebastian Reichel 	 * call bq27xxx_battery_poll.
12288c0984e5SSebastian Reichel 	 * Make sure that bq27xxx_battery_poll will not call
12298c0984e5SSebastian Reichel 	 * schedule_delayed_work again after unregister (which cause OOPS).
12308c0984e5SSebastian Reichel 	 */
12318c0984e5SSebastian Reichel 	poll_interval = 0;
12328c0984e5SSebastian Reichel 
12338c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
12348c0984e5SSebastian Reichel 
12358c0984e5SSebastian Reichel 	power_supply_unregister(di->bat);
12368c0984e5SSebastian Reichel 
12371d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
12381d72706fSMatt Ranostay 	list_del(&di->list);
12391d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
12401d72706fSMatt Ranostay 
12418c0984e5SSebastian Reichel 	mutex_destroy(&di->lock);
12428c0984e5SSebastian Reichel }
12438c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
12448c0984e5SSebastian Reichel 
12458c0984e5SSebastian Reichel static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
12468c0984e5SSebastian Reichel 					 bool single)
12478c0984e5SSebastian Reichel {
12488c0984e5SSebastian Reichel 	struct device *dev = di->dev;
12498c0984e5SSebastian Reichel 	struct bq27xxx_platform_data *pdata = dev->platform_data;
12508c0984e5SSebastian Reichel 	unsigned int timeout = 3;
12518c0984e5SSebastian Reichel 	int upper, lower;
12528c0984e5SSebastian Reichel 	int temp;
12538c0984e5SSebastian Reichel 
12548c0984e5SSebastian Reichel 	if (!single) {
12558c0984e5SSebastian Reichel 		/* Make sure the value has not changed in between reading the
12568c0984e5SSebastian Reichel 		 * lower and the upper part */
12578c0984e5SSebastian Reichel 		upper = pdata->read(dev, reg + 1);
12588c0984e5SSebastian Reichel 		do {
12598c0984e5SSebastian Reichel 			temp = upper;
12608c0984e5SSebastian Reichel 			if (upper < 0)
12618c0984e5SSebastian Reichel 				return upper;
12628c0984e5SSebastian Reichel 
12638c0984e5SSebastian Reichel 			lower = pdata->read(dev, reg);
12648c0984e5SSebastian Reichel 			if (lower < 0)
12658c0984e5SSebastian Reichel 				return lower;
12668c0984e5SSebastian Reichel 
12678c0984e5SSebastian Reichel 			upper = pdata->read(dev, reg + 1);
12688c0984e5SSebastian Reichel 		} while (temp != upper && --timeout);
12698c0984e5SSebastian Reichel 
12708c0984e5SSebastian Reichel 		if (timeout == 0)
12718c0984e5SSebastian Reichel 			return -EIO;
12728c0984e5SSebastian Reichel 
12738c0984e5SSebastian Reichel 		return (upper << 8) | lower;
12748c0984e5SSebastian Reichel 	}
12758c0984e5SSebastian Reichel 
12768c0984e5SSebastian Reichel 	return pdata->read(dev, reg);
12778c0984e5SSebastian Reichel }
12788c0984e5SSebastian Reichel 
12798c0984e5SSebastian Reichel static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
12808c0984e5SSebastian Reichel {
12818c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di;
12828c0984e5SSebastian Reichel 	struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
12838c0984e5SSebastian Reichel 
12848c0984e5SSebastian Reichel 	if (!pdata) {
12858c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no platform_data supplied\n");
12868c0984e5SSebastian Reichel 		return -EINVAL;
12878c0984e5SSebastian Reichel 	}
12888c0984e5SSebastian Reichel 
12898c0984e5SSebastian Reichel 	if (!pdata->read) {
12908c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no hdq read callback supplied\n");
12918c0984e5SSebastian Reichel 		return -EINVAL;
12928c0984e5SSebastian Reichel 	}
12938c0984e5SSebastian Reichel 
12948c0984e5SSebastian Reichel 	if (!pdata->chip) {
12958c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no device supplied\n");
12968c0984e5SSebastian Reichel 		return -EINVAL;
12978c0984e5SSebastian Reichel 	}
12988c0984e5SSebastian Reichel 
12998c0984e5SSebastian Reichel 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
13008c0984e5SSebastian Reichel 	if (!di)
13018c0984e5SSebastian Reichel 		return -ENOMEM;
13028c0984e5SSebastian Reichel 
13038c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, di);
13048c0984e5SSebastian Reichel 
13058c0984e5SSebastian Reichel 	di->dev = &pdev->dev;
13068c0984e5SSebastian Reichel 	di->chip = pdata->chip;
13078c0984e5SSebastian Reichel 	di->name = pdata->name ?: dev_name(&pdev->dev);
13088c0984e5SSebastian Reichel 	di->bus.read = bq27xxx_battery_platform_read;
13098c0984e5SSebastian Reichel 
13108c0984e5SSebastian Reichel 	return bq27xxx_battery_setup(di);
13118c0984e5SSebastian Reichel }
13128c0984e5SSebastian Reichel 
13138c0984e5SSebastian Reichel static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
13148c0984e5SSebastian Reichel {
13158c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
13168c0984e5SSebastian Reichel 
13178c0984e5SSebastian Reichel 	bq27xxx_battery_teardown(di);
13188c0984e5SSebastian Reichel 
13198c0984e5SSebastian Reichel 	return 0;
13208c0984e5SSebastian Reichel }
13218c0984e5SSebastian Reichel 
13228c0984e5SSebastian Reichel static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
13238c0984e5SSebastian Reichel 	{ "bq27000-battery", },
13248c0984e5SSebastian Reichel 	{ /* sentinel */ }
13258c0984e5SSebastian Reichel };
13268c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
13278c0984e5SSebastian Reichel 
13288c0984e5SSebastian Reichel #ifdef CONFIG_OF
13298c0984e5SSebastian Reichel static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
13308c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27000" },
13318c0984e5SSebastian Reichel 	{},
13328c0984e5SSebastian Reichel };
13338c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
13348c0984e5SSebastian Reichel #endif
13358c0984e5SSebastian Reichel 
13368c0984e5SSebastian Reichel static struct platform_driver bq27xxx_battery_platform_driver = {
13378c0984e5SSebastian Reichel 	.probe	= bq27xxx_battery_platform_probe,
13388c0984e5SSebastian Reichel 	.remove = bq27xxx_battery_platform_remove,
13398c0984e5SSebastian Reichel 	.driver = {
13408c0984e5SSebastian Reichel 		.name = "bq27000-battery",
13418c0984e5SSebastian Reichel 		.of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
13428c0984e5SSebastian Reichel 	},
13438c0984e5SSebastian Reichel 	.id_table = bq27xxx_battery_platform_id_table,
13448c0984e5SSebastian Reichel };
13458c0984e5SSebastian Reichel module_platform_driver(bq27xxx_battery_platform_driver);
13468c0984e5SSebastian Reichel 
13478c0984e5SSebastian Reichel MODULE_ALIAS("platform:bq27000-battery");
13488c0984e5SSebastian Reichel 
13498c0984e5SSebastian Reichel MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
13508c0984e5SSebastian Reichel MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
13518c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
1352