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
258c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27510-g3
268c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27520-g4
278c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27530-g1
288c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27531-g1
298c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27541-g1
308c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27542-g1
318c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27546-g1
328c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27742-g1
338c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27545-g1
348c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27421-g1
358c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27425-g1
368c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27411-g1
378c0984e5SSebastian Reichel  * http://www.ti.com/product/bq27621-g1
388c0984e5SSebastian Reichel  */
398c0984e5SSebastian Reichel 
408c0984e5SSebastian Reichel #include <linux/device.h>
418c0984e5SSebastian Reichel #include <linux/module.h>
421d72706fSMatt Ranostay #include <linux/mutex.h>
438c0984e5SSebastian Reichel #include <linux/param.h>
448c0984e5SSebastian Reichel #include <linux/jiffies.h>
458c0984e5SSebastian Reichel #include <linux/workqueue.h>
468c0984e5SSebastian Reichel #include <linux/delay.h>
478c0984e5SSebastian Reichel #include <linux/platform_device.h>
488c0984e5SSebastian Reichel #include <linux/power_supply.h>
498c0984e5SSebastian Reichel #include <linux/slab.h>
508c0984e5SSebastian Reichel #include <linux/of.h>
518c0984e5SSebastian Reichel 
528c0984e5SSebastian Reichel #include <linux/power/bq27xxx_battery.h>
538c0984e5SSebastian Reichel 
548c0984e5SSebastian Reichel #define DRIVER_VERSION		"1.2.0"
558c0984e5SSebastian Reichel 
568c0984e5SSebastian Reichel #define BQ27XXX_MANUFACTURER	"Texas Instruments"
578c0984e5SSebastian Reichel 
588c0984e5SSebastian Reichel /* BQ27XXX Flags */
598c0984e5SSebastian Reichel #define BQ27XXX_FLAG_DSC	BIT(0)
608c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOCF	BIT(1) /* State-of-Charge threshold final */
618c0984e5SSebastian Reichel #define BQ27XXX_FLAG_SOC1	BIT(2) /* State-of-Charge threshold 1 */
628c0984e5SSebastian Reichel #define BQ27XXX_FLAG_FC		BIT(9)
638c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OTD	BIT(14)
648c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OTC	BIT(15)
658c0984e5SSebastian Reichel #define BQ27XXX_FLAG_UT		BIT(14)
668c0984e5SSebastian Reichel #define BQ27XXX_FLAG_OT		BIT(15)
678c0984e5SSebastian Reichel 
688c0984e5SSebastian Reichel /* BQ27000 has different layout for Flags register */
698c0984e5SSebastian Reichel #define BQ27000_FLAG_EDVF	BIT(0) /* Final End-of-Discharge-Voltage flag */
708c0984e5SSebastian Reichel #define BQ27000_FLAG_EDV1	BIT(1) /* First End-of-Discharge-Voltage flag */
718c0984e5SSebastian Reichel #define BQ27000_FLAG_CI		BIT(4) /* Capacity Inaccurate flag */
728c0984e5SSebastian Reichel #define BQ27000_FLAG_FC		BIT(5)
738c0984e5SSebastian Reichel #define BQ27000_FLAG_CHGS	BIT(7) /* Charge state flag */
748c0984e5SSebastian Reichel 
758c0984e5SSebastian Reichel #define BQ27XXX_RS			(20) /* Resistor sense mOhm */
768c0984e5SSebastian Reichel #define BQ27XXX_POWER_CONSTANT		(29200) /* 29.2 µV^2 * 1000 */
778c0984e5SSebastian Reichel #define BQ27XXX_CURRENT_CONSTANT	(3570) /* 3.57 µV * 1000 */
788c0984e5SSebastian Reichel 
798c0984e5SSebastian Reichel #define INVALID_REG_ADDR	0xff
808c0984e5SSebastian Reichel 
818c0984e5SSebastian Reichel /*
828c0984e5SSebastian Reichel  * bq27xxx_reg_index - Register names
838c0984e5SSebastian Reichel  *
848c0984e5SSebastian Reichel  * These are indexes into a device's register mapping array.
858c0984e5SSebastian Reichel  */
868c0984e5SSebastian Reichel 
878c0984e5SSebastian Reichel enum bq27xxx_reg_index {
888c0984e5SSebastian Reichel 	BQ27XXX_REG_CTRL = 0,	/* Control */
898c0984e5SSebastian Reichel 	BQ27XXX_REG_TEMP,	/* Temperature */
908c0984e5SSebastian Reichel 	BQ27XXX_REG_INT_TEMP,	/* Internal Temperature */
918c0984e5SSebastian Reichel 	BQ27XXX_REG_VOLT,	/* Voltage */
928c0984e5SSebastian Reichel 	BQ27XXX_REG_AI,		/* Average Current */
938c0984e5SSebastian Reichel 	BQ27XXX_REG_FLAGS,	/* Flags */
948c0984e5SSebastian Reichel 	BQ27XXX_REG_TTE,	/* Time-to-Empty */
958c0984e5SSebastian Reichel 	BQ27XXX_REG_TTF,	/* Time-to-Full */
968c0984e5SSebastian Reichel 	BQ27XXX_REG_TTES,	/* Time-to-Empty Standby */
978c0984e5SSebastian Reichel 	BQ27XXX_REG_TTECP,	/* Time-to-Empty at Constant Power */
988c0984e5SSebastian Reichel 	BQ27XXX_REG_NAC,	/* Nominal Available Capacity */
998c0984e5SSebastian Reichel 	BQ27XXX_REG_FCC,	/* Full Charge Capacity */
1008c0984e5SSebastian Reichel 	BQ27XXX_REG_CYCT,	/* Cycle Count */
1018c0984e5SSebastian Reichel 	BQ27XXX_REG_AE,		/* Available Energy */
1028c0984e5SSebastian Reichel 	BQ27XXX_REG_SOC,	/* State-of-Charge */
1038c0984e5SSebastian Reichel 	BQ27XXX_REG_DCAP,	/* Design Capacity */
1048c0984e5SSebastian Reichel 	BQ27XXX_REG_AP,		/* Average Power */
1058c0984e5SSebastian Reichel 	BQ27XXX_REG_MAX,	/* sentinel */
1068c0984e5SSebastian Reichel };
1078c0984e5SSebastian Reichel 
1088c0984e5SSebastian Reichel /* Register mappings */
1098c0984e5SSebastian Reichel static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = {
1108c0984e5SSebastian Reichel 	[BQ27000] = {
1118c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1128c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1138c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1148c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1158c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1168c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1178c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1188c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1198c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1208c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1218c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1228c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1238c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1248c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = 0x22,
1258c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1268c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1278c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
1288c0984e5SSebastian Reichel 	},
1298c0984e5SSebastian Reichel 	[BQ27010] = {
1308c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1318c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1328c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
1338c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1348c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1358c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1368c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1378c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = 0x18,
1388c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1c,
1398c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = 0x26,
1408c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1418c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1428c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1438c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1448c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x0b,
1458c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x76,
1468c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1478c0984e5SSebastian Reichel 	},
1488c0984e5SSebastian Reichel 	[BQ27500] = {
1498c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1508c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1518c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
1528c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1538c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1548c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1558c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1568c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1578c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = 0x1a,
1588c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1598c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1608c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1618c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1628c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1638c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
1648c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
1658c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
1668c0984e5SSebastian Reichel 	},
1678c0984e5SSebastian Reichel 	[BQ27530] = {
1688c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1698c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1708c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x32,
1718c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1728c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1738c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1748c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1758c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1768c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
1778c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1788c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1798c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1808c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
1818c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
1828c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
1838c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
1848c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
1858c0984e5SSebastian Reichel 	},
1868c0984e5SSebastian Reichel 	[BQ27541] = {
1878c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
1888c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
1898c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
1908c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
1918c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
1928c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
1938c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
1948c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
1958c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
1968c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
1978c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
1988c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
1998c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
2008c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
2018c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
2028c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
2038c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
2048c0984e5SSebastian Reichel 	},
2058c0984e5SSebastian Reichel 	[BQ27545] = {
2068c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
2078c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x06,
2088c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x28,
2098c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x08,
2108c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x14,
2118c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x0a,
2128c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = 0x16,
2138c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
2148c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
2158c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
2168c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x0c,
2178c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x12,
2188c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = 0x2a,
2198c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
2208c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x2c,
2218c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
2228c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x24,
2238c0984e5SSebastian Reichel 	},
2248c0984e5SSebastian Reichel 	[BQ27421] = {
2258c0984e5SSebastian Reichel 		[BQ27XXX_REG_CTRL] = 0x00,
2268c0984e5SSebastian Reichel 		[BQ27XXX_REG_TEMP] = 0x02,
2278c0984e5SSebastian Reichel 		[BQ27XXX_REG_INT_TEMP] = 0x1e,
2288c0984e5SSebastian Reichel 		[BQ27XXX_REG_VOLT] = 0x04,
2298c0984e5SSebastian Reichel 		[BQ27XXX_REG_AI] = 0x10,
2308c0984e5SSebastian Reichel 		[BQ27XXX_REG_FLAGS] = 0x06,
2318c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
2328c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
2338c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
2348c0984e5SSebastian Reichel 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
2358c0984e5SSebastian Reichel 		[BQ27XXX_REG_NAC] = 0x08,
2368c0984e5SSebastian Reichel 		[BQ27XXX_REG_FCC] = 0x0e,
2378c0984e5SSebastian Reichel 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
2388c0984e5SSebastian Reichel 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
2398c0984e5SSebastian Reichel 		[BQ27XXX_REG_SOC] = 0x1c,
2408c0984e5SSebastian Reichel 		[BQ27XXX_REG_DCAP] = 0x3c,
2418c0984e5SSebastian Reichel 		[BQ27XXX_REG_AP] = 0x18,
2428c0984e5SSebastian Reichel 	},
2438c0984e5SSebastian Reichel };
2448c0984e5SSebastian Reichel 
2458c0984e5SSebastian Reichel static enum power_supply_property bq27000_battery_props[] = {
2468c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2478c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
2488c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2498c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
2508c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
2518c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
2528c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
2538c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
2548c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
2558c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
2568c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
2578c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
2588c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
2598c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
2608c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
2618c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ENERGY_NOW,
2628c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
2638c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
2648c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
2658c0984e5SSebastian Reichel };
2668c0984e5SSebastian Reichel 
2678c0984e5SSebastian Reichel static enum power_supply_property bq27010_battery_props[] = {
2688c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2698c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
2708c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2718c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
2728c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
2738c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
2748c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
2758c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
2768c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
2778c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
2788c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
2798c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
2808c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
2818c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
2828c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
2838c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
2848c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
2858c0984e5SSebastian Reichel };
2868c0984e5SSebastian Reichel 
2878c0984e5SSebastian Reichel static enum power_supply_property bq27500_battery_props[] = {
2888c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2898c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
2908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2918c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
2928c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
2938c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
2948c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
2958c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
2968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
2978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
2988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
2998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3008c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
3018c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
3028c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
3038c0984e5SSebastian Reichel };
3048c0984e5SSebastian Reichel 
3058c0984e5SSebastian Reichel static enum power_supply_property bq27530_battery_props[] = {
3068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
3088c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3098c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3108c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
3118c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3128c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
3148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
3158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
3168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
3178c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
3188c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
3198c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
3208c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
3218c0984e5SSebastian Reichel };
3228c0984e5SSebastian Reichel 
3238c0984e5SSebastian Reichel static enum power_supply_property bq27541_battery_props[] = {
3248c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3258c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
3268c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3278c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3288c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
3298c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3308c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3318c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
3328c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
3338c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
3348c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
3358c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3368c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
3378c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
3388c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
3398c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
3408c0984e5SSebastian Reichel };
3418c0984e5SSebastian Reichel 
3428c0984e5SSebastian Reichel static enum power_supply_property bq27545_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_TECHNOLOGY,
3528c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
3538c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
3548c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
3558c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CYCLE_COUNT,
3568c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_POWER_AVG,
3578c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
3588c0984e5SSebastian Reichel };
3598c0984e5SSebastian Reichel 
3608c0984e5SSebastian Reichel static enum power_supply_property bq27421_battery_props[] = {
3618c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3628c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
3638c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3648c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3658c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
3668c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3678c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3688c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
3698c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
3708c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
3718c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3728c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
3738c0984e5SSebastian Reichel };
3748c0984e5SSebastian Reichel 
3758c0984e5SSebastian Reichel #define BQ27XXX_PROP(_id, _prop)		\
3768c0984e5SSebastian Reichel 	[_id] = {				\
3778c0984e5SSebastian Reichel 		.props = _prop,			\
3788c0984e5SSebastian Reichel 		.size = ARRAY_SIZE(_prop),	\
3798c0984e5SSebastian Reichel 	}
3808c0984e5SSebastian Reichel 
3818c0984e5SSebastian Reichel static struct {
3828c0984e5SSebastian Reichel 	enum power_supply_property *props;
3838c0984e5SSebastian Reichel 	size_t size;
3848c0984e5SSebastian Reichel } bq27xxx_battery_props[] = {
3858c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27000, bq27000_battery_props),
3868c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27010, bq27010_battery_props),
3878c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27500, bq27500_battery_props),
3888c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27530, bq27530_battery_props),
3898c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27541, bq27541_battery_props),
3908c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27545, bq27545_battery_props),
3918c0984e5SSebastian Reichel 	BQ27XXX_PROP(BQ27421, bq27421_battery_props),
3928c0984e5SSebastian Reichel };
3938c0984e5SSebastian Reichel 
3941d72706fSMatt Ranostay static DEFINE_MUTEX(bq27xxx_list_lock);
3951d72706fSMatt Ranostay static LIST_HEAD(bq27xxx_battery_devices);
3961d72706fSMatt Ranostay 
3971d72706fSMatt Ranostay static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
3981d72706fSMatt Ranostay {
3991d72706fSMatt Ranostay 	struct bq27xxx_device_info *di;
4001d72706fSMatt Ranostay 	int ret;
4011d72706fSMatt Ranostay 
4021d72706fSMatt Ranostay 	ret = param_set_uint(val, kp);
4031d72706fSMatt Ranostay 	if (ret < 0)
4041d72706fSMatt Ranostay 		return ret;
4051d72706fSMatt Ranostay 
4061d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
4071d72706fSMatt Ranostay 	list_for_each_entry(di, &bq27xxx_battery_devices, list) {
4081d72706fSMatt Ranostay 		cancel_delayed_work_sync(&di->work);
4091d72706fSMatt Ranostay 		schedule_delayed_work(&di->work, 0);
4101d72706fSMatt Ranostay 	}
4111d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
4121d72706fSMatt Ranostay 
4131d72706fSMatt Ranostay 	return ret;
4141d72706fSMatt Ranostay }
4151d72706fSMatt Ranostay 
4161d72706fSMatt Ranostay static const struct kernel_param_ops param_ops_poll_interval = {
4171d72706fSMatt Ranostay 	.get = param_get_uint,
4181d72706fSMatt Ranostay 	.set = poll_interval_param_set,
4191d72706fSMatt Ranostay };
4201d72706fSMatt Ranostay 
4218c0984e5SSebastian Reichel static unsigned int poll_interval = 360;
4221d72706fSMatt Ranostay module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
4238c0984e5SSebastian Reichel MODULE_PARM_DESC(poll_interval,
4248c0984e5SSebastian Reichel 		 "battery poll interval in seconds - 0 disables polling");
4258c0984e5SSebastian Reichel 
4268c0984e5SSebastian Reichel /*
4278c0984e5SSebastian Reichel  * Common code for BQ27xxx devices
4288c0984e5SSebastian Reichel  */
4298c0984e5SSebastian Reichel 
4308c0984e5SSebastian Reichel static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
4318c0984e5SSebastian Reichel 			       bool single)
4328c0984e5SSebastian Reichel {
4338c0984e5SSebastian Reichel 	/* Reports EINVAL for invalid/missing registers */
4348c0984e5SSebastian Reichel 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
4358c0984e5SSebastian Reichel 		return -EINVAL;
4368c0984e5SSebastian Reichel 
4378c0984e5SSebastian Reichel 	return di->bus.read(di, di->regs[reg_index], single);
4388c0984e5SSebastian Reichel }
4398c0984e5SSebastian Reichel 
4408c0984e5SSebastian Reichel /*
4418c0984e5SSebastian Reichel  * Return the battery State-of-Charge
4428c0984e5SSebastian Reichel  * Or < 0 if something fails.
4438c0984e5SSebastian Reichel  */
4448c0984e5SSebastian Reichel static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
4458c0984e5SSebastian Reichel {
4468c0984e5SSebastian Reichel 	int soc;
4478c0984e5SSebastian Reichel 
4488c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
4498c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
4508c0984e5SSebastian Reichel 	else
4518c0984e5SSebastian Reichel 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
4528c0984e5SSebastian Reichel 
4538c0984e5SSebastian Reichel 	if (soc < 0)
4548c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading State-of-Charge\n");
4558c0984e5SSebastian Reichel 
4568c0984e5SSebastian Reichel 	return soc;
4578c0984e5SSebastian Reichel }
4588c0984e5SSebastian Reichel 
4598c0984e5SSebastian Reichel /*
4608c0984e5SSebastian Reichel  * Return a battery charge value in µAh
4618c0984e5SSebastian Reichel  * Or < 0 if something fails.
4628c0984e5SSebastian Reichel  */
4638c0984e5SSebastian Reichel static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
4648c0984e5SSebastian Reichel {
4658c0984e5SSebastian Reichel 	int charge;
4668c0984e5SSebastian Reichel 
4678c0984e5SSebastian Reichel 	charge = bq27xxx_read(di, reg, false);
4688c0984e5SSebastian Reichel 	if (charge < 0) {
4698c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
4708c0984e5SSebastian Reichel 			reg, charge);
4718c0984e5SSebastian Reichel 		return charge;
4728c0984e5SSebastian Reichel 	}
4738c0984e5SSebastian Reichel 
4748c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
4758c0984e5SSebastian Reichel 		charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
4768c0984e5SSebastian Reichel 	else
4778c0984e5SSebastian Reichel 		charge *= 1000;
4788c0984e5SSebastian Reichel 
4798c0984e5SSebastian Reichel 	return charge;
4808c0984e5SSebastian Reichel }
4818c0984e5SSebastian Reichel 
4828c0984e5SSebastian Reichel /*
4838c0984e5SSebastian Reichel  * Return the battery Nominal available capacity in µAh
4848c0984e5SSebastian Reichel  * Or < 0 if something fails.
4858c0984e5SSebastian Reichel  */
4868c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
4878c0984e5SSebastian Reichel {
4888c0984e5SSebastian Reichel 	int flags;
4898c0984e5SSebastian Reichel 
4908c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
4918c0984e5SSebastian Reichel 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
4928c0984e5SSebastian Reichel 		if (flags >= 0 && (flags & BQ27000_FLAG_CI))
4938c0984e5SSebastian Reichel 			return -ENODATA;
4948c0984e5SSebastian Reichel 	}
4958c0984e5SSebastian Reichel 
4968c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
4978c0984e5SSebastian Reichel }
4988c0984e5SSebastian Reichel 
4998c0984e5SSebastian Reichel /*
5008c0984e5SSebastian Reichel  * Return the battery Full Charge Capacity in µAh
5018c0984e5SSebastian Reichel  * Or < 0 if something fails.
5028c0984e5SSebastian Reichel  */
5038c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
5048c0984e5SSebastian Reichel {
5058c0984e5SSebastian Reichel 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
5068c0984e5SSebastian Reichel }
5078c0984e5SSebastian Reichel 
5088c0984e5SSebastian Reichel /*
5098c0984e5SSebastian Reichel  * Return the Design Capacity in µAh
5108c0984e5SSebastian Reichel  * Or < 0 if something fails.
5118c0984e5SSebastian Reichel  */
5128c0984e5SSebastian Reichel static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
5138c0984e5SSebastian Reichel {
5148c0984e5SSebastian Reichel 	int dcap;
5158c0984e5SSebastian Reichel 
5168c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
5178c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
5188c0984e5SSebastian Reichel 	else
5198c0984e5SSebastian Reichel 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
5208c0984e5SSebastian Reichel 
5218c0984e5SSebastian Reichel 	if (dcap < 0) {
5228c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading initial last measured discharge\n");
5238c0984e5SSebastian Reichel 		return dcap;
5248c0984e5SSebastian Reichel 	}
5258c0984e5SSebastian Reichel 
5268c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
5278c0984e5SSebastian Reichel 		dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
5288c0984e5SSebastian Reichel 	else
5298c0984e5SSebastian Reichel 		dcap *= 1000;
5308c0984e5SSebastian Reichel 
5318c0984e5SSebastian Reichel 	return dcap;
5328c0984e5SSebastian Reichel }
5338c0984e5SSebastian Reichel 
5348c0984e5SSebastian Reichel /*
5358c0984e5SSebastian Reichel  * Return the battery Available energy in µWh
5368c0984e5SSebastian Reichel  * Or < 0 if something fails.
5378c0984e5SSebastian Reichel  */
5388c0984e5SSebastian Reichel static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
5398c0984e5SSebastian Reichel {
5408c0984e5SSebastian Reichel 	int ae;
5418c0984e5SSebastian Reichel 
5428c0984e5SSebastian Reichel 	ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
5438c0984e5SSebastian Reichel 	if (ae < 0) {
5448c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading available energy\n");
5458c0984e5SSebastian Reichel 		return ae;
5468c0984e5SSebastian Reichel 	}
5478c0984e5SSebastian Reichel 
5488c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
5498c0984e5SSebastian Reichel 		ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
5508c0984e5SSebastian Reichel 	else
5518c0984e5SSebastian Reichel 		ae *= 1000;
5528c0984e5SSebastian Reichel 
5538c0984e5SSebastian Reichel 	return ae;
5548c0984e5SSebastian Reichel }
5558c0984e5SSebastian Reichel 
5568c0984e5SSebastian Reichel /*
5578c0984e5SSebastian Reichel  * Return the battery temperature in tenths of degree Kelvin
5588c0984e5SSebastian Reichel  * Or < 0 if something fails.
5598c0984e5SSebastian Reichel  */
5608c0984e5SSebastian Reichel static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
5618c0984e5SSebastian Reichel {
5628c0984e5SSebastian Reichel 	int temp;
5638c0984e5SSebastian Reichel 
5648c0984e5SSebastian Reichel 	temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
5658c0984e5SSebastian Reichel 	if (temp < 0) {
5668c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading temperature\n");
5678c0984e5SSebastian Reichel 		return temp;
5688c0984e5SSebastian Reichel 	}
5698c0984e5SSebastian Reichel 
5708c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
5718c0984e5SSebastian Reichel 		temp = 5 * temp / 2;
5728c0984e5SSebastian Reichel 
5738c0984e5SSebastian Reichel 	return temp;
5748c0984e5SSebastian Reichel }
5758c0984e5SSebastian Reichel 
5768c0984e5SSebastian Reichel /*
5778c0984e5SSebastian Reichel  * Return the battery Cycle count total
5788c0984e5SSebastian Reichel  * Or < 0 if something fails.
5798c0984e5SSebastian Reichel  */
5808c0984e5SSebastian Reichel static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
5818c0984e5SSebastian Reichel {
5828c0984e5SSebastian Reichel 	int cyct;
5838c0984e5SSebastian Reichel 
5848c0984e5SSebastian Reichel 	cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
5858c0984e5SSebastian Reichel 	if (cyct < 0)
5868c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading cycle count total\n");
5878c0984e5SSebastian Reichel 
5888c0984e5SSebastian Reichel 	return cyct;
5898c0984e5SSebastian Reichel }
5908c0984e5SSebastian Reichel 
5918c0984e5SSebastian Reichel /*
5928c0984e5SSebastian Reichel  * Read a time register.
5938c0984e5SSebastian Reichel  * Return < 0 if something fails.
5948c0984e5SSebastian Reichel  */
5958c0984e5SSebastian Reichel static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
5968c0984e5SSebastian Reichel {
5978c0984e5SSebastian Reichel 	int tval;
5988c0984e5SSebastian Reichel 
5998c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, reg, false);
6008c0984e5SSebastian Reichel 	if (tval < 0) {
6018c0984e5SSebastian Reichel 		dev_dbg(di->dev, "error reading time register %02x: %d\n",
6028c0984e5SSebastian Reichel 			reg, tval);
6038c0984e5SSebastian Reichel 		return tval;
6048c0984e5SSebastian Reichel 	}
6058c0984e5SSebastian Reichel 
6068c0984e5SSebastian Reichel 	if (tval == 65535)
6078c0984e5SSebastian Reichel 		return -ENODATA;
6088c0984e5SSebastian Reichel 
6098c0984e5SSebastian Reichel 	return tval * 60;
6108c0984e5SSebastian Reichel }
6118c0984e5SSebastian Reichel 
6128c0984e5SSebastian Reichel /*
6138c0984e5SSebastian Reichel  * Read an average power register.
6148c0984e5SSebastian Reichel  * Return < 0 if something fails.
6158c0984e5SSebastian Reichel  */
6168c0984e5SSebastian Reichel static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
6178c0984e5SSebastian Reichel {
6188c0984e5SSebastian Reichel 	int tval;
6198c0984e5SSebastian Reichel 
6208c0984e5SSebastian Reichel 	tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
6218c0984e5SSebastian Reichel 	if (tval < 0) {
6228c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading average power register  %02x: %d\n",
6238c0984e5SSebastian Reichel 			BQ27XXX_REG_AP, tval);
6248c0984e5SSebastian Reichel 		return tval;
6258c0984e5SSebastian Reichel 	}
6268c0984e5SSebastian Reichel 
6278c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
6288c0984e5SSebastian Reichel 		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
6298c0984e5SSebastian Reichel 	else
6308c0984e5SSebastian Reichel 		return tval;
6318c0984e5SSebastian Reichel }
6328c0984e5SSebastian Reichel 
6338c0984e5SSebastian Reichel /*
6348c0984e5SSebastian Reichel  * Returns true if a battery over temperature condition is detected
6358c0984e5SSebastian Reichel  */
6368c0984e5SSebastian Reichel static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
6378c0984e5SSebastian Reichel {
6388c0984e5SSebastian Reichel 	if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
6398c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
6408c0984e5SSebastian Reichel 	if (di->chip == BQ27530 || di->chip == BQ27421)
6418c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_OT;
6428c0984e5SSebastian Reichel 
6438c0984e5SSebastian Reichel 	return false;
6448c0984e5SSebastian Reichel }
6458c0984e5SSebastian Reichel 
6468c0984e5SSebastian Reichel /*
6478c0984e5SSebastian Reichel  * Returns true if a battery under temperature condition is detected
6488c0984e5SSebastian Reichel  */
6498c0984e5SSebastian Reichel static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
6508c0984e5SSebastian Reichel {
6518c0984e5SSebastian Reichel 	if (di->chip == BQ27530 || di->chip == BQ27421)
6528c0984e5SSebastian Reichel 		return flags & BQ27XXX_FLAG_UT;
6538c0984e5SSebastian Reichel 
6548c0984e5SSebastian Reichel 	return false;
6558c0984e5SSebastian Reichel }
6568c0984e5SSebastian Reichel 
6578c0984e5SSebastian Reichel /*
6588c0984e5SSebastian Reichel  * Returns true if a low state of charge condition is detected
6598c0984e5SSebastian Reichel  */
6608c0984e5SSebastian Reichel static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
6618c0984e5SSebastian Reichel {
6628c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010)
6638c0984e5SSebastian Reichel 		return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
6648c0984e5SSebastian Reichel 	else
6658c0984e5SSebastian Reichel 		return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
6668c0984e5SSebastian Reichel }
6678c0984e5SSebastian Reichel 
6688c0984e5SSebastian Reichel /*
6698c0984e5SSebastian Reichel  * Read flag register.
6708c0984e5SSebastian Reichel  * Return < 0 if something fails.
6718c0984e5SSebastian Reichel  */
6728c0984e5SSebastian Reichel static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
6738c0984e5SSebastian Reichel {
6748c0984e5SSebastian Reichel 	int flags;
675e4a404a0SH. Nikolaus Schaller 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
6768c0984e5SSebastian Reichel 
677e4a404a0SH. Nikolaus Schaller 	flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
6788c0984e5SSebastian Reichel 	if (flags < 0) {
6798c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading flag register:%d\n", flags);
6808c0984e5SSebastian Reichel 		return flags;
6818c0984e5SSebastian Reichel 	}
6828c0984e5SSebastian Reichel 
6838c0984e5SSebastian Reichel 	/* Unlikely but important to return first */
6848c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_overtemp(di, flags)))
6858c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_OVERHEAT;
6868c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_undertemp(di, flags)))
6878c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_COLD;
6888c0984e5SSebastian Reichel 	if (unlikely(bq27xxx_battery_dead(di, flags)))
6898c0984e5SSebastian Reichel 		return POWER_SUPPLY_HEALTH_DEAD;
6908c0984e5SSebastian Reichel 
6918c0984e5SSebastian Reichel 	return POWER_SUPPLY_HEALTH_GOOD;
6928c0984e5SSebastian Reichel }
6938c0984e5SSebastian Reichel 
6948c0984e5SSebastian Reichel void bq27xxx_battery_update(struct bq27xxx_device_info *di)
6958c0984e5SSebastian Reichel {
6968c0984e5SSebastian Reichel 	struct bq27xxx_reg_cache cache = {0, };
6978c0984e5SSebastian Reichel 	bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
6988c0984e5SSebastian Reichel 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
6998c0984e5SSebastian Reichel 
7008c0984e5SSebastian Reichel 	cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
7018c0984e5SSebastian Reichel 	if ((cache.flags & 0xff) == 0xff)
7028c0984e5SSebastian Reichel 		cache.flags = -1; /* read error */
7038c0984e5SSebastian Reichel 	if (cache.flags >= 0) {
7048c0984e5SSebastian Reichel 		cache.temperature = bq27xxx_battery_read_temperature(di);
7058c0984e5SSebastian Reichel 		if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
7068c0984e5SSebastian Reichel 			dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
7078c0984e5SSebastian Reichel 			cache.capacity = -ENODATA;
7088c0984e5SSebastian Reichel 			cache.energy = -ENODATA;
7098c0984e5SSebastian Reichel 			cache.time_to_empty = -ENODATA;
7108c0984e5SSebastian Reichel 			cache.time_to_empty_avg = -ENODATA;
7118c0984e5SSebastian Reichel 			cache.time_to_full = -ENODATA;
7128c0984e5SSebastian Reichel 			cache.charge_full = -ENODATA;
7138c0984e5SSebastian Reichel 			cache.health = -ENODATA;
7148c0984e5SSebastian Reichel 		} else {
7158c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
7168c0984e5SSebastian Reichel 				cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
7178c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
7188c0984e5SSebastian Reichel 				cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
7198c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
7208c0984e5SSebastian Reichel 				cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
7218c0984e5SSebastian Reichel 			cache.charge_full = bq27xxx_battery_read_fcc(di);
7228c0984e5SSebastian Reichel 			cache.capacity = bq27xxx_battery_read_soc(di);
7238c0984e5SSebastian Reichel 			if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
7248c0984e5SSebastian Reichel 				cache.energy = bq27xxx_battery_read_energy(di);
7258c0984e5SSebastian Reichel 			cache.health = bq27xxx_battery_read_health(di);
7268c0984e5SSebastian Reichel 		}
7278c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
7288c0984e5SSebastian Reichel 			cache.cycle_count = bq27xxx_battery_read_cyct(di);
7298c0984e5SSebastian Reichel 		if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
7308c0984e5SSebastian Reichel 			cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
7318c0984e5SSebastian Reichel 
7328c0984e5SSebastian Reichel 		/* We only have to read charge design full once */
7338c0984e5SSebastian Reichel 		if (di->charge_design_full <= 0)
7348c0984e5SSebastian Reichel 			di->charge_design_full = bq27xxx_battery_read_dcap(di);
7358c0984e5SSebastian Reichel 	}
7368c0984e5SSebastian Reichel 
7378c0984e5SSebastian Reichel 	if (di->cache.capacity != cache.capacity)
7388c0984e5SSebastian Reichel 		power_supply_changed(di->bat);
7398c0984e5SSebastian Reichel 
7408c0984e5SSebastian Reichel 	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
7418c0984e5SSebastian Reichel 		di->cache = cache;
7428c0984e5SSebastian Reichel 
7438c0984e5SSebastian Reichel 	di->last_update = jiffies;
7448c0984e5SSebastian Reichel }
7458c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
7468c0984e5SSebastian Reichel 
7478c0984e5SSebastian Reichel static void bq27xxx_battery_poll(struct work_struct *work)
7488c0984e5SSebastian Reichel {
7498c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di =
7508c0984e5SSebastian Reichel 			container_of(work, struct bq27xxx_device_info,
7518c0984e5SSebastian Reichel 				     work.work);
7528c0984e5SSebastian Reichel 
7538c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
7548c0984e5SSebastian Reichel 
7558c0984e5SSebastian Reichel 	if (poll_interval > 0)
7568c0984e5SSebastian Reichel 		schedule_delayed_work(&di->work, poll_interval * HZ);
7578c0984e5SSebastian Reichel }
7588c0984e5SSebastian Reichel 
7598c0984e5SSebastian Reichel /*
7608c0984e5SSebastian Reichel  * Return the battery average current in µA
7618c0984e5SSebastian Reichel  * Note that current can be negative signed as well
7628c0984e5SSebastian Reichel  * Or 0 if something fails.
7638c0984e5SSebastian Reichel  */
7648c0984e5SSebastian Reichel static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
7658c0984e5SSebastian Reichel 				   union power_supply_propval *val)
7668c0984e5SSebastian Reichel {
7678c0984e5SSebastian Reichel 	int curr;
7688c0984e5SSebastian Reichel 	int flags;
7698c0984e5SSebastian Reichel 
7708c0984e5SSebastian Reichel 	curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
7718c0984e5SSebastian Reichel 	if (curr < 0) {
7728c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading current\n");
7738c0984e5SSebastian Reichel 		return curr;
7748c0984e5SSebastian Reichel 	}
7758c0984e5SSebastian Reichel 
7768c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
777e4a404a0SH. Nikolaus Schaller 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
7788c0984e5SSebastian Reichel 		if (flags & BQ27000_FLAG_CHGS) {
7798c0984e5SSebastian Reichel 			dev_dbg(di->dev, "negative current!\n");
7808c0984e5SSebastian Reichel 			curr = -curr;
7818c0984e5SSebastian Reichel 		}
7828c0984e5SSebastian Reichel 
7838c0984e5SSebastian Reichel 		val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
7848c0984e5SSebastian Reichel 	} else {
7858c0984e5SSebastian Reichel 		/* Other gauges return signed value */
7868c0984e5SSebastian Reichel 		val->intval = (int)((s16)curr) * 1000;
7878c0984e5SSebastian Reichel 	}
7888c0984e5SSebastian Reichel 
7898c0984e5SSebastian Reichel 	return 0;
7908c0984e5SSebastian Reichel }
7918c0984e5SSebastian Reichel 
7928c0984e5SSebastian Reichel static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
7938c0984e5SSebastian Reichel 				  union power_supply_propval *val)
7948c0984e5SSebastian Reichel {
7958c0984e5SSebastian Reichel 	int status;
7968c0984e5SSebastian Reichel 
7978c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
7988c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
7998c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
8008c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_CHGS)
8018c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
8028c0984e5SSebastian Reichel 		else if (power_supply_am_i_supplied(di->bat))
8038c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
8048c0984e5SSebastian Reichel 		else
8058c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
8068c0984e5SSebastian Reichel 	} else {
8078c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
8088c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_FULL;
8098c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_DSC)
8108c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_DISCHARGING;
8118c0984e5SSebastian Reichel 		else
8128c0984e5SSebastian Reichel 			status = POWER_SUPPLY_STATUS_CHARGING;
8138c0984e5SSebastian Reichel 	}
8148c0984e5SSebastian Reichel 
8158c0984e5SSebastian Reichel 	val->intval = status;
8168c0984e5SSebastian Reichel 
8178c0984e5SSebastian Reichel 	return 0;
8188c0984e5SSebastian Reichel }
8198c0984e5SSebastian Reichel 
8208c0984e5SSebastian Reichel static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
8218c0984e5SSebastian Reichel 					  union power_supply_propval *val)
8228c0984e5SSebastian Reichel {
8238c0984e5SSebastian Reichel 	int level;
8248c0984e5SSebastian Reichel 
8258c0984e5SSebastian Reichel 	if (di->chip == BQ27000 || di->chip == BQ27010) {
8268c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27000_FLAG_FC)
8278c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
8288c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDV1)
8298c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
8308c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27000_FLAG_EDVF)
8318c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
8328c0984e5SSebastian Reichel 		else
8338c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
8348c0984e5SSebastian Reichel 	} else {
8358c0984e5SSebastian Reichel 		if (di->cache.flags & BQ27XXX_FLAG_FC)
8368c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
8378c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
8388c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
8398c0984e5SSebastian Reichel 		else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
8408c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
8418c0984e5SSebastian Reichel 		else
8428c0984e5SSebastian Reichel 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
8438c0984e5SSebastian Reichel 	}
8448c0984e5SSebastian Reichel 
8458c0984e5SSebastian Reichel 	val->intval = level;
8468c0984e5SSebastian Reichel 
8478c0984e5SSebastian Reichel 	return 0;
8488c0984e5SSebastian Reichel }
8498c0984e5SSebastian Reichel 
8508c0984e5SSebastian Reichel /*
8518c0984e5SSebastian Reichel  * Return the battery Voltage in millivolts
8528c0984e5SSebastian Reichel  * Or < 0 if something fails.
8538c0984e5SSebastian Reichel  */
8548c0984e5SSebastian Reichel static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
8558c0984e5SSebastian Reichel 				   union power_supply_propval *val)
8568c0984e5SSebastian Reichel {
8578c0984e5SSebastian Reichel 	int volt;
8588c0984e5SSebastian Reichel 
8598c0984e5SSebastian Reichel 	volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
8608c0984e5SSebastian Reichel 	if (volt < 0) {
8618c0984e5SSebastian Reichel 		dev_err(di->dev, "error reading voltage\n");
8628c0984e5SSebastian Reichel 		return volt;
8638c0984e5SSebastian Reichel 	}
8648c0984e5SSebastian Reichel 
8658c0984e5SSebastian Reichel 	val->intval = volt * 1000;
8668c0984e5SSebastian Reichel 
8678c0984e5SSebastian Reichel 	return 0;
8688c0984e5SSebastian Reichel }
8698c0984e5SSebastian Reichel 
8708c0984e5SSebastian Reichel static int bq27xxx_simple_value(int value,
8718c0984e5SSebastian Reichel 				union power_supply_propval *val)
8728c0984e5SSebastian Reichel {
8738c0984e5SSebastian Reichel 	if (value < 0)
8748c0984e5SSebastian Reichel 		return value;
8758c0984e5SSebastian Reichel 
8768c0984e5SSebastian Reichel 	val->intval = value;
8778c0984e5SSebastian Reichel 
8788c0984e5SSebastian Reichel 	return 0;
8798c0984e5SSebastian Reichel }
8808c0984e5SSebastian Reichel 
8818c0984e5SSebastian Reichel static int bq27xxx_battery_get_property(struct power_supply *psy,
8828c0984e5SSebastian Reichel 					enum power_supply_property psp,
8838c0984e5SSebastian Reichel 					union power_supply_propval *val)
8848c0984e5SSebastian Reichel {
8858c0984e5SSebastian Reichel 	int ret = 0;
8868c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
8878c0984e5SSebastian Reichel 
8888c0984e5SSebastian Reichel 	mutex_lock(&di->lock);
8898c0984e5SSebastian Reichel 	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
8908c0984e5SSebastian Reichel 		cancel_delayed_work_sync(&di->work);
8918c0984e5SSebastian Reichel 		bq27xxx_battery_poll(&di->work.work);
8928c0984e5SSebastian Reichel 	}
8938c0984e5SSebastian Reichel 	mutex_unlock(&di->lock);
8948c0984e5SSebastian Reichel 
8958c0984e5SSebastian Reichel 	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
8968c0984e5SSebastian Reichel 		return -ENODEV;
8978c0984e5SSebastian Reichel 
8988c0984e5SSebastian Reichel 	switch (psp) {
8998c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
9008c0984e5SSebastian Reichel 		ret = bq27xxx_battery_status(di, val);
9018c0984e5SSebastian Reichel 		break;
9028c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
9038c0984e5SSebastian Reichel 		ret = bq27xxx_battery_voltage(di, val);
9048c0984e5SSebastian Reichel 		break;
9058c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
9068c0984e5SSebastian Reichel 		val->intval = di->cache.flags < 0 ? 0 : 1;
9078c0984e5SSebastian Reichel 		break;
9088c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
9098c0984e5SSebastian Reichel 		ret = bq27xxx_battery_current(di, val);
9108c0984e5SSebastian Reichel 		break;
9118c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
9128c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.capacity, val);
9138c0984e5SSebastian Reichel 		break;
9148c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
9158c0984e5SSebastian Reichel 		ret = bq27xxx_battery_capacity_level(di, val);
9168c0984e5SSebastian Reichel 		break;
9178c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
9188c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.temperature, val);
9198c0984e5SSebastian Reichel 		if (ret == 0)
9208c0984e5SSebastian Reichel 			val->intval -= 2731; /* convert decidegree k to c */
9218c0984e5SSebastian Reichel 		break;
9228c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
9238c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
9248c0984e5SSebastian Reichel 		break;
9258c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
9268c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
9278c0984e5SSebastian Reichel 		break;
9288c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
9298c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
9308c0984e5SSebastian Reichel 		break;
9318c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
9328c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
9338c0984e5SSebastian Reichel 		break;
9348c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
9358c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
9368c0984e5SSebastian Reichel 		break;
9378c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
9388c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.charge_full, val);
9398c0984e5SSebastian Reichel 		break;
9408c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
9418c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->charge_design_full, val);
9428c0984e5SSebastian Reichel 		break;
9438c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
9448c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
9458c0984e5SSebastian Reichel 		break;
9468c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ENERGY_NOW:
9478c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.energy, val);
9488c0984e5SSebastian Reichel 		break;
9498c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_POWER_AVG:
9508c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.power_avg, val);
9518c0984e5SSebastian Reichel 		break;
9528c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
9538c0984e5SSebastian Reichel 		ret = bq27xxx_simple_value(di->cache.health, val);
9548c0984e5SSebastian Reichel 		break;
9558c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
9568c0984e5SSebastian Reichel 		val->strval = BQ27XXX_MANUFACTURER;
9578c0984e5SSebastian Reichel 		break;
9588c0984e5SSebastian Reichel 	default:
9598c0984e5SSebastian Reichel 		return -EINVAL;
9608c0984e5SSebastian Reichel 	}
9618c0984e5SSebastian Reichel 
9628c0984e5SSebastian Reichel 	return ret;
9638c0984e5SSebastian Reichel }
9648c0984e5SSebastian Reichel 
9658c0984e5SSebastian Reichel static void bq27xxx_external_power_changed(struct power_supply *psy)
9668c0984e5SSebastian Reichel {
9678c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
9688c0984e5SSebastian Reichel 
9698c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
9708c0984e5SSebastian Reichel 	schedule_delayed_work(&di->work, 0);
9718c0984e5SSebastian Reichel }
9728c0984e5SSebastian Reichel 
9738c0984e5SSebastian Reichel int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
9748c0984e5SSebastian Reichel {
9758c0984e5SSebastian Reichel 	struct power_supply_desc *psy_desc;
9768c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = { .drv_data = di, };
9778c0984e5SSebastian Reichel 
9788c0984e5SSebastian Reichel 	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
9798c0984e5SSebastian Reichel 	mutex_init(&di->lock);
9808c0984e5SSebastian Reichel 	di->regs = bq27xxx_regs[di->chip];
9818c0984e5SSebastian Reichel 
9828c0984e5SSebastian Reichel 	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
9838c0984e5SSebastian Reichel 	if (!psy_desc)
9848c0984e5SSebastian Reichel 		return -ENOMEM;
9858c0984e5SSebastian Reichel 
9868c0984e5SSebastian Reichel 	psy_desc->name = di->name;
9878c0984e5SSebastian Reichel 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
9888c0984e5SSebastian Reichel 	psy_desc->properties = bq27xxx_battery_props[di->chip].props;
9898c0984e5SSebastian Reichel 	psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
9908c0984e5SSebastian Reichel 	psy_desc->get_property = bq27xxx_battery_get_property;
9918c0984e5SSebastian Reichel 	psy_desc->external_power_changed = bq27xxx_external_power_changed;
9928c0984e5SSebastian Reichel 
9938c0984e5SSebastian Reichel 	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
9948c0984e5SSebastian Reichel 	if (IS_ERR(di->bat)) {
9958c0984e5SSebastian Reichel 		dev_err(di->dev, "failed to register battery\n");
9968c0984e5SSebastian Reichel 		return PTR_ERR(di->bat);
9978c0984e5SSebastian Reichel 	}
9988c0984e5SSebastian Reichel 
9998c0984e5SSebastian Reichel 	dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
10008c0984e5SSebastian Reichel 
10018c0984e5SSebastian Reichel 	bq27xxx_battery_update(di);
10028c0984e5SSebastian Reichel 
10031d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
10041d72706fSMatt Ranostay 	list_add(&di->list, &bq27xxx_battery_devices);
10051d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
10061d72706fSMatt Ranostay 
10078c0984e5SSebastian Reichel 	return 0;
10088c0984e5SSebastian Reichel }
10098c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
10108c0984e5SSebastian Reichel 
10118c0984e5SSebastian Reichel void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
10128c0984e5SSebastian Reichel {
10138c0984e5SSebastian Reichel 	/*
10148c0984e5SSebastian Reichel 	 * power_supply_unregister call bq27xxx_battery_get_property which
10158c0984e5SSebastian Reichel 	 * call bq27xxx_battery_poll.
10168c0984e5SSebastian Reichel 	 * Make sure that bq27xxx_battery_poll will not call
10178c0984e5SSebastian Reichel 	 * schedule_delayed_work again after unregister (which cause OOPS).
10188c0984e5SSebastian Reichel 	 */
10198c0984e5SSebastian Reichel 	poll_interval = 0;
10208c0984e5SSebastian Reichel 
10218c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->work);
10228c0984e5SSebastian Reichel 
10238c0984e5SSebastian Reichel 	power_supply_unregister(di->bat);
10248c0984e5SSebastian Reichel 
10251d72706fSMatt Ranostay 	mutex_lock(&bq27xxx_list_lock);
10261d72706fSMatt Ranostay 	list_del(&di->list);
10271d72706fSMatt Ranostay 	mutex_unlock(&bq27xxx_list_lock);
10281d72706fSMatt Ranostay 
10298c0984e5SSebastian Reichel 	mutex_destroy(&di->lock);
10308c0984e5SSebastian Reichel }
10318c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
10328c0984e5SSebastian Reichel 
10338c0984e5SSebastian Reichel static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
10348c0984e5SSebastian Reichel 					 bool single)
10358c0984e5SSebastian Reichel {
10368c0984e5SSebastian Reichel 	struct device *dev = di->dev;
10378c0984e5SSebastian Reichel 	struct bq27xxx_platform_data *pdata = dev->platform_data;
10388c0984e5SSebastian Reichel 	unsigned int timeout = 3;
10398c0984e5SSebastian Reichel 	int upper, lower;
10408c0984e5SSebastian Reichel 	int temp;
10418c0984e5SSebastian Reichel 
10428c0984e5SSebastian Reichel 	if (!single) {
10438c0984e5SSebastian Reichel 		/* Make sure the value has not changed in between reading the
10448c0984e5SSebastian Reichel 		 * lower and the upper part */
10458c0984e5SSebastian Reichel 		upper = pdata->read(dev, reg + 1);
10468c0984e5SSebastian Reichel 		do {
10478c0984e5SSebastian Reichel 			temp = upper;
10488c0984e5SSebastian Reichel 			if (upper < 0)
10498c0984e5SSebastian Reichel 				return upper;
10508c0984e5SSebastian Reichel 
10518c0984e5SSebastian Reichel 			lower = pdata->read(dev, reg);
10528c0984e5SSebastian Reichel 			if (lower < 0)
10538c0984e5SSebastian Reichel 				return lower;
10548c0984e5SSebastian Reichel 
10558c0984e5SSebastian Reichel 			upper = pdata->read(dev, reg + 1);
10568c0984e5SSebastian Reichel 		} while (temp != upper && --timeout);
10578c0984e5SSebastian Reichel 
10588c0984e5SSebastian Reichel 		if (timeout == 0)
10598c0984e5SSebastian Reichel 			return -EIO;
10608c0984e5SSebastian Reichel 
10618c0984e5SSebastian Reichel 		return (upper << 8) | lower;
10628c0984e5SSebastian Reichel 	}
10638c0984e5SSebastian Reichel 
10648c0984e5SSebastian Reichel 	return pdata->read(dev, reg);
10658c0984e5SSebastian Reichel }
10668c0984e5SSebastian Reichel 
10678c0984e5SSebastian Reichel static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
10688c0984e5SSebastian Reichel {
10698c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di;
10708c0984e5SSebastian Reichel 	struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
10718c0984e5SSebastian Reichel 
10728c0984e5SSebastian Reichel 	if (!pdata) {
10738c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no platform_data supplied\n");
10748c0984e5SSebastian Reichel 		return -EINVAL;
10758c0984e5SSebastian Reichel 	}
10768c0984e5SSebastian Reichel 
10778c0984e5SSebastian Reichel 	if (!pdata->read) {
10788c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no hdq read callback supplied\n");
10798c0984e5SSebastian Reichel 		return -EINVAL;
10808c0984e5SSebastian Reichel 	}
10818c0984e5SSebastian Reichel 
10828c0984e5SSebastian Reichel 	if (!pdata->chip) {
10838c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no device supplied\n");
10848c0984e5SSebastian Reichel 		return -EINVAL;
10858c0984e5SSebastian Reichel 	}
10868c0984e5SSebastian Reichel 
10878c0984e5SSebastian Reichel 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
10888c0984e5SSebastian Reichel 	if (!di)
10898c0984e5SSebastian Reichel 		return -ENOMEM;
10908c0984e5SSebastian Reichel 
10918c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, di);
10928c0984e5SSebastian Reichel 
10938c0984e5SSebastian Reichel 	di->dev = &pdev->dev;
10948c0984e5SSebastian Reichel 	di->chip = pdata->chip;
10958c0984e5SSebastian Reichel 	di->name = pdata->name ?: dev_name(&pdev->dev);
10968c0984e5SSebastian Reichel 	di->bus.read = bq27xxx_battery_platform_read;
10978c0984e5SSebastian Reichel 
10988c0984e5SSebastian Reichel 	return bq27xxx_battery_setup(di);
10998c0984e5SSebastian Reichel }
11008c0984e5SSebastian Reichel 
11018c0984e5SSebastian Reichel static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
11028c0984e5SSebastian Reichel {
11038c0984e5SSebastian Reichel 	struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
11048c0984e5SSebastian Reichel 
11058c0984e5SSebastian Reichel 	bq27xxx_battery_teardown(di);
11068c0984e5SSebastian Reichel 
11078c0984e5SSebastian Reichel 	return 0;
11088c0984e5SSebastian Reichel }
11098c0984e5SSebastian Reichel 
11108c0984e5SSebastian Reichel static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
11118c0984e5SSebastian Reichel 	{ "bq27000-battery", },
11128c0984e5SSebastian Reichel 	{ /* sentinel */ }
11138c0984e5SSebastian Reichel };
11148c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
11158c0984e5SSebastian Reichel 
11168c0984e5SSebastian Reichel #ifdef CONFIG_OF
11178c0984e5SSebastian Reichel static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
11188c0984e5SSebastian Reichel 	{ .compatible = "ti,bq27000" },
11198c0984e5SSebastian Reichel 	{},
11208c0984e5SSebastian Reichel };
11218c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
11228c0984e5SSebastian Reichel #endif
11238c0984e5SSebastian Reichel 
11248c0984e5SSebastian Reichel static struct platform_driver bq27xxx_battery_platform_driver = {
11258c0984e5SSebastian Reichel 	.probe	= bq27xxx_battery_platform_probe,
11268c0984e5SSebastian Reichel 	.remove = bq27xxx_battery_platform_remove,
11278c0984e5SSebastian Reichel 	.driver = {
11288c0984e5SSebastian Reichel 		.name = "bq27000-battery",
11298c0984e5SSebastian Reichel 		.of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
11308c0984e5SSebastian Reichel 	},
11318c0984e5SSebastian Reichel 	.id_table = bq27xxx_battery_platform_id_table,
11328c0984e5SSebastian Reichel };
11338c0984e5SSebastian Reichel module_platform_driver(bq27xxx_battery_platform_driver);
11348c0984e5SSebastian Reichel 
11358c0984e5SSebastian Reichel MODULE_ALIAS("platform:bq27000-battery");
11368c0984e5SSebastian Reichel 
11378c0984e5SSebastian Reichel MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
11388c0984e5SSebastian Reichel MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
11398c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
1140