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 }, 1676da6e4bdSChris Lapa [BQ2751X] = { 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 }, 18632833635SChris Lapa [BQ27500] = { 18732833635SChris Lapa [BQ27XXX_REG_CTRL] = 0x00, 18832833635SChris Lapa [BQ27XXX_REG_TEMP] = 0x06, 18932833635SChris Lapa [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, 19032833635SChris Lapa [BQ27XXX_REG_VOLT] = 0x08, 19132833635SChris Lapa [BQ27XXX_REG_AI] = 0x14, 19232833635SChris Lapa [BQ27XXX_REG_FLAGS] = 0x0a, 19332833635SChris Lapa [BQ27XXX_REG_TTE] = 0x16, 19432833635SChris Lapa [BQ27XXX_REG_TTF] = 0x18, 19532833635SChris Lapa [BQ27XXX_REG_TTES] = 0x1c, 19632833635SChris Lapa [BQ27XXX_REG_TTECP] = 0x26, 19732833635SChris Lapa [BQ27XXX_REG_NAC] = 0x0c, 19832833635SChris Lapa [BQ27XXX_REG_FCC] = 0x12, 19932833635SChris Lapa [BQ27XXX_REG_CYCT] = 0x2a, 20032833635SChris Lapa [BQ27XXX_REG_AE] = 0x22, 20132833635SChris Lapa [BQ27XXX_REG_SOC] = 0x2c, 20232833635SChris Lapa [BQ27XXX_REG_DCAP] = 0x3c, 20332833635SChris Lapa [BQ27XXX_REG_AP] = 0x24, 20432833635SChris Lapa }, 2058c0984e5SSebastian Reichel [BQ27530] = { 2068c0984e5SSebastian Reichel [BQ27XXX_REG_CTRL] = 0x00, 2078c0984e5SSebastian Reichel [BQ27XXX_REG_TEMP] = 0x06, 2088c0984e5SSebastian Reichel [BQ27XXX_REG_INT_TEMP] = 0x32, 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 [BQ27541] = { 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] = 0x3c, 2418c0984e5SSebastian Reichel [BQ27XXX_REG_AP] = 0x24, 2428c0984e5SSebastian Reichel }, 2438c0984e5SSebastian Reichel [BQ27545] = { 2448c0984e5SSebastian Reichel [BQ27XXX_REG_CTRL] = 0x00, 2458c0984e5SSebastian Reichel [BQ27XXX_REG_TEMP] = 0x06, 2468c0984e5SSebastian Reichel [BQ27XXX_REG_INT_TEMP] = 0x28, 2478c0984e5SSebastian Reichel [BQ27XXX_REG_VOLT] = 0x08, 2488c0984e5SSebastian Reichel [BQ27XXX_REG_AI] = 0x14, 2498c0984e5SSebastian Reichel [BQ27XXX_REG_FLAGS] = 0x0a, 2508c0984e5SSebastian Reichel [BQ27XXX_REG_TTE] = 0x16, 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] = 0x0c, 2558c0984e5SSebastian Reichel [BQ27XXX_REG_FCC] = 0x12, 2568c0984e5SSebastian Reichel [BQ27XXX_REG_CYCT] = 0x2a, 2578c0984e5SSebastian Reichel [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 2588c0984e5SSebastian Reichel [BQ27XXX_REG_SOC] = 0x2c, 2598c0984e5SSebastian Reichel [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, 2608c0984e5SSebastian Reichel [BQ27XXX_REG_AP] = 0x24, 2618c0984e5SSebastian Reichel }, 2628c0984e5SSebastian Reichel [BQ27421] = { 2638c0984e5SSebastian Reichel [BQ27XXX_REG_CTRL] = 0x00, 2648c0984e5SSebastian Reichel [BQ27XXX_REG_TEMP] = 0x02, 2658c0984e5SSebastian Reichel [BQ27XXX_REG_INT_TEMP] = 0x1e, 2668c0984e5SSebastian Reichel [BQ27XXX_REG_VOLT] = 0x04, 2678c0984e5SSebastian Reichel [BQ27XXX_REG_AI] = 0x10, 2688c0984e5SSebastian Reichel [BQ27XXX_REG_FLAGS] = 0x06, 2698c0984e5SSebastian Reichel [BQ27XXX_REG_TTE] = INVALID_REG_ADDR, 2708c0984e5SSebastian Reichel [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 2718c0984e5SSebastian Reichel [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, 2728c0984e5SSebastian Reichel [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 2738c0984e5SSebastian Reichel [BQ27XXX_REG_NAC] = 0x08, 2748c0984e5SSebastian Reichel [BQ27XXX_REG_FCC] = 0x0e, 2758c0984e5SSebastian Reichel [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR, 2768c0984e5SSebastian Reichel [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 2778c0984e5SSebastian Reichel [BQ27XXX_REG_SOC] = 0x1c, 2788c0984e5SSebastian Reichel [BQ27XXX_REG_DCAP] = 0x3c, 2798c0984e5SSebastian Reichel [BQ27XXX_REG_AP] = 0x18, 2808c0984e5SSebastian Reichel }, 2818c0984e5SSebastian Reichel }; 2828c0984e5SSebastian Reichel 2838c0984e5SSebastian Reichel static enum power_supply_property bq27000_battery_props[] = { 2848c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 2858c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 2868c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 2878c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 2888c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 2898c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 2908c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 2918c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 2928c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 2938c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 2948c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 2958c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 2968c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 2978c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 2988c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 2998c0984e5SSebastian Reichel POWER_SUPPLY_PROP_ENERGY_NOW, 3008c0984e5SSebastian Reichel POWER_SUPPLY_PROP_POWER_AVG, 3018c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3028c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3038c0984e5SSebastian Reichel }; 3048c0984e5SSebastian Reichel 3058c0984e5SSebastian Reichel static enum power_supply_property bq27010_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_TIME_TO_EMPTY_AVG, 3158c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 3168c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 3178c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 3188c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 3198c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 3208c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3218c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3228c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3238c0984e5SSebastian Reichel }; 3248c0984e5SSebastian Reichel 325818e3012SChris Lapa static enum power_supply_property bq2750x_battery_props[] = { 3268c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 3278c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 3288c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 3298c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 3308c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 3318c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 3328c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 3338c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 3348c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 3358c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 3368c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 3378c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 3388c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3398c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3408c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3418c0984e5SSebastian Reichel }; 3428c0984e5SSebastian Reichel 3436da6e4bdSChris Lapa static enum power_supply_property bq2751x_battery_props[] = { 3443bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_STATUS, 3453bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_PRESENT, 3463bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_VOLTAGE_NOW, 3473bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CURRENT_NOW, 3483bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CAPACITY, 3493bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CAPACITY_LEVEL, 3503bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_TEMP, 3513bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 3523bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_TECHNOLOGY, 3533bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CHARGE_FULL, 3543bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CHARGE_NOW, 3553bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 3563bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_CYCLE_COUNT, 3573bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_HEALTH, 3583bee9ea1SAndrew F. Davis POWER_SUPPLY_PROP_MANUFACTURER, 3593bee9ea1SAndrew F. Davis }; 3603bee9ea1SAndrew F. Davis 36132833635SChris Lapa static enum power_supply_property bq27500_battery_props[] = { 36232833635SChris Lapa POWER_SUPPLY_PROP_STATUS, 36332833635SChris Lapa POWER_SUPPLY_PROP_PRESENT, 36432833635SChris Lapa POWER_SUPPLY_PROP_VOLTAGE_NOW, 36532833635SChris Lapa POWER_SUPPLY_PROP_CURRENT_NOW, 36632833635SChris Lapa POWER_SUPPLY_PROP_CAPACITY, 36732833635SChris Lapa POWER_SUPPLY_PROP_CAPACITY_LEVEL, 36832833635SChris Lapa POWER_SUPPLY_PROP_TEMP, 36932833635SChris Lapa POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 37032833635SChris Lapa POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 37132833635SChris Lapa POWER_SUPPLY_PROP_TECHNOLOGY, 37232833635SChris Lapa POWER_SUPPLY_PROP_CHARGE_FULL, 37332833635SChris Lapa POWER_SUPPLY_PROP_CHARGE_NOW, 37432833635SChris Lapa POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 37532833635SChris Lapa POWER_SUPPLY_PROP_CYCLE_COUNT, 37632833635SChris Lapa POWER_SUPPLY_PROP_ENERGY_NOW, 37732833635SChris Lapa POWER_SUPPLY_PROP_POWER_AVG, 37832833635SChris Lapa POWER_SUPPLY_PROP_HEALTH, 37932833635SChris Lapa POWER_SUPPLY_PROP_MANUFACTURER, 38032833635SChris Lapa }; 38132833635SChris Lapa 3828c0984e5SSebastian Reichel static enum power_supply_property bq27530_battery_props[] = { 3838c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 3848c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 3858c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 3868c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 3878c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 3888c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 3898c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 3908c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 3918c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 3928c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 3938c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 3948c0984e5SSebastian Reichel POWER_SUPPLY_PROP_POWER_AVG, 3958c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 3968c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 3978c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 3988c0984e5SSebastian Reichel }; 3998c0984e5SSebastian Reichel 4008c0984e5SSebastian Reichel static enum power_supply_property bq27541_battery_props[] = { 4018c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 4028c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 4038c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 4048c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 4058c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 4068c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 4078c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 4088c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 4098c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 4108c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 4118c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 4128c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 4138c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 4148c0984e5SSebastian Reichel POWER_SUPPLY_PROP_POWER_AVG, 4158c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 4168c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 4178c0984e5SSebastian Reichel }; 4188c0984e5SSebastian Reichel 4198c0984e5SSebastian Reichel static enum power_supply_property bq27545_battery_props[] = { 4208c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 4218c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 4228c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 4238c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 4248c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 4258c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 4268c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 4278c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 4288c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 4298c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 4308c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 4318c0984e5SSebastian Reichel POWER_SUPPLY_PROP_HEALTH, 4328c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CYCLE_COUNT, 4338c0984e5SSebastian Reichel POWER_SUPPLY_PROP_POWER_AVG, 4348c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 4358c0984e5SSebastian Reichel }; 4368c0984e5SSebastian Reichel 4378c0984e5SSebastian Reichel static enum power_supply_property bq27421_battery_props[] = { 4388c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 4398c0984e5SSebastian Reichel POWER_SUPPLY_PROP_PRESENT, 4408c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 4418c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW, 4428c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 4438c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY_LEVEL, 4448c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP, 4458c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TECHNOLOGY, 4468c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL, 4478c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW, 4488c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 4498c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER, 4508c0984e5SSebastian Reichel }; 4518c0984e5SSebastian Reichel 4528c0984e5SSebastian Reichel #define BQ27XXX_PROP(_id, _prop) \ 4538c0984e5SSebastian Reichel [_id] = { \ 4548c0984e5SSebastian Reichel .props = _prop, \ 4558c0984e5SSebastian Reichel .size = ARRAY_SIZE(_prop), \ 4568c0984e5SSebastian Reichel } 4578c0984e5SSebastian Reichel 4588c0984e5SSebastian Reichel static struct { 4598c0984e5SSebastian Reichel enum power_supply_property *props; 4608c0984e5SSebastian Reichel size_t size; 4618c0984e5SSebastian Reichel } bq27xxx_battery_props[] = { 4628c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27000, bq27000_battery_props), 4638c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27010, bq27010_battery_props), 464818e3012SChris Lapa BQ27XXX_PROP(BQ2750X, bq2750x_battery_props), 4656da6e4bdSChris Lapa BQ27XXX_PROP(BQ2751X, bq2751x_battery_props), 46632833635SChris Lapa BQ27XXX_PROP(BQ27500, bq27500_battery_props), 4678c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27530, bq27530_battery_props), 4688c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27541, bq27541_battery_props), 4698c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27545, bq27545_battery_props), 4708c0984e5SSebastian Reichel BQ27XXX_PROP(BQ27421, bq27421_battery_props), 4718c0984e5SSebastian Reichel }; 4728c0984e5SSebastian Reichel 4731d72706fSMatt Ranostay static DEFINE_MUTEX(bq27xxx_list_lock); 4741d72706fSMatt Ranostay static LIST_HEAD(bq27xxx_battery_devices); 4751d72706fSMatt Ranostay 4761d72706fSMatt Ranostay static int poll_interval_param_set(const char *val, const struct kernel_param *kp) 4771d72706fSMatt Ranostay { 4781d72706fSMatt Ranostay struct bq27xxx_device_info *di; 479950b6c2dSMatt Ranostay unsigned int prev_val = *(unsigned int *) kp->arg; 4801d72706fSMatt Ranostay int ret; 4811d72706fSMatt Ranostay 4821d72706fSMatt Ranostay ret = param_set_uint(val, kp); 483950b6c2dSMatt Ranostay if (ret < 0 || prev_val == *(unsigned int *) kp->arg) 4841d72706fSMatt Ranostay return ret; 4851d72706fSMatt Ranostay 4861d72706fSMatt Ranostay mutex_lock(&bq27xxx_list_lock); 4871d72706fSMatt Ranostay list_for_each_entry(di, &bq27xxx_battery_devices, list) { 4881d72706fSMatt Ranostay cancel_delayed_work_sync(&di->work); 4891d72706fSMatt Ranostay schedule_delayed_work(&di->work, 0); 4901d72706fSMatt Ranostay } 4911d72706fSMatt Ranostay mutex_unlock(&bq27xxx_list_lock); 4921d72706fSMatt Ranostay 4931d72706fSMatt Ranostay return ret; 4941d72706fSMatt Ranostay } 4951d72706fSMatt Ranostay 4961d72706fSMatt Ranostay static const struct kernel_param_ops param_ops_poll_interval = { 4971d72706fSMatt Ranostay .get = param_get_uint, 4981d72706fSMatt Ranostay .set = poll_interval_param_set, 4991d72706fSMatt Ranostay }; 5001d72706fSMatt Ranostay 5018c0984e5SSebastian Reichel static unsigned int poll_interval = 360; 5021d72706fSMatt Ranostay module_param_cb(poll_interval, ¶m_ops_poll_interval, &poll_interval, 0644); 5038c0984e5SSebastian Reichel MODULE_PARM_DESC(poll_interval, 5048c0984e5SSebastian Reichel "battery poll interval in seconds - 0 disables polling"); 5058c0984e5SSebastian Reichel 5068c0984e5SSebastian Reichel /* 5078c0984e5SSebastian Reichel * Common code for BQ27xxx devices 5088c0984e5SSebastian Reichel */ 5098c0984e5SSebastian Reichel 5108c0984e5SSebastian Reichel static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index, 5118c0984e5SSebastian Reichel bool single) 5128c0984e5SSebastian Reichel { 5138c0984e5SSebastian Reichel /* Reports EINVAL for invalid/missing registers */ 5148c0984e5SSebastian Reichel if (!di || di->regs[reg_index] == INVALID_REG_ADDR) 5158c0984e5SSebastian Reichel return -EINVAL; 5168c0984e5SSebastian Reichel 5178c0984e5SSebastian Reichel return di->bus.read(di, di->regs[reg_index], single); 5188c0984e5SSebastian Reichel } 5198c0984e5SSebastian Reichel 5208c0984e5SSebastian Reichel /* 5218c0984e5SSebastian Reichel * Return the battery State-of-Charge 5228c0984e5SSebastian Reichel * Or < 0 if something fails. 5238c0984e5SSebastian Reichel */ 5248c0984e5SSebastian Reichel static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di) 5258c0984e5SSebastian Reichel { 5268c0984e5SSebastian Reichel int soc; 5278c0984e5SSebastian Reichel 5288c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 5298c0984e5SSebastian Reichel soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true); 5308c0984e5SSebastian Reichel else 5318c0984e5SSebastian Reichel soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false); 5328c0984e5SSebastian Reichel 5338c0984e5SSebastian Reichel if (soc < 0) 5348c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading State-of-Charge\n"); 5358c0984e5SSebastian Reichel 5368c0984e5SSebastian Reichel return soc; 5378c0984e5SSebastian Reichel } 5388c0984e5SSebastian Reichel 5398c0984e5SSebastian Reichel /* 5408c0984e5SSebastian Reichel * Return a battery charge value in µAh 5418c0984e5SSebastian Reichel * Or < 0 if something fails. 5428c0984e5SSebastian Reichel */ 5438c0984e5SSebastian Reichel static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg) 5448c0984e5SSebastian Reichel { 5458c0984e5SSebastian Reichel int charge; 5468c0984e5SSebastian Reichel 5478c0984e5SSebastian Reichel charge = bq27xxx_read(di, reg, false); 5488c0984e5SSebastian Reichel if (charge < 0) { 5498c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading charge register %02x: %d\n", 5508c0984e5SSebastian Reichel reg, charge); 5518c0984e5SSebastian Reichel return charge; 5528c0984e5SSebastian Reichel } 5538c0984e5SSebastian Reichel 5548c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 5558c0984e5SSebastian Reichel charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 5568c0984e5SSebastian Reichel else 5578c0984e5SSebastian Reichel charge *= 1000; 5588c0984e5SSebastian Reichel 5598c0984e5SSebastian Reichel return charge; 5608c0984e5SSebastian Reichel } 5618c0984e5SSebastian Reichel 5628c0984e5SSebastian Reichel /* 5638c0984e5SSebastian Reichel * Return the battery Nominal available capacity in µAh 5648c0984e5SSebastian Reichel * Or < 0 if something fails. 5658c0984e5SSebastian Reichel */ 5668c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di) 5678c0984e5SSebastian Reichel { 5688c0984e5SSebastian Reichel int flags; 5698c0984e5SSebastian Reichel 5708c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 5718c0984e5SSebastian Reichel flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); 5728c0984e5SSebastian Reichel if (flags >= 0 && (flags & BQ27000_FLAG_CI)) 5738c0984e5SSebastian Reichel return -ENODATA; 5748c0984e5SSebastian Reichel } 5758c0984e5SSebastian Reichel 5768c0984e5SSebastian Reichel return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC); 5778c0984e5SSebastian Reichel } 5788c0984e5SSebastian Reichel 5798c0984e5SSebastian Reichel /* 5808c0984e5SSebastian Reichel * Return the battery Full Charge Capacity in µAh 5818c0984e5SSebastian Reichel * Or < 0 if something fails. 5828c0984e5SSebastian Reichel */ 5838c0984e5SSebastian Reichel static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di) 5848c0984e5SSebastian Reichel { 5858c0984e5SSebastian Reichel return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC); 5868c0984e5SSebastian Reichel } 5878c0984e5SSebastian Reichel 5888c0984e5SSebastian Reichel /* 5898c0984e5SSebastian Reichel * Return the Design Capacity in µAh 5908c0984e5SSebastian Reichel * Or < 0 if something fails. 5918c0984e5SSebastian Reichel */ 5928c0984e5SSebastian Reichel static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di) 5938c0984e5SSebastian Reichel { 5948c0984e5SSebastian Reichel int dcap; 5958c0984e5SSebastian Reichel 5968c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 5978c0984e5SSebastian Reichel dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true); 5988c0984e5SSebastian Reichel else 5998c0984e5SSebastian Reichel dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false); 6008c0984e5SSebastian Reichel 6018c0984e5SSebastian Reichel if (dcap < 0) { 6028c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading initial last measured discharge\n"); 6038c0984e5SSebastian Reichel return dcap; 6048c0984e5SSebastian Reichel } 6058c0984e5SSebastian Reichel 6068c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 6078c0984e5SSebastian Reichel dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 6088c0984e5SSebastian Reichel else 6098c0984e5SSebastian Reichel dcap *= 1000; 6108c0984e5SSebastian Reichel 6118c0984e5SSebastian Reichel return dcap; 6128c0984e5SSebastian Reichel } 6138c0984e5SSebastian Reichel 6148c0984e5SSebastian Reichel /* 6158c0984e5SSebastian Reichel * Return the battery Available energy in µWh 6168c0984e5SSebastian Reichel * Or < 0 if something fails. 6178c0984e5SSebastian Reichel */ 6188c0984e5SSebastian Reichel static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di) 6198c0984e5SSebastian Reichel { 6208c0984e5SSebastian Reichel int ae; 6218c0984e5SSebastian Reichel 6228c0984e5SSebastian Reichel ae = bq27xxx_read(di, BQ27XXX_REG_AE, false); 6238c0984e5SSebastian Reichel if (ae < 0) { 6248c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading available energy\n"); 6258c0984e5SSebastian Reichel return ae; 6268c0984e5SSebastian Reichel } 6278c0984e5SSebastian Reichel 6288c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 6298c0984e5SSebastian Reichel ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS; 6308c0984e5SSebastian Reichel else 6318c0984e5SSebastian Reichel ae *= 1000; 6328c0984e5SSebastian Reichel 6338c0984e5SSebastian Reichel return ae; 6348c0984e5SSebastian Reichel } 6358c0984e5SSebastian Reichel 6368c0984e5SSebastian Reichel /* 6378c0984e5SSebastian Reichel * Return the battery temperature in tenths of degree Kelvin 6388c0984e5SSebastian Reichel * Or < 0 if something fails. 6398c0984e5SSebastian Reichel */ 6408c0984e5SSebastian Reichel static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di) 6418c0984e5SSebastian Reichel { 6428c0984e5SSebastian Reichel int temp; 6438c0984e5SSebastian Reichel 6448c0984e5SSebastian Reichel temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false); 6458c0984e5SSebastian Reichel if (temp < 0) { 6468c0984e5SSebastian Reichel dev_err(di->dev, "error reading temperature\n"); 6478c0984e5SSebastian Reichel return temp; 6488c0984e5SSebastian Reichel } 6498c0984e5SSebastian Reichel 6508c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 6518c0984e5SSebastian Reichel temp = 5 * temp / 2; 6528c0984e5SSebastian Reichel 6538c0984e5SSebastian Reichel return temp; 6548c0984e5SSebastian Reichel } 6558c0984e5SSebastian Reichel 6568c0984e5SSebastian Reichel /* 6578c0984e5SSebastian Reichel * Return the battery Cycle count total 6588c0984e5SSebastian Reichel * Or < 0 if something fails. 6598c0984e5SSebastian Reichel */ 6608c0984e5SSebastian Reichel static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di) 6618c0984e5SSebastian Reichel { 6628c0984e5SSebastian Reichel int cyct; 6638c0984e5SSebastian Reichel 6648c0984e5SSebastian Reichel cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false); 6658c0984e5SSebastian Reichel if (cyct < 0) 6668c0984e5SSebastian Reichel dev_err(di->dev, "error reading cycle count total\n"); 6678c0984e5SSebastian Reichel 6688c0984e5SSebastian Reichel return cyct; 6698c0984e5SSebastian Reichel } 6708c0984e5SSebastian Reichel 6718c0984e5SSebastian Reichel /* 6728c0984e5SSebastian Reichel * Read a time register. 6738c0984e5SSebastian Reichel * Return < 0 if something fails. 6748c0984e5SSebastian Reichel */ 6758c0984e5SSebastian Reichel static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg) 6768c0984e5SSebastian Reichel { 6778c0984e5SSebastian Reichel int tval; 6788c0984e5SSebastian Reichel 6798c0984e5SSebastian Reichel tval = bq27xxx_read(di, reg, false); 6808c0984e5SSebastian Reichel if (tval < 0) { 6818c0984e5SSebastian Reichel dev_dbg(di->dev, "error reading time register %02x: %d\n", 6828c0984e5SSebastian Reichel reg, tval); 6838c0984e5SSebastian Reichel return tval; 6848c0984e5SSebastian Reichel } 6858c0984e5SSebastian Reichel 6868c0984e5SSebastian Reichel if (tval == 65535) 6878c0984e5SSebastian Reichel return -ENODATA; 6888c0984e5SSebastian Reichel 6898c0984e5SSebastian Reichel return tval * 60; 6908c0984e5SSebastian Reichel } 6918c0984e5SSebastian Reichel 6928c0984e5SSebastian Reichel /* 6938c0984e5SSebastian Reichel * Read an average power register. 6948c0984e5SSebastian Reichel * Return < 0 if something fails. 6958c0984e5SSebastian Reichel */ 6968c0984e5SSebastian Reichel static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di) 6978c0984e5SSebastian Reichel { 6988c0984e5SSebastian Reichel int tval; 6998c0984e5SSebastian Reichel 7008c0984e5SSebastian Reichel tval = bq27xxx_read(di, BQ27XXX_REG_AP, false); 7018c0984e5SSebastian Reichel if (tval < 0) { 7028c0984e5SSebastian Reichel dev_err(di->dev, "error reading average power register %02x: %d\n", 7038c0984e5SSebastian Reichel BQ27XXX_REG_AP, tval); 7048c0984e5SSebastian Reichel return tval; 7058c0984e5SSebastian Reichel } 7068c0984e5SSebastian Reichel 7078c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 7088c0984e5SSebastian Reichel return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS; 7098c0984e5SSebastian Reichel else 7108c0984e5SSebastian Reichel return tval; 7118c0984e5SSebastian Reichel } 7128c0984e5SSebastian Reichel 7138c0984e5SSebastian Reichel /* 7148c0984e5SSebastian Reichel * Returns true if a battery over temperature condition is detected 7158c0984e5SSebastian Reichel */ 7168c0984e5SSebastian Reichel static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags) 7178c0984e5SSebastian Reichel { 718e839a448SChris Lapa switch (di->chip) { 719818e3012SChris Lapa case BQ2750X: 7206da6e4bdSChris Lapa case BQ2751X: 72132833635SChris Lapa case BQ27500: 722e839a448SChris Lapa case BQ27541: 723e839a448SChris Lapa case BQ27545: 7248c0984e5SSebastian Reichel return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD); 725e839a448SChris Lapa case BQ27530: 726e839a448SChris Lapa case BQ27421: 7278c0984e5SSebastian Reichel return flags & BQ27XXX_FLAG_OT; 728e839a448SChris Lapa default: 7298c0984e5SSebastian Reichel return false; 7308c0984e5SSebastian Reichel } 731e839a448SChris Lapa } 7328c0984e5SSebastian Reichel 7338c0984e5SSebastian Reichel /* 7348c0984e5SSebastian Reichel * Returns true if a battery under temperature condition is detected 7358c0984e5SSebastian Reichel */ 7368c0984e5SSebastian Reichel static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags) 7378c0984e5SSebastian Reichel { 7388c0984e5SSebastian Reichel if (di->chip == BQ27530 || di->chip == BQ27421) 7398c0984e5SSebastian Reichel return flags & BQ27XXX_FLAG_UT; 7408c0984e5SSebastian Reichel 7418c0984e5SSebastian Reichel return false; 7428c0984e5SSebastian Reichel } 7438c0984e5SSebastian Reichel 7448c0984e5SSebastian Reichel /* 7458c0984e5SSebastian Reichel * Returns true if a low state of charge condition is detected 7468c0984e5SSebastian Reichel */ 7478c0984e5SSebastian Reichel static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags) 7488c0984e5SSebastian Reichel { 7498c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) 7508c0984e5SSebastian Reichel return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF); 7518c0984e5SSebastian Reichel else 7528c0984e5SSebastian Reichel return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF); 7538c0984e5SSebastian Reichel } 7548c0984e5SSebastian Reichel 7558c0984e5SSebastian Reichel /* 7568c0984e5SSebastian Reichel * Read flag register. 7578c0984e5SSebastian Reichel * Return < 0 if something fails. 7588c0984e5SSebastian Reichel */ 7598c0984e5SSebastian Reichel static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di) 7608c0984e5SSebastian Reichel { 7618c0984e5SSebastian Reichel int flags; 762e4a404a0SH. Nikolaus Schaller bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; 7638c0984e5SSebastian Reichel 764e4a404a0SH. Nikolaus Schaller flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); 7658c0984e5SSebastian Reichel if (flags < 0) { 7668c0984e5SSebastian Reichel dev_err(di->dev, "error reading flag register:%d\n", flags); 7678c0984e5SSebastian Reichel return flags; 7688c0984e5SSebastian Reichel } 7698c0984e5SSebastian Reichel 7708c0984e5SSebastian Reichel /* Unlikely but important to return first */ 7718c0984e5SSebastian Reichel if (unlikely(bq27xxx_battery_overtemp(di, flags))) 7728c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_OVERHEAT; 7738c0984e5SSebastian Reichel if (unlikely(bq27xxx_battery_undertemp(di, flags))) 7748c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_COLD; 7758c0984e5SSebastian Reichel if (unlikely(bq27xxx_battery_dead(di, flags))) 7768c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_DEAD; 7778c0984e5SSebastian Reichel 7788c0984e5SSebastian Reichel return POWER_SUPPLY_HEALTH_GOOD; 7798c0984e5SSebastian Reichel } 7808c0984e5SSebastian Reichel 7818c0984e5SSebastian Reichel void bq27xxx_battery_update(struct bq27xxx_device_info *di) 7828c0984e5SSebastian Reichel { 7838c0984e5SSebastian Reichel struct bq27xxx_reg_cache cache = {0, }; 7848c0984e5SSebastian Reichel bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010; 7858c0984e5SSebastian Reichel bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; 7868c0984e5SSebastian Reichel 7878c0984e5SSebastian Reichel cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); 7888c0984e5SSebastian Reichel if ((cache.flags & 0xff) == 0xff) 7898c0984e5SSebastian Reichel cache.flags = -1; /* read error */ 7908c0984e5SSebastian Reichel if (cache.flags >= 0) { 7918c0984e5SSebastian Reichel cache.temperature = bq27xxx_battery_read_temperature(di); 7928c0984e5SSebastian Reichel if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) { 7938c0984e5SSebastian Reichel dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n"); 7948c0984e5SSebastian Reichel cache.capacity = -ENODATA; 7958c0984e5SSebastian Reichel cache.energy = -ENODATA; 7968c0984e5SSebastian Reichel cache.time_to_empty = -ENODATA; 7978c0984e5SSebastian Reichel cache.time_to_empty_avg = -ENODATA; 7988c0984e5SSebastian Reichel cache.time_to_full = -ENODATA; 7998c0984e5SSebastian Reichel cache.charge_full = -ENODATA; 8008c0984e5SSebastian Reichel cache.health = -ENODATA; 8018c0984e5SSebastian Reichel } else { 8028c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR) 8038c0984e5SSebastian Reichel cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE); 8048c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR) 8058c0984e5SSebastian Reichel cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP); 8068c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR) 8078c0984e5SSebastian Reichel cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF); 8088c0984e5SSebastian Reichel cache.charge_full = bq27xxx_battery_read_fcc(di); 8098c0984e5SSebastian Reichel cache.capacity = bq27xxx_battery_read_soc(di); 8108c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR) 8118c0984e5SSebastian Reichel cache.energy = bq27xxx_battery_read_energy(di); 8128c0984e5SSebastian Reichel cache.health = bq27xxx_battery_read_health(di); 8138c0984e5SSebastian Reichel } 8148c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR) 8158c0984e5SSebastian Reichel cache.cycle_count = bq27xxx_battery_read_cyct(di); 8168c0984e5SSebastian Reichel if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR) 8178c0984e5SSebastian Reichel cache.power_avg = bq27xxx_battery_read_pwr_avg(di); 8188c0984e5SSebastian Reichel 8198c0984e5SSebastian Reichel /* We only have to read charge design full once */ 8208c0984e5SSebastian Reichel if (di->charge_design_full <= 0) 8218c0984e5SSebastian Reichel di->charge_design_full = bq27xxx_battery_read_dcap(di); 8228c0984e5SSebastian Reichel } 8238c0984e5SSebastian Reichel 8248c0984e5SSebastian Reichel if (di->cache.capacity != cache.capacity) 8258c0984e5SSebastian Reichel power_supply_changed(di->bat); 8268c0984e5SSebastian Reichel 8278c0984e5SSebastian Reichel if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) 8288c0984e5SSebastian Reichel di->cache = cache; 8298c0984e5SSebastian Reichel 8308c0984e5SSebastian Reichel di->last_update = jiffies; 8318c0984e5SSebastian Reichel } 8328c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_update); 8338c0984e5SSebastian Reichel 8348c0984e5SSebastian Reichel static void bq27xxx_battery_poll(struct work_struct *work) 8358c0984e5SSebastian Reichel { 8368c0984e5SSebastian Reichel struct bq27xxx_device_info *di = 8378c0984e5SSebastian Reichel container_of(work, struct bq27xxx_device_info, 8388c0984e5SSebastian Reichel work.work); 8398c0984e5SSebastian Reichel 8408c0984e5SSebastian Reichel bq27xxx_battery_update(di); 8418c0984e5SSebastian Reichel 8428c0984e5SSebastian Reichel if (poll_interval > 0) 8438c0984e5SSebastian Reichel schedule_delayed_work(&di->work, poll_interval * HZ); 8448c0984e5SSebastian Reichel } 8458c0984e5SSebastian Reichel 8468c0984e5SSebastian Reichel /* 8478c0984e5SSebastian Reichel * Return the battery average current in µA 8488c0984e5SSebastian Reichel * Note that current can be negative signed as well 8498c0984e5SSebastian Reichel * Or 0 if something fails. 8508c0984e5SSebastian Reichel */ 8518c0984e5SSebastian Reichel static int bq27xxx_battery_current(struct bq27xxx_device_info *di, 8528c0984e5SSebastian Reichel union power_supply_propval *val) 8538c0984e5SSebastian Reichel { 8548c0984e5SSebastian Reichel int curr; 8558c0984e5SSebastian Reichel int flags; 8568c0984e5SSebastian Reichel 8578c0984e5SSebastian Reichel curr = bq27xxx_read(di, BQ27XXX_REG_AI, false); 8588c0984e5SSebastian Reichel if (curr < 0) { 8598c0984e5SSebastian Reichel dev_err(di->dev, "error reading current\n"); 8608c0984e5SSebastian Reichel return curr; 8618c0984e5SSebastian Reichel } 8628c0984e5SSebastian Reichel 8638c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 864e4a404a0SH. Nikolaus Schaller flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); 8658c0984e5SSebastian Reichel if (flags & BQ27000_FLAG_CHGS) { 8668c0984e5SSebastian Reichel dev_dbg(di->dev, "negative current!\n"); 8678c0984e5SSebastian Reichel curr = -curr; 8688c0984e5SSebastian Reichel } 8698c0984e5SSebastian Reichel 8708c0984e5SSebastian Reichel val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 8718c0984e5SSebastian Reichel } else { 8728c0984e5SSebastian Reichel /* Other gauges return signed value */ 8738c0984e5SSebastian Reichel val->intval = (int)((s16)curr) * 1000; 8748c0984e5SSebastian Reichel } 8758c0984e5SSebastian Reichel 8768c0984e5SSebastian Reichel return 0; 8778c0984e5SSebastian Reichel } 8788c0984e5SSebastian Reichel 8798c0984e5SSebastian Reichel static int bq27xxx_battery_status(struct bq27xxx_device_info *di, 8808c0984e5SSebastian Reichel union power_supply_propval *val) 8818c0984e5SSebastian Reichel { 8828c0984e5SSebastian Reichel int status; 8838c0984e5SSebastian Reichel 8848c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 8858c0984e5SSebastian Reichel if (di->cache.flags & BQ27000_FLAG_FC) 8868c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_FULL; 8878c0984e5SSebastian Reichel else if (di->cache.flags & BQ27000_FLAG_CHGS) 8888c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_CHARGING; 8898c0984e5SSebastian Reichel else if (power_supply_am_i_supplied(di->bat)) 8908c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_NOT_CHARGING; 8918c0984e5SSebastian Reichel else 8928c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_DISCHARGING; 8938c0984e5SSebastian Reichel } else { 8948c0984e5SSebastian Reichel if (di->cache.flags & BQ27XXX_FLAG_FC) 8958c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_FULL; 8968c0984e5SSebastian Reichel else if (di->cache.flags & BQ27XXX_FLAG_DSC) 8978c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_DISCHARGING; 8988c0984e5SSebastian Reichel else 8998c0984e5SSebastian Reichel status = POWER_SUPPLY_STATUS_CHARGING; 9008c0984e5SSebastian Reichel } 9018c0984e5SSebastian Reichel 9028c0984e5SSebastian Reichel val->intval = status; 9038c0984e5SSebastian Reichel 9048c0984e5SSebastian Reichel return 0; 9058c0984e5SSebastian Reichel } 9068c0984e5SSebastian Reichel 9078c0984e5SSebastian Reichel static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di, 9088c0984e5SSebastian Reichel union power_supply_propval *val) 9098c0984e5SSebastian Reichel { 9108c0984e5SSebastian Reichel int level; 9118c0984e5SSebastian Reichel 9128c0984e5SSebastian Reichel if (di->chip == BQ27000 || di->chip == BQ27010) { 9138c0984e5SSebastian Reichel if (di->cache.flags & BQ27000_FLAG_FC) 9148c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 9158c0984e5SSebastian Reichel else if (di->cache.flags & BQ27000_FLAG_EDV1) 9168c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 9178c0984e5SSebastian Reichel else if (di->cache.flags & BQ27000_FLAG_EDVF) 9188c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 9198c0984e5SSebastian Reichel else 9208c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 9218c0984e5SSebastian Reichel } else { 9228c0984e5SSebastian Reichel if (di->cache.flags & BQ27XXX_FLAG_FC) 9238c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 9248c0984e5SSebastian Reichel else if (di->cache.flags & BQ27XXX_FLAG_SOC1) 9258c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 9268c0984e5SSebastian Reichel else if (di->cache.flags & BQ27XXX_FLAG_SOCF) 9278c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 9288c0984e5SSebastian Reichel else 9298c0984e5SSebastian Reichel level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 9308c0984e5SSebastian Reichel } 9318c0984e5SSebastian Reichel 9328c0984e5SSebastian Reichel val->intval = level; 9338c0984e5SSebastian Reichel 9348c0984e5SSebastian Reichel return 0; 9358c0984e5SSebastian Reichel } 9368c0984e5SSebastian Reichel 9378c0984e5SSebastian Reichel /* 9388c0984e5SSebastian Reichel * Return the battery Voltage in millivolts 9398c0984e5SSebastian Reichel * Or < 0 if something fails. 9408c0984e5SSebastian Reichel */ 9418c0984e5SSebastian Reichel static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di, 9428c0984e5SSebastian Reichel union power_supply_propval *val) 9438c0984e5SSebastian Reichel { 9448c0984e5SSebastian Reichel int volt; 9458c0984e5SSebastian Reichel 9468c0984e5SSebastian Reichel volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false); 9478c0984e5SSebastian Reichel if (volt < 0) { 9488c0984e5SSebastian Reichel dev_err(di->dev, "error reading voltage\n"); 9498c0984e5SSebastian Reichel return volt; 9508c0984e5SSebastian Reichel } 9518c0984e5SSebastian Reichel 9528c0984e5SSebastian Reichel val->intval = volt * 1000; 9538c0984e5SSebastian Reichel 9548c0984e5SSebastian Reichel return 0; 9558c0984e5SSebastian Reichel } 9568c0984e5SSebastian Reichel 9578c0984e5SSebastian Reichel static int bq27xxx_simple_value(int value, 9588c0984e5SSebastian Reichel union power_supply_propval *val) 9598c0984e5SSebastian Reichel { 9608c0984e5SSebastian Reichel if (value < 0) 9618c0984e5SSebastian Reichel return value; 9628c0984e5SSebastian Reichel 9638c0984e5SSebastian Reichel val->intval = value; 9648c0984e5SSebastian Reichel 9658c0984e5SSebastian Reichel return 0; 9668c0984e5SSebastian Reichel } 9678c0984e5SSebastian Reichel 9688c0984e5SSebastian Reichel static int bq27xxx_battery_get_property(struct power_supply *psy, 9698c0984e5SSebastian Reichel enum power_supply_property psp, 9708c0984e5SSebastian Reichel union power_supply_propval *val) 9718c0984e5SSebastian Reichel { 9728c0984e5SSebastian Reichel int ret = 0; 9738c0984e5SSebastian Reichel struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); 9748c0984e5SSebastian Reichel 9758c0984e5SSebastian Reichel mutex_lock(&di->lock); 9768c0984e5SSebastian Reichel if (time_is_before_jiffies(di->last_update + 5 * HZ)) { 9778c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->work); 9788c0984e5SSebastian Reichel bq27xxx_battery_poll(&di->work.work); 9798c0984e5SSebastian Reichel } 9808c0984e5SSebastian Reichel mutex_unlock(&di->lock); 9818c0984e5SSebastian Reichel 9828c0984e5SSebastian Reichel if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) 9838c0984e5SSebastian Reichel return -ENODEV; 9848c0984e5SSebastian Reichel 9858c0984e5SSebastian Reichel switch (psp) { 9868c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_STATUS: 9878c0984e5SSebastian Reichel ret = bq27xxx_battery_status(di, val); 9888c0984e5SSebastian Reichel break; 9898c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_VOLTAGE_NOW: 9908c0984e5SSebastian Reichel ret = bq27xxx_battery_voltage(di, val); 9918c0984e5SSebastian Reichel break; 9928c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_PRESENT: 9938c0984e5SSebastian Reichel val->intval = di->cache.flags < 0 ? 0 : 1; 9948c0984e5SSebastian Reichel break; 9958c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CURRENT_NOW: 9968c0984e5SSebastian Reichel ret = bq27xxx_battery_current(di, val); 9978c0984e5SSebastian Reichel break; 9988c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CAPACITY: 9998c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.capacity, val); 10008c0984e5SSebastian Reichel break; 10018c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 10028c0984e5SSebastian Reichel ret = bq27xxx_battery_capacity_level(di, val); 10038c0984e5SSebastian Reichel break; 10048c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TEMP: 10058c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.temperature, val); 10068c0984e5SSebastian Reichel if (ret == 0) 10078c0984e5SSebastian Reichel val->intval -= 2731; /* convert decidegree k to c */ 10088c0984e5SSebastian Reichel break; 10098c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 10108c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.time_to_empty, val); 10118c0984e5SSebastian Reichel break; 10128c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 10138c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val); 10148c0984e5SSebastian Reichel break; 10158c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: 10168c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.time_to_full, val); 10178c0984e5SSebastian Reichel break; 10188c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TECHNOLOGY: 10198c0984e5SSebastian Reichel val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 10208c0984e5SSebastian Reichel break; 10218c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW: 10228c0984e5SSebastian Reichel ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val); 10238c0984e5SSebastian Reichel break; 10248c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL: 10258c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.charge_full, val); 10268c0984e5SSebastian Reichel break; 10278c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 10288c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->charge_design_full, val); 10298c0984e5SSebastian Reichel break; 10308c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CYCLE_COUNT: 10318c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.cycle_count, val); 10328c0984e5SSebastian Reichel break; 10338c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_ENERGY_NOW: 10348c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.energy, val); 10358c0984e5SSebastian Reichel break; 10368c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_POWER_AVG: 10378c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.power_avg, val); 10388c0984e5SSebastian Reichel break; 10398c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_HEALTH: 10408c0984e5SSebastian Reichel ret = bq27xxx_simple_value(di->cache.health, val); 10418c0984e5SSebastian Reichel break; 10428c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_MANUFACTURER: 10438c0984e5SSebastian Reichel val->strval = BQ27XXX_MANUFACTURER; 10448c0984e5SSebastian Reichel break; 10458c0984e5SSebastian Reichel default: 10468c0984e5SSebastian Reichel return -EINVAL; 10478c0984e5SSebastian Reichel } 10488c0984e5SSebastian Reichel 10498c0984e5SSebastian Reichel return ret; 10508c0984e5SSebastian Reichel } 10518c0984e5SSebastian Reichel 10528c0984e5SSebastian Reichel static void bq27xxx_external_power_changed(struct power_supply *psy) 10538c0984e5SSebastian Reichel { 10548c0984e5SSebastian Reichel struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); 10558c0984e5SSebastian Reichel 10568c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->work); 10578c0984e5SSebastian Reichel schedule_delayed_work(&di->work, 0); 10588c0984e5SSebastian Reichel } 10598c0984e5SSebastian Reichel 10608c0984e5SSebastian Reichel int bq27xxx_battery_setup(struct bq27xxx_device_info *di) 10618c0984e5SSebastian Reichel { 10628c0984e5SSebastian Reichel struct power_supply_desc *psy_desc; 10638c0984e5SSebastian Reichel struct power_supply_config psy_cfg = { .drv_data = di, }; 10648c0984e5SSebastian Reichel 10658c0984e5SSebastian Reichel INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll); 10668c0984e5SSebastian Reichel mutex_init(&di->lock); 10678c0984e5SSebastian Reichel di->regs = bq27xxx_regs[di->chip]; 10688c0984e5SSebastian Reichel 10698c0984e5SSebastian Reichel psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL); 10708c0984e5SSebastian Reichel if (!psy_desc) 10718c0984e5SSebastian Reichel return -ENOMEM; 10728c0984e5SSebastian Reichel 10738c0984e5SSebastian Reichel psy_desc->name = di->name; 10748c0984e5SSebastian Reichel psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; 10758c0984e5SSebastian Reichel psy_desc->properties = bq27xxx_battery_props[di->chip].props; 10768c0984e5SSebastian Reichel psy_desc->num_properties = bq27xxx_battery_props[di->chip].size; 10778c0984e5SSebastian Reichel psy_desc->get_property = bq27xxx_battery_get_property; 10788c0984e5SSebastian Reichel psy_desc->external_power_changed = bq27xxx_external_power_changed; 10798c0984e5SSebastian Reichel 10808c0984e5SSebastian Reichel di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg); 10818c0984e5SSebastian Reichel if (IS_ERR(di->bat)) { 10828c0984e5SSebastian Reichel dev_err(di->dev, "failed to register battery\n"); 10838c0984e5SSebastian Reichel return PTR_ERR(di->bat); 10848c0984e5SSebastian Reichel } 10858c0984e5SSebastian Reichel 10868c0984e5SSebastian Reichel dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); 10878c0984e5SSebastian Reichel 10888c0984e5SSebastian Reichel bq27xxx_battery_update(di); 10898c0984e5SSebastian Reichel 10901d72706fSMatt Ranostay mutex_lock(&bq27xxx_list_lock); 10911d72706fSMatt Ranostay list_add(&di->list, &bq27xxx_battery_devices); 10921d72706fSMatt Ranostay mutex_unlock(&bq27xxx_list_lock); 10931d72706fSMatt Ranostay 10948c0984e5SSebastian Reichel return 0; 10958c0984e5SSebastian Reichel } 10968c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_setup); 10978c0984e5SSebastian Reichel 10988c0984e5SSebastian Reichel void bq27xxx_battery_teardown(struct bq27xxx_device_info *di) 10998c0984e5SSebastian Reichel { 11008c0984e5SSebastian Reichel /* 11018c0984e5SSebastian Reichel * power_supply_unregister call bq27xxx_battery_get_property which 11028c0984e5SSebastian Reichel * call bq27xxx_battery_poll. 11038c0984e5SSebastian Reichel * Make sure that bq27xxx_battery_poll will not call 11048c0984e5SSebastian Reichel * schedule_delayed_work again after unregister (which cause OOPS). 11058c0984e5SSebastian Reichel */ 11068c0984e5SSebastian Reichel poll_interval = 0; 11078c0984e5SSebastian Reichel 11088c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->work); 11098c0984e5SSebastian Reichel 11108c0984e5SSebastian Reichel power_supply_unregister(di->bat); 11118c0984e5SSebastian Reichel 11121d72706fSMatt Ranostay mutex_lock(&bq27xxx_list_lock); 11131d72706fSMatt Ranostay list_del(&di->list); 11141d72706fSMatt Ranostay mutex_unlock(&bq27xxx_list_lock); 11151d72706fSMatt Ranostay 11168c0984e5SSebastian Reichel mutex_destroy(&di->lock); 11178c0984e5SSebastian Reichel } 11188c0984e5SSebastian Reichel EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown); 11198c0984e5SSebastian Reichel 11208c0984e5SSebastian Reichel static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg, 11218c0984e5SSebastian Reichel bool single) 11228c0984e5SSebastian Reichel { 11238c0984e5SSebastian Reichel struct device *dev = di->dev; 11248c0984e5SSebastian Reichel struct bq27xxx_platform_data *pdata = dev->platform_data; 11258c0984e5SSebastian Reichel unsigned int timeout = 3; 11268c0984e5SSebastian Reichel int upper, lower; 11278c0984e5SSebastian Reichel int temp; 11288c0984e5SSebastian Reichel 11298c0984e5SSebastian Reichel if (!single) { 11308c0984e5SSebastian Reichel /* Make sure the value has not changed in between reading the 11318c0984e5SSebastian Reichel * lower and the upper part */ 11328c0984e5SSebastian Reichel upper = pdata->read(dev, reg + 1); 11338c0984e5SSebastian Reichel do { 11348c0984e5SSebastian Reichel temp = upper; 11358c0984e5SSebastian Reichel if (upper < 0) 11368c0984e5SSebastian Reichel return upper; 11378c0984e5SSebastian Reichel 11388c0984e5SSebastian Reichel lower = pdata->read(dev, reg); 11398c0984e5SSebastian Reichel if (lower < 0) 11408c0984e5SSebastian Reichel return lower; 11418c0984e5SSebastian Reichel 11428c0984e5SSebastian Reichel upper = pdata->read(dev, reg + 1); 11438c0984e5SSebastian Reichel } while (temp != upper && --timeout); 11448c0984e5SSebastian Reichel 11458c0984e5SSebastian Reichel if (timeout == 0) 11468c0984e5SSebastian Reichel return -EIO; 11478c0984e5SSebastian Reichel 11488c0984e5SSebastian Reichel return (upper << 8) | lower; 11498c0984e5SSebastian Reichel } 11508c0984e5SSebastian Reichel 11518c0984e5SSebastian Reichel return pdata->read(dev, reg); 11528c0984e5SSebastian Reichel } 11538c0984e5SSebastian Reichel 11548c0984e5SSebastian Reichel static int bq27xxx_battery_platform_probe(struct platform_device *pdev) 11558c0984e5SSebastian Reichel { 11568c0984e5SSebastian Reichel struct bq27xxx_device_info *di; 11578c0984e5SSebastian Reichel struct bq27xxx_platform_data *pdata = pdev->dev.platform_data; 11588c0984e5SSebastian Reichel 11598c0984e5SSebastian Reichel if (!pdata) { 11608c0984e5SSebastian Reichel dev_err(&pdev->dev, "no platform_data supplied\n"); 11618c0984e5SSebastian Reichel return -EINVAL; 11628c0984e5SSebastian Reichel } 11638c0984e5SSebastian Reichel 11648c0984e5SSebastian Reichel if (!pdata->read) { 11658c0984e5SSebastian Reichel dev_err(&pdev->dev, "no hdq read callback supplied\n"); 11668c0984e5SSebastian Reichel return -EINVAL; 11678c0984e5SSebastian Reichel } 11688c0984e5SSebastian Reichel 11698c0984e5SSebastian Reichel if (!pdata->chip) { 11708c0984e5SSebastian Reichel dev_err(&pdev->dev, "no device supplied\n"); 11718c0984e5SSebastian Reichel return -EINVAL; 11728c0984e5SSebastian Reichel } 11738c0984e5SSebastian Reichel 11748c0984e5SSebastian Reichel di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); 11758c0984e5SSebastian Reichel if (!di) 11768c0984e5SSebastian Reichel return -ENOMEM; 11778c0984e5SSebastian Reichel 11788c0984e5SSebastian Reichel platform_set_drvdata(pdev, di); 11798c0984e5SSebastian Reichel 11808c0984e5SSebastian Reichel di->dev = &pdev->dev; 11818c0984e5SSebastian Reichel di->chip = pdata->chip; 11828c0984e5SSebastian Reichel di->name = pdata->name ?: dev_name(&pdev->dev); 11838c0984e5SSebastian Reichel di->bus.read = bq27xxx_battery_platform_read; 11848c0984e5SSebastian Reichel 11858c0984e5SSebastian Reichel return bq27xxx_battery_setup(di); 11868c0984e5SSebastian Reichel } 11878c0984e5SSebastian Reichel 11888c0984e5SSebastian Reichel static int bq27xxx_battery_platform_remove(struct platform_device *pdev) 11898c0984e5SSebastian Reichel { 11908c0984e5SSebastian Reichel struct bq27xxx_device_info *di = platform_get_drvdata(pdev); 11918c0984e5SSebastian Reichel 11928c0984e5SSebastian Reichel bq27xxx_battery_teardown(di); 11938c0984e5SSebastian Reichel 11948c0984e5SSebastian Reichel return 0; 11958c0984e5SSebastian Reichel } 11968c0984e5SSebastian Reichel 11978c0984e5SSebastian Reichel static const struct platform_device_id bq27xxx_battery_platform_id_table[] = { 11988c0984e5SSebastian Reichel { "bq27000-battery", }, 11998c0984e5SSebastian Reichel { /* sentinel */ } 12008c0984e5SSebastian Reichel }; 12018c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table); 12028c0984e5SSebastian Reichel 12038c0984e5SSebastian Reichel #ifdef CONFIG_OF 12048c0984e5SSebastian Reichel static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = { 12058c0984e5SSebastian Reichel { .compatible = "ti,bq27000" }, 12068c0984e5SSebastian Reichel {}, 12078c0984e5SSebastian Reichel }; 12088c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table); 12098c0984e5SSebastian Reichel #endif 12108c0984e5SSebastian Reichel 12118c0984e5SSebastian Reichel static struct platform_driver bq27xxx_battery_platform_driver = { 12128c0984e5SSebastian Reichel .probe = bq27xxx_battery_platform_probe, 12138c0984e5SSebastian Reichel .remove = bq27xxx_battery_platform_remove, 12148c0984e5SSebastian Reichel .driver = { 12158c0984e5SSebastian Reichel .name = "bq27000-battery", 12168c0984e5SSebastian Reichel .of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table), 12178c0984e5SSebastian Reichel }, 12188c0984e5SSebastian Reichel .id_table = bq27xxx_battery_platform_id_table, 12198c0984e5SSebastian Reichel }; 12208c0984e5SSebastian Reichel module_platform_driver(bq27xxx_battery_platform_driver); 12218c0984e5SSebastian Reichel 12228c0984e5SSebastian Reichel MODULE_ALIAS("platform:bq27000-battery"); 12238c0984e5SSebastian Reichel 12248c0984e5SSebastian Reichel MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); 12258c0984e5SSebastian Reichel MODULE_DESCRIPTION("BQ27xxx battery monitor driver"); 12268c0984e5SSebastian Reichel MODULE_LICENSE("GPL"); 1227