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 }, 148818e3012SChris Lapa [BQ2750X] = { 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 }, 1673bee9ea1SAndrew F. Davis [BQ27510] = { 1683bee9ea1SAndrew F. Davis [BQ27XXX_REG_CTRL] = 0x00, 1693bee9ea1SAndrew F. Davis [BQ27XXX_REG_TEMP] = 0x06, 1703bee9ea1SAndrew F. Davis [BQ27XXX_REG_INT_TEMP] = 0x28, 1713bee9ea1SAndrew F. Davis [BQ27XXX_REG_VOLT] = 0x08, 1723bee9ea1SAndrew F. Davis [BQ27XXX_REG_AI] = 0x14, 1733bee9ea1SAndrew F. Davis [BQ27XXX_REG_FLAGS] = 0x0a, 1743bee9ea1SAndrew F. Davis [BQ27XXX_REG_TTE] = 0x16, 1753bee9ea1SAndrew F. Davis [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 1763bee9ea1SAndrew F. Davis [BQ27XXX_REG_TTES] = 0x1a, 1773bee9ea1SAndrew F. Davis [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 1783bee9ea1SAndrew F. Davis [BQ27XXX_REG_NAC] = 0x0c, 1793bee9ea1SAndrew F. Davis [BQ27XXX_REG_FCC] = 0x12, 1803bee9ea1SAndrew F. Davis [BQ27XXX_REG_CYCT] = 0x1e, 1813bee9ea1SAndrew F. Davis [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 1823bee9ea1SAndrew F. Davis [BQ27XXX_REG_SOC] = 0x20, 1833bee9ea1SAndrew F. Davis [BQ27XXX_REG_DCAP] = 0x2e, 1843bee9ea1SAndrew F. Davis [BQ27XXX_REG_AP] = INVALID_REG_ADDR, 1853bee9ea1SAndrew F. Davis }, 1868c0984e5SSebastian Reichel [BQ27530] = { 1878c0984e5SSebastian Reichel [BQ27XXX_REG_CTRL] = 0x00, 1888c0984e5SSebastian Reichel [BQ27XXX_REG_TEMP] = 0x06, 1898c0984e5SSebastian Reichel [BQ27XXX_REG_INT_TEMP] = 0x32, 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] = INVALID_REG_ADDR, 2038c0984e5SSebastian Reichel [BQ27XXX_REG_AP] = 0x24, 2048c0984e5SSebastian Reichel }, 2058c0984e5SSebastian Reichel [BQ27541] = { 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] = 0x3c, 2228c0984e5SSebastian Reichel [BQ27XXX_REG_AP] = 0x24, 2238c0984e5SSebastian Reichel }, 2248c0984e5SSebastian Reichel [BQ27545] = { 2258c0984e5SSebastian Reichel [BQ27XXX_REG_CTRL] = 0x00, 2268c0984e5SSebastian Reichel [BQ27XXX_REG_TEMP] = 0x06, 2278c0984e5SSebastian Reichel [BQ27XXX_REG_INT_TEMP] = 0x28, 2288c0984e5SSebastian Reichel [BQ27XXX_REG_VOLT] = 0x08, 2298c0984e5SSebastian Reichel [BQ27XXX_REG_AI] = 0x14, 2308c0984e5SSebastian Reichel [BQ27XXX_REG_FLAGS] = 0x0a, 2318c0984e5SSebastian Reichel [BQ27XXX_REG_TTE] = 0x16, 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] = 0x0c, 2368c0984e5SSebastian Reichel [BQ27XXX_REG_FCC] = 0x12, 2378c0984e5SSebastian Reichel [BQ27XXX_REG_CYCT] = 0x2a, 2388c0984e5SSebastian Reichel [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 2398c0984e5SSebastian Reichel [BQ27XXX_REG_SOC] = 0x2c, 2408c0984e5SSebastian Reichel [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, 2418c0984e5SSebastian Reichel [BQ27XXX_REG_AP] = 0x24, 2428c0984e5SSebastian Reichel }, 2438c0984e5SSebastian Reichel [BQ27421] = { 2448c0984e5SSebastian Reichel [BQ27XXX_REG_CTRL] = 0x00, 2458c0984e5SSebastian Reichel [BQ27XXX_REG_TEMP] = 0x02, 2468c0984e5SSebastian Reichel [BQ27XXX_REG_INT_TEMP] = 0x1e, 2478c0984e5SSebastian Reichel [BQ27XXX_REG_VOLT] = 0x04, 2488c0984e5SSebastian Reichel [BQ27XXX_REG_AI] = 0x10, 2498c0984e5SSebastian Reichel [BQ27XXX_REG_FLAGS] = 0x06, 2508c0984e5SSebastian Reichel [BQ27XXX_REG_TTE] = INVALID_REG_ADDR, 2518c0984e5SSebastian Reichel [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 2528c0984e5SSebastian Reichel [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, 2538c0984e5SSebastian Reichel [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 2548c0984e5SSebastian Reichel [BQ27XXX_REG_NAC] = 0x08, 2558c0984e5SSebastian Reichel [BQ27XXX_REG_FCC] = 0x0e, 2568c0984e5SSebastian Reichel [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR, 2578c0984e5SSebastian Reichel [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 2588c0984e5SSebastian Reichel [BQ27XXX_REG_SOC] = 0x1c, 2598c0984e5SSebastian Reichel [BQ27XXX_REG_DCAP] = 0x3c, 2608c0984e5SSebastian Reichel [BQ27XXX_REG_AP] = 0x18, 2618c0984e5SSebastian Reichel }, 2628c0984e5SSebastian Reichel }; 2638c0984e5SSebastian Reichel 2648c0984e5SSebastian Reichel static enum power_supply_property bq27000_battery_props[] = { 2658c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 2668c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 2678c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 2688c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 2698c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 2708c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 2718c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 2728c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 2738c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 2748c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 2758c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 2768c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 2778c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 2788c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 2798c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 2808c0984e5SSebastian Reichel POWER_SUPPLY_PROP_ENERGY_NOW, 2818c0984e5SSebastian Reichel POWER_SUPPLY_PROP_POWER_AVG, 2828c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 2838c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 2848c0984e5SSebastian Reichel }; 2858c0984e5SSebastian Reichel 2868c0984e5SSebastian Reichel static enum power_supply_property bq27010_battery_props[] = { 2878c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 2888c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 2898c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 2908c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 2918c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 2928c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 2938c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 2948c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 2958c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 2968c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 2978c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 2988c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 2998c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 3008c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 3018c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3028c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3038c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3048c0984e5SSebastian Reichel }; 3058c0984e5SSebastian Reichel 306818e3012SChris Lapa static enum power_supply_property bq2750x_battery_props[] = { 3078c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 3088c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 3098c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 3108c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 3118c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 3128c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 3138c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 3148c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 3158c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 3168c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 3178c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 3188c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 3198c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3208c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3218c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3228c0984e5SSebastian Reichel }; 3238c0984e5SSebastian Reichel 3243bee9ea1SAndrew F. Davis static enum power_supply_property bq27510_battery_props[] = { 3253bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_STATUS, 3263bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_PRESENT, 3273bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_VOLTAGE_NOW, 3283bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CURRENT_NOW, 3293bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CAPACITY, 3303bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CAPACITY_LEVEL, 3313bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_TEMP, 3323bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 3333bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_TECHNOLOGY, 3343bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CHARGE_FULL, 3353bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CHARGE_NOW, 3363bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 3373bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CYCLE_COUNT, 3383bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_HEALTH, 3393bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_MANUFACTURER, 3403bee9ea1SAndrew F. Davis }; 3413bee9ea1SAndrew F. Davis 3428c0984e5SSebastian Reichel static enum power_supply_property bq27530_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_POWER_AVG, 3558c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3568c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3578c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3588c0984e5SSebastian Reichel }; 3598c0984e5SSebastian Reichel 3608c0984e5SSebastian Reichel static enum power_supply_property bq27541_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_TIME_TO_EMPTY_NOW, 3698c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 3708c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 3718c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 3728c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 3738c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3748c0984e5SSebastian Reichel POWER_SUPPLY_PROP_POWER_AVG, 3758c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3768c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3778c0984e5SSebastian Reichel }; 3788c0984e5SSebastian Reichel 3798c0984e5SSebastian Reichel static enum power_supply_property bq27545_battery_props[] = { 3808c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 3818c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 3828c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 3838c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 3848c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 3858c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 3868c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 3878c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 3888c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 3898c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 3908c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 3918c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3928c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3938c0984e5SSebastian Reichel POWER_SUPPLY_PROP_POWER_AVG, 3948c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3958c0984e5SSebastian Reichel }; 3968c0984e5SSebastian Reichel 3978c0984e5SSebastian Reichel static enum power_supply_property bq27421_battery_props[] = { 3988c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 3998c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 4008c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 4018c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 4028c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 4038c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 4048c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 4058c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 4068c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 4078c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 4088c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 4098c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 4108c0984e5SSebastian Reichel }; 4118c0984e5SSebastian Reichel 4128c0984e5SSebastian Reichel #define BQ27XXX_PROP(_id, _prop) \ 4138c0984e5SSebastian Reichel [_id] = { \ 4148c0984e5SSebastian Reichel .props = _prop, \ 4158c0984e5SSebastian Reichel .size = ARRAY_SIZE(_prop), \ 4168c0984e5SSebastian Reichel } 4178c0984e5SSebastian Reichel 4188c0984e5SSebastian Reichel static struct { 4198c0984e5SSebastian Reichel enum power_supply_property *props; 4208c0984e5SSebastian Reichel size_t size; 4218c0984e5SSebastian Reichel } bq27xxx_battery_props[] = { 4228c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27000, bq27000_battery_props), 4238c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27010, bq27010_battery_props), 424818e3012SChris Lapa BQ27XXX_PROP(BQ2750X, bq2750x_battery_props), 4253bee9ea1SAndrew F. Davis BQ27XXX_PROP(BQ27510, bq27510_battery_props), 4268c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27530, bq27530_battery_props), 4278c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27541, bq27541_battery_props), 4288c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27545, bq27545_battery_props), 4298c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27421, bq27421_battery_props), 4308c0984e5SSebastian Reichel }; 4318c0984e5SSebastian Reichel 4321d72706fSMatt Ranostay static DEFINE_MUTEX(bq27xxx_list_lock); 4331d72706fSMatt Ranostay static LIST_HEAD(bq27xxx_battery_devices); 4341d72706fSMatt Ranostay 4351d72706fSMatt Ranostay static int poll_interval_param_set(const char *val, const struct kernel_param *kp) 4361d72706fSMatt Ranostay { 4371d72706fSMatt Ranostay struct bq27xxx_device_info *di; 438950b6c2dSMatt Ranostay unsigned int prev_val = *(unsigned int *) kp->arg; 4391d72706fSMatt Ranostay int ret; 4401d72706fSMatt Ranostay 4411d72706fSMatt Ranostay ret = param_set_uint(val, kp); 442950b6c2dSMatt Ranostay if (ret < 0 || prev_val == *(unsigned int *) kp->arg) 4431d72706fSMatt Ranostay return ret; 4441d72706fSMatt Ranostay 4451d72706fSMatt Ranostay mutex_lock(&bq27xxx_list_lock); 4461d72706fSMatt Ranostay list_for_each_entry(di, &bq27xxx_battery_devices, list) { 4471d72706fSMatt Ranostay cancel_delayed_work_sync(&di->work); 4481d72706fSMatt Ranostay schedule_delayed_work(&di->work, 0); 4491d72706fSMatt Ranostay } 4501d72706fSMatt Ranostay mutex_unlock(&bq27xxx_list_lock); 4511d72706fSMatt Ranostay 4521d72706fSMatt Ranostay return ret; 4531d72706fSMatt Ranostay } 4541d72706fSMatt Ranostay 4551d72706fSMatt Ranostay static const struct kernel_param_ops param_ops_poll_interval = { 4561d72706fSMatt Ranostay .get = param_get_uint, 4571d72706fSMatt Ranostay .set = poll_interval_param_set, 4581d72706fSMatt Ranostay }; 4591d72706fSMatt Ranostay 4608c0984e5SSebastian Reichel static unsigned int poll_interval = 360; 4611d72706fSMatt Ranostay module_param_cb(poll_interval, ¶m_ops_poll_interval, &poll_interval, 0644); 4628c0984e5SSebastian Reichel MODULE_PARM_DESC(poll_interval, 4638c0984e5SSebastian Reichel "battery poll interval in seconds - 0 disables polling"); 4648c0984e5SSebastian Reichel 4658c0984e5SSebastian Reichel /* 4668c0984e5SSebastian Reichel * Common code for BQ27xxx devices 4678c0984e5SSebastian Reichel */ 4688c0984e5SSebastian Reichel 4698c0984e5SSebastian Reichel static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index, 4708c0984e5SSebastian Reichel bool single) 4718c0984e5SSebastian Reichel { 4728c0984e5SSebastian Reichel /* Reports EINVAL for invalid/missing registers */ 4738c0984e5SSebastian Reichel if (!di || di->regs[reg_index] == INVALID_REG_ADDR) 4748c0984e5SSebastian Reichel return -EINVAL; 4758c0984e5SSebastian Reichel 4768c0984e5SSebastian Reichel return di->bus.read(di, di->regs[reg_index], single); 4778c0984e5SSebastian Reichel } 4788c0984e5SSebastian Reichel 4798c0984e5SSebastian Reichel /* 4808c0984e5SSebastian Reichel * Return the battery State-of-Charge 4818c0984e5SSebastian Reichel * Or < 0 if something fails. 4828c0984e5SSebastian Reichel */ 4838c0984e5SSebastian Reichel static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di) 4848c0984e5SSebastian Reichel { 4858c0984e5SSebastian Reichel int soc; 4868c0984e5SSebastian Reichel 4878c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 4888c0984e5SSebastian Reichel soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true); 4898c0984e5SSebastian Reichel else 4908c0984e5SSebastian Reichel soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false); 4918c0984e5SSebastian Reichel 4928c0984e5SSebastian Reichel if (soc < 0) 4938c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading State-of-Charge\n"); 4948c0984e5SSebastian Reichel 4958c0984e5SSebastian Reichel return soc; 4968c0984e5SSebastian Reichel } 4978c0984e5SSebastian Reichel 4988c0984e5SSebastian Reichel /* 4998c0984e5SSebastian Reichel * Return a battery charge value in µAh 5008c0984e5SSebastian Reichel * Or < 0 if something fails. 5018c0984e5SSebastian Reichel */ 5028c0984e5SSebastian Reichel static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg) 5038c0984e5SSebastian Reichel { 5048c0984e5SSebastian Reichel int charge; 5058c0984e5SSebastian Reichel 5068c0984e5SSebastian Reichel charge = bq27xxx_read(di, reg, false); 5078c0984e5SSebastian Reichel if (charge < 0) { 5088c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading charge register %02x: %d\n", 5098c0984e5SSebastian Reichel reg, charge); 5108c0984e5SSebastian Reichel return charge; 5118c0984e5SSebastian Reichel } 5128c0984e5SSebastian Reichel 5138c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 5148c0984e5SSebastian Reichel charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 5158c0984e5SSebastian Reichel else 5168c0984e5SSebastian Reichel charge *= 1000; 5178c0984e5SSebastian Reichel 5188c0984e5SSebastian Reichel return charge; 5198c0984e5SSebastian Reichel } 5208c0984e5SSebastian Reichel 5218c0984e5SSebastian Reichel /* 5228c0984e5SSebastian Reichel * Return the battery Nominal available capacity in µAh 5238c0984e5SSebastian Reichel * Or < 0 if something fails. 5248c0984e5SSebastian Reichel */ 5258c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di) 5268c0984e5SSebastian Reichel { 5278c0984e5SSebastian Reichel int flags; 5288c0984e5SSebastian Reichel 5298c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 5308c0984e5SSebastian Reichel flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); 5318c0984e5SSebastian Reichel if (flags >= 0 && (flags & BQ27000_FLAG_CI)) 5328c0984e5SSebastian Reichel return -ENODATA; 5338c0984e5SSebastian Reichel } 5348c0984e5SSebastian Reichel 5358c0984e5SSebastian Reichel return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC); 5368c0984e5SSebastian Reichel } 5378c0984e5SSebastian Reichel 5388c0984e5SSebastian Reichel /* 5398c0984e5SSebastian Reichel * Return the battery Full Charge Capacity in µAh 5408c0984e5SSebastian Reichel * Or < 0 if something fails. 5418c0984e5SSebastian Reichel */ 5428c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di) 5438c0984e5SSebastian Reichel { 5448c0984e5SSebastian Reichel return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC); 5458c0984e5SSebastian Reichel } 5468c0984e5SSebastian Reichel 5478c0984e5SSebastian Reichel /* 5488c0984e5SSebastian Reichel * Return the Design Capacity in µAh 5498c0984e5SSebastian Reichel * Or < 0 if something fails. 5508c0984e5SSebastian Reichel */ 5518c0984e5SSebastian Reichel static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di) 5528c0984e5SSebastian Reichel { 5538c0984e5SSebastian Reichel int dcap; 5548c0984e5SSebastian Reichel 5558c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 5568c0984e5SSebastian Reichel dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true); 5578c0984e5SSebastian Reichel else 5588c0984e5SSebastian Reichel dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false); 5598c0984e5SSebastian Reichel 5608c0984e5SSebastian Reichel if (dcap < 0) { 5618c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading initial last measured discharge\n"); 5628c0984e5SSebastian Reichel return dcap; 5638c0984e5SSebastian Reichel } 5648c0984e5SSebastian Reichel 5658c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 5668c0984e5SSebastian Reichel dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 5678c0984e5SSebastian Reichel else 5688c0984e5SSebastian Reichel dcap *= 1000; 5698c0984e5SSebastian Reichel 5708c0984e5SSebastian Reichel return dcap; 5718c0984e5SSebastian Reichel } 5728c0984e5SSebastian Reichel 5738c0984e5SSebastian Reichel /* 5748c0984e5SSebastian Reichel * Return the battery Available energy in µWh 5758c0984e5SSebastian Reichel * Or < 0 if something fails. 5768c0984e5SSebastian Reichel */ 5778c0984e5SSebastian Reichel static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di) 5788c0984e5SSebastian Reichel { 5798c0984e5SSebastian Reichel int ae; 5808c0984e5SSebastian Reichel 5818c0984e5SSebastian Reichel ae = bq27xxx_read(di, BQ27XXX_REG_AE, false); 5828c0984e5SSebastian Reichel if (ae < 0) { 5838c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading available energy\n"); 5848c0984e5SSebastian Reichel return ae; 5858c0984e5SSebastian Reichel } 5868c0984e5SSebastian Reichel 5878c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 5888c0984e5SSebastian Reichel ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS; 5898c0984e5SSebastian Reichel else 5908c0984e5SSebastian Reichel ae *= 1000; 5918c0984e5SSebastian Reichel 5928c0984e5SSebastian Reichel return ae; 5938c0984e5SSebastian Reichel } 5948c0984e5SSebastian Reichel 5958c0984e5SSebastian Reichel /* 5968c0984e5SSebastian Reichel * Return the battery temperature in tenths of degree Kelvin 5978c0984e5SSebastian Reichel * Or < 0 if something fails. 5988c0984e5SSebastian Reichel */ 5998c0984e5SSebastian Reichel static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di) 6008c0984e5SSebastian Reichel { 6018c0984e5SSebastian Reichel int temp; 6028c0984e5SSebastian Reichel 6038c0984e5SSebastian Reichel temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false); 6048c0984e5SSebastian Reichel if (temp < 0) { 6058c0984e5SSebastian Reichel dev_err(di->dev, "error reading temperature\n"); 6068c0984e5SSebastian Reichel return temp; 6078c0984e5SSebastian Reichel } 6088c0984e5SSebastian Reichel 6098c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 6108c0984e5SSebastian Reichel temp = 5 * temp / 2; 6118c0984e5SSebastian Reichel 6128c0984e5SSebastian Reichel return temp; 6138c0984e5SSebastian Reichel } 6148c0984e5SSebastian Reichel 6158c0984e5SSebastian Reichel /* 6168c0984e5SSebastian Reichel * Return the battery Cycle count total 6178c0984e5SSebastian Reichel * Or < 0 if something fails. 6188c0984e5SSebastian Reichel */ 6198c0984e5SSebastian Reichel static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di) 6208c0984e5SSebastian Reichel { 6218c0984e5SSebastian Reichel int cyct; 6228c0984e5SSebastian Reichel 6238c0984e5SSebastian Reichel cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false); 6248c0984e5SSebastian Reichel if (cyct < 0) 6258c0984e5SSebastian Reichel dev_err(di->dev, "error reading cycle count total\n"); 6268c0984e5SSebastian Reichel 6278c0984e5SSebastian Reichel return cyct; 6288c0984e5SSebastian Reichel } 6298c0984e5SSebastian Reichel 6308c0984e5SSebastian Reichel /* 6318c0984e5SSebastian Reichel * Read a time register. 6328c0984e5SSebastian Reichel * Return < 0 if something fails. 6338c0984e5SSebastian Reichel */ 6348c0984e5SSebastian Reichel static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg) 6358c0984e5SSebastian Reichel { 6368c0984e5SSebastian Reichel int tval; 6378c0984e5SSebastian Reichel 6388c0984e5SSebastian Reichel tval = bq27xxx_read(di, reg, false); 6398c0984e5SSebastian Reichel if (tval < 0) { 6408c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading time register %02x: %d\n", 6418c0984e5SSebastian Reichel reg, tval); 6428c0984e5SSebastian Reichel return tval; 6438c0984e5SSebastian Reichel } 6448c0984e5SSebastian Reichel 6458c0984e5SSebastian Reichel if (tval == 65535) 6468c0984e5SSebastian Reichel return -ENODATA; 6478c0984e5SSebastian Reichel 6488c0984e5SSebastian Reichel return tval * 60; 6498c0984e5SSebastian Reichel } 6508c0984e5SSebastian Reichel 6518c0984e5SSebastian Reichel /* 6528c0984e5SSebastian Reichel * Read an average power register. 6538c0984e5SSebastian Reichel * Return < 0 if something fails. 6548c0984e5SSebastian Reichel */ 6558c0984e5SSebastian Reichel static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di) 6568c0984e5SSebastian Reichel { 6578c0984e5SSebastian Reichel int tval; 6588c0984e5SSebastian Reichel 6598c0984e5SSebastian Reichel tval = bq27xxx_read(di, BQ27XXX_REG_AP, false); 6608c0984e5SSebastian Reichel if (tval < 0) { 6618c0984e5SSebastian Reichel dev_err(di->dev, "error reading average power register %02x: %d\n", 6628c0984e5SSebastian Reichel BQ27XXX_REG_AP, tval); 6638c0984e5SSebastian Reichel return tval; 6648c0984e5SSebastian Reichel } 6658c0984e5SSebastian Reichel 6668c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 6678c0984e5SSebastian Reichel return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS; 6688c0984e5SSebastian Reichel else 6698c0984e5SSebastian Reichel return tval; 6708c0984e5SSebastian Reichel } 6718c0984e5SSebastian Reichel 6728c0984e5SSebastian Reichel /* 6738c0984e5SSebastian Reichel * Returns true if a battery over temperature condition is detected 6748c0984e5SSebastian Reichel */ 6758c0984e5SSebastian Reichel static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags) 6768c0984e5SSebastian Reichel { 677e839a448SChris Lapa switch (di->chip) { 678818e3012SChris Lapa case BQ2750X: 679e839a448SChris Lapa case BQ27510: 680e839a448SChris Lapa case BQ27541: 681e839a448SChris Lapa case BQ27545: 6828c0984e5SSebastian Reichel return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD); 683e839a448SChris Lapa case BQ27530: 684e839a448SChris Lapa case BQ27421: 6858c0984e5SSebastian Reichel return flags & BQ27XXX_FLAG_OT; 686e839a448SChris Lapa default: 6878c0984e5SSebastian Reichel return false; 6888c0984e5SSebastian Reichel } 689e839a448SChris Lapa } 6908c0984e5SSebastian Reichel 6918c0984e5SSebastian Reichel /* 6928c0984e5SSebastian Reichel * Returns true if a battery under temperature condition is detected 6938c0984e5SSebastian Reichel */ 6948c0984e5SSebastian Reichel static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags) 6958c0984e5SSebastian Reichel { 6968c0984e5SSebastian Reichel if (di->chip == BQ27530 || di->chip == BQ27421) 6978c0984e5SSebastian Reichel return flags & BQ27XXX_FLAG_UT; 6988c0984e5SSebastian Reichel 6998c0984e5SSebastian Reichel return false; 7008c0984e5SSebastian Reichel } 7018c0984e5SSebastian Reichel 7028c0984e5SSebastian Reichel /* 7038c0984e5SSebastian Reichel * Returns true if a low state of charge condition is detected 7048c0984e5SSebastian Reichel */ 7058c0984e5SSebastian Reichel static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags) 7068c0984e5SSebastian Reichel { 7078c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 7088c0984e5SSebastian Reichel return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF); 7098c0984e5SSebastian Reichel else 7108c0984e5SSebastian Reichel return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF); 7118c0984e5SSebastian Reichel } 7128c0984e5SSebastian Reichel 7138c0984e5SSebastian Reichel /* 7148c0984e5SSebastian Reichel * Read flag register. 7158c0984e5SSebastian Reichel * Return < 0 if something fails. 7168c0984e5SSebastian Reichel */ 7178c0984e5SSebastian Reichel static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di) 7188c0984e5SSebastian Reichel { 7198c0984e5SSebastian Reichel int flags; 720e4a404a0SH. Nikolaus Schaller bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; 7218c0984e5SSebastian Reichel 722e4a404a0SH. Nikolaus Schaller flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); 7238c0984e5SSebastian Reichel if (flags < 0) { 7248c0984e5SSebastian Reichel dev_err(di->dev, "error reading flag register:%d\n", flags); 7258c0984e5SSebastian Reichel return flags; 7268c0984e5SSebastian Reichel } 7278c0984e5SSebastian Reichel 7288c0984e5SSebastian Reichel /* Unlikely but important to return first */ 7298c0984e5SSebastian Reichel if (unlikely(bq27xxx_battery_overtemp(di, flags))) 7308c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_OVERHEAT; 7318c0984e5SSebastian Reichel if (unlikely(bq27xxx_battery_undertemp(di, flags))) 7328c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_COLD; 7338c0984e5SSebastian Reichel if (unlikely(bq27xxx_battery_dead(di, flags))) 7348c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_DEAD; 7358c0984e5SSebastian Reichel 7368c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_GOOD; 7378c0984e5SSebastian Reichel } 7388c0984e5SSebastian Reichel 7398c0984e5SSebastian Reichel void bq27xxx_battery_update(struct bq27xxx_device_info *di) 7408c0984e5SSebastian Reichel { 7418c0984e5SSebastian Reichel struct bq27xxx_reg_cache cache = {0, }; 7428c0984e5SSebastian Reichel bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010; 7438c0984e5SSebastian Reichel bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; 7448c0984e5SSebastian Reichel 7458c0984e5SSebastian Reichel cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); 7468c0984e5SSebastian Reichel if ((cache.flags & 0xff) == 0xff) 7478c0984e5SSebastian Reichel cache.flags = -1; /* read error */ 7488c0984e5SSebastian Reichel if (cache.flags >= 0) { 7498c0984e5SSebastian Reichel cache.temperature = bq27xxx_battery_read_temperature(di); 7508c0984e5SSebastian Reichel if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) { 7518c0984e5SSebastian Reichel dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n"); 7528c0984e5SSebastian Reichel cache.capacity = -ENODATA; 7538c0984e5SSebastian Reichel cache.energy = -ENODATA; 7548c0984e5SSebastian Reichel cache.time_to_empty = -ENODATA; 7558c0984e5SSebastian Reichel cache.time_to_empty_avg = -ENODATA; 7568c0984e5SSebastian Reichel cache.time_to_full = -ENODATA; 7578c0984e5SSebastian Reichel cache.charge_full = -ENODATA; 7588c0984e5SSebastian Reichel cache.health = -ENODATA; 7598c0984e5SSebastian Reichel } else { 7608c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR) 7618c0984e5SSebastian Reichel cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE); 7628c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR) 7638c0984e5SSebastian Reichel cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP); 7648c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR) 7658c0984e5SSebastian Reichel cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF); 7668c0984e5SSebastian Reichel cache.charge_full = bq27xxx_battery_read_fcc(di); 7678c0984e5SSebastian Reichel cache.capacity = bq27xxx_battery_read_soc(di); 7688c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR) 7698c0984e5SSebastian Reichel cache.energy = bq27xxx_battery_read_energy(di); 7708c0984e5SSebastian Reichel cache.health = bq27xxx_battery_read_health(di); 7718c0984e5SSebastian Reichel } 7728c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR) 7738c0984e5SSebastian Reichel cache.cycle_count = bq27xxx_battery_read_cyct(di); 7748c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR) 7758c0984e5SSebastian Reichel cache.power_avg = bq27xxx_battery_read_pwr_avg(di); 7768c0984e5SSebastian Reichel 7778c0984e5SSebastian Reichel /* We only have to read charge design full once */ 7788c0984e5SSebastian Reichel if (di->charge_design_full <= 0) 7798c0984e5SSebastian Reichel di->charge_design_full = bq27xxx_battery_read_dcap(di); 7808c0984e5SSebastian Reichel } 7818c0984e5SSebastian Reichel 7828c0984e5SSebastian Reichel if (di->cache.capacity != cache.capacity) 7838c0984e5SSebastian Reichel power_supply_changed(di->bat); 7848c0984e5SSebastian Reichel 7858c0984e5SSebastian Reichel if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) 7868c0984e5SSebastian Reichel di->cache = cache; 7878c0984e5SSebastian Reichel 7888c0984e5SSebastian Reichel di->last_update = jiffies; 7898c0984e5SSebastian Reichel } 7908c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_update); 7918c0984e5SSebastian Reichel 7928c0984e5SSebastian Reichel static void bq27xxx_battery_poll(struct work_struct *work) 7938c0984e5SSebastian Reichel { 7948c0984e5SSebastian Reichel struct bq27xxx_device_info *di = 7958c0984e5SSebastian Reichel container_of(work, struct bq27xxx_device_info, 7968c0984e5SSebastian Reichel work.work); 7978c0984e5SSebastian Reichel 7988c0984e5SSebastian Reichel bq27xxx_battery_update(di); 7998c0984e5SSebastian Reichel 8008c0984e5SSebastian Reichel if (poll_interval > 0) 8018c0984e5SSebastian Reichel schedule_delayed_work(&di->work, poll_interval * HZ); 8028c0984e5SSebastian Reichel } 8038c0984e5SSebastian Reichel 8048c0984e5SSebastian Reichel /* 8058c0984e5SSebastian Reichel * Return the battery average current in µA 8068c0984e5SSebastian Reichel * Note that current can be negative signed as well 8078c0984e5SSebastian Reichel * Or 0 if something fails. 8088c0984e5SSebastian Reichel */ 8098c0984e5SSebastian Reichel static int bq27xxx_battery_current(struct bq27xxx_device_info *di, 8108c0984e5SSebastian Reichel union power_supply_propval *val) 8118c0984e5SSebastian Reichel { 8128c0984e5SSebastian Reichel int curr; 8138c0984e5SSebastian Reichel int flags; 8148c0984e5SSebastian Reichel 8158c0984e5SSebastian Reichel curr = bq27xxx_read(di, BQ27XXX_REG_AI, false); 8168c0984e5SSebastian Reichel if (curr < 0) { 8178c0984e5SSebastian Reichel dev_err(di->dev, "error reading current\n"); 8188c0984e5SSebastian Reichel return curr; 8198c0984e5SSebastian Reichel } 8208c0984e5SSebastian Reichel 8218c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 822e4a404a0SH. Nikolaus Schaller flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); 8238c0984e5SSebastian Reichel if (flags & BQ27000_FLAG_CHGS) { 8248c0984e5SSebastian Reichel dev_dbg(di->dev, "negative current!\n"); 8258c0984e5SSebastian Reichel curr = -curr; 8268c0984e5SSebastian Reichel } 8278c0984e5SSebastian Reichel 8288c0984e5SSebastian Reichel val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 8298c0984e5SSebastian Reichel } else { 8308c0984e5SSebastian Reichel /* Other gauges return signed value */ 8318c0984e5SSebastian Reichel val->intval = (int)((s16)curr) * 1000; 8328c0984e5SSebastian Reichel } 8338c0984e5SSebastian Reichel 8348c0984e5SSebastian Reichel return 0; 8358c0984e5SSebastian Reichel } 8368c0984e5SSebastian Reichel 8378c0984e5SSebastian Reichel static int bq27xxx_battery_status(struct bq27xxx_device_info *di, 8388c0984e5SSebastian Reichel union power_supply_propval *val) 8398c0984e5SSebastian Reichel { 8408c0984e5SSebastian Reichel int status; 8418c0984e5SSebastian Reichel 8428c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 8438c0984e5SSebastian Reichel if (di->cache.flags & BQ27000_FLAG_FC) 8448c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_FULL; 8458c0984e5SSebastian Reichel else if (di->cache.flags & BQ27000_FLAG_CHGS) 8468c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_CHARGING; 8478c0984e5SSebastian Reichel else if (power_supply_am_i_supplied(di->bat)) 8488c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_NOT_CHARGING; 8498c0984e5SSebastian Reichel else 8508c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_DISCHARGING; 8518c0984e5SSebastian Reichel } else { 8528c0984e5SSebastian Reichel if (di->cache.flags & BQ27XXX_FLAG_FC) 8538c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_FULL; 8548c0984e5SSebastian Reichel else if (di->cache.flags & BQ27XXX_FLAG_DSC) 8558c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_DISCHARGING; 8568c0984e5SSebastian Reichel else 8578c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_CHARGING; 8588c0984e5SSebastian Reichel } 8598c0984e5SSebastian Reichel 8608c0984e5SSebastian Reichel val->intval = status; 8618c0984e5SSebastian Reichel 8628c0984e5SSebastian Reichel return 0; 8638c0984e5SSebastian Reichel } 8648c0984e5SSebastian Reichel 8658c0984e5SSebastian Reichel static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di, 8668c0984e5SSebastian Reichel union power_supply_propval *val) 8678c0984e5SSebastian Reichel { 8688c0984e5SSebastian Reichel int level; 8698c0984e5SSebastian Reichel 8708c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 8718c0984e5SSebastian Reichel if (di->cache.flags & BQ27000_FLAG_FC) 8728c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 8738c0984e5SSebastian Reichel else if (di->cache.flags & BQ27000_FLAG_EDV1) 8748c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 8758c0984e5SSebastian Reichel else if (di->cache.flags & BQ27000_FLAG_EDVF) 8768c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 8778c0984e5SSebastian Reichel else 8788c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 8798c0984e5SSebastian Reichel } else { 8808c0984e5SSebastian Reichel if (di->cache.flags & BQ27XXX_FLAG_FC) 8818c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 8828c0984e5SSebastian Reichel else if (di->cache.flags & BQ27XXX_FLAG_SOC1) 8838c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 8848c0984e5SSebastian Reichel else if (di->cache.flags & BQ27XXX_FLAG_SOCF) 8858c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 8868c0984e5SSebastian Reichel else 8878c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 8888c0984e5SSebastian Reichel } 8898c0984e5SSebastian Reichel 8908c0984e5SSebastian Reichel val->intval = level; 8918c0984e5SSebastian Reichel 8928c0984e5SSebastian Reichel return 0; 8938c0984e5SSebastian Reichel } 8948c0984e5SSebastian Reichel 8958c0984e5SSebastian Reichel /* 8968c0984e5SSebastian Reichel * Return the battery Voltage in millivolts 8978c0984e5SSebastian Reichel * Or < 0 if something fails. 8988c0984e5SSebastian Reichel */ 8998c0984e5SSebastian Reichel static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di, 9008c0984e5SSebastian Reichel union power_supply_propval *val) 9018c0984e5SSebastian Reichel { 9028c0984e5SSebastian Reichel int volt; 9038c0984e5SSebastian Reichel 9048c0984e5SSebastian Reichel volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false); 9058c0984e5SSebastian Reichel if (volt < 0) { 9068c0984e5SSebastian Reichel dev_err(di->dev, "error reading voltage\n"); 9078c0984e5SSebastian Reichel return volt; 9088c0984e5SSebastian Reichel } 9098c0984e5SSebastian Reichel 9108c0984e5SSebastian Reichel val->intval = volt * 1000; 9118c0984e5SSebastian Reichel 9128c0984e5SSebastian Reichel return 0; 9138c0984e5SSebastian Reichel } 9148c0984e5SSebastian Reichel 9158c0984e5SSebastian Reichel static int bq27xxx_simple_value(int value, 9168c0984e5SSebastian Reichel union power_supply_propval *val) 9178c0984e5SSebastian Reichel { 9188c0984e5SSebastian Reichel if (value < 0) 9198c0984e5SSebastian Reichel return value; 9208c0984e5SSebastian Reichel 9218c0984e5SSebastian Reichel val->intval = value; 9228c0984e5SSebastian Reichel 9238c0984e5SSebastian Reichel return 0; 9248c0984e5SSebastian Reichel } 9258c0984e5SSebastian Reichel 9268c0984e5SSebastian Reichel static int bq27xxx_battery_get_property(struct power_supply *psy, 9278c0984e5SSebastian Reichel enum power_supply_property psp, 9288c0984e5SSebastian Reichel union power_supply_propval *val) 9298c0984e5SSebastian Reichel { 9308c0984e5SSebastian Reichel int ret = 0; 9318c0984e5SSebastian Reichel struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); 9328c0984e5SSebastian Reichel 9338c0984e5SSebastian Reichel mutex_lock(&di->lock); 9348c0984e5SSebastian Reichel if (time_is_before_jiffies(di->last_update + 5 * HZ)) { 9358c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->work); 9368c0984e5SSebastian Reichel bq27xxx_battery_poll(&di->work.work); 9378c0984e5SSebastian Reichel } 9388c0984e5SSebastian Reichel mutex_unlock(&di->lock); 9398c0984e5SSebastian Reichel 9408c0984e5SSebastian Reichel if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) 9418c0984e5SSebastian Reichel return -ENODEV; 9428c0984e5SSebastian Reichel 9438c0984e5SSebastian Reichel switch (psp) { 9448c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_STATUS: 9458c0984e5SSebastian Reichel ret = bq27xxx_battery_status(di, val); 9468c0984e5SSebastian Reichel break; 9478c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_VOLTAGE_NOW: 9488c0984e5SSebastian Reichel ret = bq27xxx_battery_voltage(di, val); 9498c0984e5SSebastian Reichel break; 9508c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_PRESENT: 9518c0984e5SSebastian Reichel val->intval = di->cache.flags < 0 ? 0 : 1; 9528c0984e5SSebastian Reichel break; 9538c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CURRENT_NOW: 9548c0984e5SSebastian Reichel ret = bq27xxx_battery_current(di, val); 9558c0984e5SSebastian Reichel break; 9568c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CAPACITY: 9578c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.capacity, val); 9588c0984e5SSebastian Reichel break; 9598c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 9608c0984e5SSebastian Reichel ret = bq27xxx_battery_capacity_level(di, val); 9618c0984e5SSebastian Reichel break; 9628c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TEMP: 9638c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.temperature, val); 9648c0984e5SSebastian Reichel if (ret == 0) 9658c0984e5SSebastian Reichel val->intval -= 2731; /* convert decidegree k to c */ 9668c0984e5SSebastian Reichel break; 9678c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 9688c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.time_to_empty, val); 9698c0984e5SSebastian Reichel break; 9708c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 9718c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val); 9728c0984e5SSebastian Reichel break; 9738c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: 9748c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.time_to_full, val); 9758c0984e5SSebastian Reichel break; 9768c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TECHNOLOGY: 9778c0984e5SSebastian Reichel val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 9788c0984e5SSebastian Reichel break; 9798c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW: 9808c0984e5SSebastian Reichel ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val); 9818c0984e5SSebastian Reichel break; 9828c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL: 9838c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.charge_full, val); 9848c0984e5SSebastian Reichel break; 9858c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 9868c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->charge_design_full, val); 9878c0984e5SSebastian Reichel break; 9888c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CYCLE_COUNT: 9898c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.cycle_count, val); 9908c0984e5SSebastian Reichel break; 9918c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_ENERGY_NOW: 9928c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.energy, val); 9938c0984e5SSebastian Reichel break; 9948c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_POWER_AVG: 9958c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.power_avg, val); 9968c0984e5SSebastian Reichel break; 9978c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_HEALTH: 9988c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.health, val); 9998c0984e5SSebastian Reichel break; 10008c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_MANUFACTURER: 10018c0984e5SSebastian Reichel val->strval = BQ27XXX_MANUFACTURER; 10028c0984e5SSebastian Reichel break; 10038c0984e5SSebastian Reichel default: 10048c0984e5SSebastian Reichel return -EINVAL; 10058c0984e5SSebastian Reichel } 10068c0984e5SSebastian Reichel 10078c0984e5SSebastian Reichel return ret; 10088c0984e5SSebastian Reichel } 10098c0984e5SSebastian Reichel 10108c0984e5SSebastian Reichel static void bq27xxx_external_power_changed(struct power_supply *psy) 10118c0984e5SSebastian Reichel { 10128c0984e5SSebastian Reichel struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); 10138c0984e5SSebastian Reichel 10148c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->work); 10158c0984e5SSebastian Reichel schedule_delayed_work(&di->work, 0); 10168c0984e5SSebastian Reichel } 10178c0984e5SSebastian Reichel 10188c0984e5SSebastian Reichel int bq27xxx_battery_setup(struct bq27xxx_device_info *di) 10198c0984e5SSebastian Reichel { 10208c0984e5SSebastian Reichel struct power_supply_desc *psy_desc; 10218c0984e5SSebastian Reichel struct power_supply_config psy_cfg = { .drv_data = di, }; 10228c0984e5SSebastian Reichel 10238c0984e5SSebastian Reichel INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll); 10248c0984e5SSebastian Reichel mutex_init(&di->lock); 10258c0984e5SSebastian Reichel di->regs = bq27xxx_regs[di->chip]; 10268c0984e5SSebastian Reichel 10278c0984e5SSebastian Reichel psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL); 10288c0984e5SSebastian Reichel if (!psy_desc) 10298c0984e5SSebastian Reichel return -ENOMEM; 10308c0984e5SSebastian Reichel 10318c0984e5SSebastian Reichel psy_desc->name = di->name; 10328c0984e5SSebastian Reichel psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; 10338c0984e5SSebastian Reichel psy_desc->properties = bq27xxx_battery_props[di->chip].props; 10348c0984e5SSebastian Reichel psy_desc->num_properties = bq27xxx_battery_props[di->chip].size; 10358c0984e5SSebastian Reichel psy_desc->get_property = bq27xxx_battery_get_property; 10368c0984e5SSebastian Reichel psy_desc->external_power_changed = bq27xxx_external_power_changed; 10378c0984e5SSebastian Reichel 10388c0984e5SSebastian Reichel di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg); 10398c0984e5SSebastian Reichel if (IS_ERR(di->bat)) { 10408c0984e5SSebastian Reichel dev_err(di->dev, "failed to register battery\n"); 10418c0984e5SSebastian Reichel return PTR_ERR(di->bat); 10428c0984e5SSebastian Reichel } 10438c0984e5SSebastian Reichel 10448c0984e5SSebastian Reichel dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); 10458c0984e5SSebastian Reichel 10468c0984e5SSebastian Reichel bq27xxx_battery_update(di); 10478c0984e5SSebastian Reichel 10481d72706fSMatt Ranostay mutex_lock(&bq27xxx_list_lock); 10491d72706fSMatt Ranostay list_add(&di->list, &bq27xxx_battery_devices); 10501d72706fSMatt Ranostay mutex_unlock(&bq27xxx_list_lock); 10511d72706fSMatt Ranostay 10528c0984e5SSebastian Reichel return 0; 10538c0984e5SSebastian Reichel } 10548c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_setup); 10558c0984e5SSebastian Reichel 10568c0984e5SSebastian Reichel void bq27xxx_battery_teardown(struct bq27xxx_device_info *di) 10578c0984e5SSebastian Reichel { 10588c0984e5SSebastian Reichel /* 10598c0984e5SSebastian Reichel * power_supply_unregister call bq27xxx_battery_get_property which 10608c0984e5SSebastian Reichel * call bq27xxx_battery_poll. 10618c0984e5SSebastian Reichel * Make sure that bq27xxx_battery_poll will not call 10628c0984e5SSebastian Reichel * schedule_delayed_work again after unregister (which cause OOPS). 10638c0984e5SSebastian Reichel */ 10648c0984e5SSebastian Reichel poll_interval = 0; 10658c0984e5SSebastian Reichel 10668c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->work); 10678c0984e5SSebastian Reichel 10688c0984e5SSebastian Reichel power_supply_unregister(di->bat); 10698c0984e5SSebastian Reichel 10701d72706fSMatt Ranostay mutex_lock(&bq27xxx_list_lock); 10711d72706fSMatt Ranostay list_del(&di->list); 10721d72706fSMatt Ranostay mutex_unlock(&bq27xxx_list_lock); 10731d72706fSMatt Ranostay 10748c0984e5SSebastian Reichel mutex_destroy(&di->lock); 10758c0984e5SSebastian Reichel } 10768c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown); 10778c0984e5SSebastian Reichel 10788c0984e5SSebastian Reichel static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg, 10798c0984e5SSebastian Reichel bool single) 10808c0984e5SSebastian Reichel { 10818c0984e5SSebastian Reichel struct device *dev = di->dev; 10828c0984e5SSebastian Reichel struct bq27xxx_platform_data *pdata = dev->platform_data; 10838c0984e5SSebastian Reichel unsigned int timeout = 3; 10848c0984e5SSebastian Reichel int upper, lower; 10858c0984e5SSebastian Reichel int temp; 10868c0984e5SSebastian Reichel 10878c0984e5SSebastian Reichel if (!single) { 10888c0984e5SSebastian Reichel /* Make sure the value has not changed in between reading the 10898c0984e5SSebastian Reichel * lower and the upper part */ 10908c0984e5SSebastian Reichel upper = pdata->read(dev, reg + 1); 10918c0984e5SSebastian Reichel do { 10928c0984e5SSebastian Reichel temp = upper; 10938c0984e5SSebastian Reichel if (upper < 0) 10948c0984e5SSebastian Reichel return upper; 10958c0984e5SSebastian Reichel 10968c0984e5SSebastian Reichel lower = pdata->read(dev, reg); 10978c0984e5SSebastian Reichel if (lower < 0) 10988c0984e5SSebastian Reichel return lower; 10998c0984e5SSebastian Reichel 11008c0984e5SSebastian Reichel upper = pdata->read(dev, reg + 1); 11018c0984e5SSebastian Reichel } while (temp != upper && --timeout); 11028c0984e5SSebastian Reichel 11038c0984e5SSebastian Reichel if (timeout == 0) 11048c0984e5SSebastian Reichel return -EIO; 11058c0984e5SSebastian Reichel 11068c0984e5SSebastian Reichel return (upper << 8) | lower; 11078c0984e5SSebastian Reichel } 11088c0984e5SSebastian Reichel 11098c0984e5SSebastian Reichel return pdata->read(dev, reg); 11108c0984e5SSebastian Reichel } 11118c0984e5SSebastian Reichel 11128c0984e5SSebastian Reichel static int bq27xxx_battery_platform_probe(struct platform_device *pdev) 11138c0984e5SSebastian Reichel { 11148c0984e5SSebastian Reichel struct bq27xxx_device_info *di; 11158c0984e5SSebastian Reichel struct bq27xxx_platform_data *pdata = pdev->dev.platform_data; 11168c0984e5SSebastian Reichel 11178c0984e5SSebastian Reichel if (!pdata) { 11188c0984e5SSebastian Reichel dev_err(&pdev->dev, "no platform_data supplied\n"); 11198c0984e5SSebastian Reichel return -EINVAL; 11208c0984e5SSebastian Reichel } 11218c0984e5SSebastian Reichel 11228c0984e5SSebastian Reichel if (!pdata->read) { 11238c0984e5SSebastian Reichel dev_err(&pdev->dev, "no hdq read callback supplied\n"); 11248c0984e5SSebastian Reichel return -EINVAL; 11258c0984e5SSebastian Reichel } 11268c0984e5SSebastian Reichel 11278c0984e5SSebastian Reichel if (!pdata->chip) { 11288c0984e5SSebastian Reichel dev_err(&pdev->dev, "no device supplied\n"); 11298c0984e5SSebastian Reichel return -EINVAL; 11308c0984e5SSebastian Reichel } 11318c0984e5SSebastian Reichel 11328c0984e5SSebastian Reichel di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); 11338c0984e5SSebastian Reichel if (!di) 11348c0984e5SSebastian Reichel return -ENOMEM; 11358c0984e5SSebastian Reichel 11368c0984e5SSebastian Reichel platform_set_drvdata(pdev, di); 11378c0984e5SSebastian Reichel 11388c0984e5SSebastian Reichel di->dev = &pdev->dev; 11398c0984e5SSebastian Reichel di->chip = pdata->chip; 11408c0984e5SSebastian Reichel di->name = pdata->name ?: dev_name(&pdev->dev); 11418c0984e5SSebastian Reichel di->bus.read = bq27xxx_battery_platform_read; 11428c0984e5SSebastian Reichel 11438c0984e5SSebastian Reichel return bq27xxx_battery_setup(di); 11448c0984e5SSebastian Reichel } 11458c0984e5SSebastian Reichel 11468c0984e5SSebastian Reichel static int bq27xxx_battery_platform_remove(struct platform_device *pdev) 11478c0984e5SSebastian Reichel { 11488c0984e5SSebastian Reichel struct bq27xxx_device_info *di = platform_get_drvdata(pdev); 11498c0984e5SSebastian Reichel 11508c0984e5SSebastian Reichel bq27xxx_battery_teardown(di); 11518c0984e5SSebastian Reichel 11528c0984e5SSebastian Reichel return 0; 11538c0984e5SSebastian Reichel } 11548c0984e5SSebastian Reichel 11558c0984e5SSebastian Reichel static const struct platform_device_id bq27xxx_battery_platform_id_table[] = { 11568c0984e5SSebastian Reichel { "bq27000-battery", }, 11578c0984e5SSebastian Reichel { /* sentinel */ } 11588c0984e5SSebastian Reichel }; 11598c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table); 11608c0984e5SSebastian Reichel 11618c0984e5SSebastian Reichel #ifdef CONFIG_OF 11628c0984e5SSebastian Reichel static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = { 11638c0984e5SSebastian Reichel { .compatible = "ti,bq27000" }, 11648c0984e5SSebastian Reichel {}, 11658c0984e5SSebastian Reichel }; 11668c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table); 11678c0984e5SSebastian Reichel #endif 11688c0984e5SSebastian Reichel 11698c0984e5SSebastian Reichel static struct platform_driver bq27xxx_battery_platform_driver = { 11708c0984e5SSebastian Reichel .probe = bq27xxx_battery_platform_probe, 11718c0984e5SSebastian Reichel .remove = bq27xxx_battery_platform_remove, 11728c0984e5SSebastian Reichel .driver = { 11738c0984e5SSebastian Reichel .name = "bq27000-battery", 11748c0984e5SSebastian Reichel .of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table), 11758c0984e5SSebastian Reichel }, 11768c0984e5SSebastian Reichel .id_table = bq27xxx_battery_platform_id_table, 11778c0984e5SSebastian Reichel }; 11788c0984e5SSebastian Reichel module_platform_driver(bq27xxx_battery_platform_driver); 11798c0984e5SSebastian Reichel 11808c0984e5SSebastian Reichel MODULE_ALIAS("platform:bq27000-battery"); 11818c0984e5SSebastian Reichel 11828c0984e5SSebastian Reichel MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); 11838c0984e5SSebastian Reichel MODULE_DESCRIPTION("BQ27xxx battery monitor driver"); 11848c0984e5SSebastian Reichel MODULE_LICENSE("GPL"); 1185