Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1 | /* |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 2 | * BQ27xxx battery driver |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> |
| 5 | * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it> |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 6 | * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de> |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 7 | * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 8 | * |
| 9 | * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. |
| 10 | * |
| 11 | * This package is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | * |
| 15 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 17 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 18 | * |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 19 | * Datasheets: |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 20 | * http://www.ti.com/product/bq27000 |
| 21 | * http://www.ti.com/product/bq27200 |
| 22 | * http://www.ti.com/product/bq27010 |
| 23 | * http://www.ti.com/product/bq27210 |
| 24 | * http://www.ti.com/product/bq27500 |
Chris Lapa | bd28177 | 2017-01-11 12:44:41 +1100 | [diff] [blame] | 25 | * http://www.ti.com/product/bq27510-g1 |
Chris Lapa | 698a2bf | 2017-01-11 12:44:42 +1100 | [diff] [blame] | 26 | * http://www.ti.com/product/bq27510-g2 |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 27 | * http://www.ti.com/product/bq27510-g3 |
| 28 | * http://www.ti.com/product/bq27520-g4 |
Chris Lapa | 68f2a81 | 2017-01-11 12:44:44 +1100 | [diff] [blame] | 29 | * http://www.ti.com/product/bq27520-g1 |
Chris Lapa | a5deb9a | 2017-01-11 12:44:45 +1100 | [diff] [blame] | 30 | * http://www.ti.com/product/bq27520-g2 |
Chris Lapa | 825e915 | 2017-01-11 12:44:46 +1100 | [diff] [blame] | 31 | * http://www.ti.com/product/bq27520-g3 |
Chris Lapa | 8835cae | 2017-01-11 12:44:47 +1100 | [diff] [blame] | 32 | * http://www.ti.com/product/bq27520-g4 |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 33 | * http://www.ti.com/product/bq27530-g1 |
| 34 | * http://www.ti.com/product/bq27531-g1 |
| 35 | * http://www.ti.com/product/bq27541-g1 |
| 36 | * http://www.ti.com/product/bq27542-g1 |
| 37 | * http://www.ti.com/product/bq27546-g1 |
| 38 | * http://www.ti.com/product/bq27742-g1 |
| 39 | * http://www.ti.com/product/bq27545-g1 |
| 40 | * http://www.ti.com/product/bq27421-g1 |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 41 | * http://www.ti.com/product/bq27425-g1 |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 42 | * http://www.ti.com/product/bq27411-g1 |
| 43 | * http://www.ti.com/product/bq27621-g1 |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 44 | */ |
| 45 | |
Himangi Saraogi | 1cb82fd | 2014-05-28 22:58:49 +0530 | [diff] [blame] | 46 | #include <linux/device.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 47 | #include <linux/module.h> |
Matt Ranostay | 1d72706 | 2016-09-19 20:43:02 -0700 | [diff] [blame] | 48 | #include <linux/mutex.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 49 | #include <linux/param.h> |
| 50 | #include <linux/jiffies.h> |
| 51 | #include <linux/workqueue.h> |
| 52 | #include <linux/delay.h> |
| 53 | #include <linux/platform_device.h> |
| 54 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 55 | #include <linux/slab.h> |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 56 | #include <linux/of.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 57 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 58 | #include <linux/power/bq27xxx_battery.h> |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 59 | |
Andrew F. Davis | 6eb207f | 2015-07-22 16:51:54 -0500 | [diff] [blame] | 60 | #define DRIVER_VERSION "1.2.0" |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 61 | |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 62 | #define BQ27XXX_MANUFACTURER "Texas Instruments" |
| 63 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 64 | /* BQ27XXX Flags */ |
| 65 | #define BQ27XXX_FLAG_DSC BIT(0) |
| 66 | #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */ |
| 67 | #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */ |
| 68 | #define BQ27XXX_FLAG_FC BIT(9) |
| 69 | #define BQ27XXX_FLAG_OTD BIT(14) |
| 70 | #define BQ27XXX_FLAG_OTC BIT(15) |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 71 | #define BQ27XXX_FLAG_UT BIT(14) |
| 72 | #define BQ27XXX_FLAG_OT BIT(15) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 73 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 74 | /* BQ27000 has different layout for Flags register */ |
Andrew F. Davis | 6eb207f | 2015-07-22 16:51:54 -0500 | [diff] [blame] | 75 | #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */ |
| 76 | #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */ |
| 77 | #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */ |
| 78 | #define BQ27000_FLAG_FC BIT(5) |
| 79 | #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */ |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 80 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 81 | #define BQ27XXX_RS (20) /* Resistor sense mOhm */ |
| 82 | #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */ |
| 83 | #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */ |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 84 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 85 | #define INVALID_REG_ADDR 0xff |
| 86 | |
| 87 | /* |
| 88 | * bq27xxx_reg_index - Register names |
| 89 | * |
| 90 | * These are indexes into a device's register mapping array. |
| 91 | */ |
Andrew F. Davis | 2e05b51 | 2016-06-17 17:02:35 -0500 | [diff] [blame] | 92 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 93 | enum bq27xxx_reg_index { |
| 94 | BQ27XXX_REG_CTRL = 0, /* Control */ |
| 95 | BQ27XXX_REG_TEMP, /* Temperature */ |
| 96 | BQ27XXX_REG_INT_TEMP, /* Internal Temperature */ |
| 97 | BQ27XXX_REG_VOLT, /* Voltage */ |
| 98 | BQ27XXX_REG_AI, /* Average Current */ |
| 99 | BQ27XXX_REG_FLAGS, /* Flags */ |
| 100 | BQ27XXX_REG_TTE, /* Time-to-Empty */ |
| 101 | BQ27XXX_REG_TTF, /* Time-to-Full */ |
| 102 | BQ27XXX_REG_TTES, /* Time-to-Empty Standby */ |
| 103 | BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */ |
| 104 | BQ27XXX_REG_NAC, /* Nominal Available Capacity */ |
| 105 | BQ27XXX_REG_FCC, /* Full Charge Capacity */ |
| 106 | BQ27XXX_REG_CYCT, /* Cycle Count */ |
| 107 | BQ27XXX_REG_AE, /* Available Energy */ |
| 108 | BQ27XXX_REG_SOC, /* State-of-Charge */ |
| 109 | BQ27XXX_REG_DCAP, /* Design Capacity */ |
| 110 | BQ27XXX_REG_AP, /* Average Power */ |
Andrew F. Davis | 2e05b51 | 2016-06-17 17:02:35 -0500 | [diff] [blame] | 111 | BQ27XXX_REG_MAX, /* sentinel */ |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 112 | }; |
| 113 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 114 | /* Register mappings */ |
Andrew F. Davis | 2e05b51 | 2016-06-17 17:02:35 -0500 | [diff] [blame] | 115 | static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = { |
| 116 | [BQ27000] = { |
| 117 | [BQ27XXX_REG_CTRL] = 0x00, |
| 118 | [BQ27XXX_REG_TEMP] = 0x06, |
| 119 | [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, |
| 120 | [BQ27XXX_REG_VOLT] = 0x08, |
| 121 | [BQ27XXX_REG_AI] = 0x14, |
| 122 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 123 | [BQ27XXX_REG_TTE] = 0x16, |
| 124 | [BQ27XXX_REG_TTF] = 0x18, |
| 125 | [BQ27XXX_REG_TTES] = 0x1c, |
| 126 | [BQ27XXX_REG_TTECP] = 0x26, |
| 127 | [BQ27XXX_REG_NAC] = 0x0c, |
| 128 | [BQ27XXX_REG_FCC] = 0x12, |
| 129 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 130 | [BQ27XXX_REG_AE] = 0x22, |
| 131 | [BQ27XXX_REG_SOC] = 0x0b, |
| 132 | [BQ27XXX_REG_DCAP] = 0x76, |
| 133 | [BQ27XXX_REG_AP] = 0x24, |
| 134 | }, |
| 135 | [BQ27010] = { |
| 136 | [BQ27XXX_REG_CTRL] = 0x00, |
| 137 | [BQ27XXX_REG_TEMP] = 0x06, |
| 138 | [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, |
| 139 | [BQ27XXX_REG_VOLT] = 0x08, |
| 140 | [BQ27XXX_REG_AI] = 0x14, |
| 141 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 142 | [BQ27XXX_REG_TTE] = 0x16, |
| 143 | [BQ27XXX_REG_TTF] = 0x18, |
| 144 | [BQ27XXX_REG_TTES] = 0x1c, |
| 145 | [BQ27XXX_REG_TTECP] = 0x26, |
| 146 | [BQ27XXX_REG_NAC] = 0x0c, |
| 147 | [BQ27XXX_REG_FCC] = 0x12, |
| 148 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 149 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 150 | [BQ27XXX_REG_SOC] = 0x0b, |
| 151 | [BQ27XXX_REG_DCAP] = 0x76, |
| 152 | [BQ27XXX_REG_AP] = INVALID_REG_ADDR, |
| 153 | }, |
Chris Lapa | 818e301 | 2017-01-11 12:44:38 +1100 | [diff] [blame] | 154 | [BQ2750X] = { |
Andrew F. Davis | 2e05b51 | 2016-06-17 17:02:35 -0500 | [diff] [blame] | 155 | [BQ27XXX_REG_CTRL] = 0x00, |
| 156 | [BQ27XXX_REG_TEMP] = 0x06, |
| 157 | [BQ27XXX_REG_INT_TEMP] = 0x28, |
| 158 | [BQ27XXX_REG_VOLT] = 0x08, |
| 159 | [BQ27XXX_REG_AI] = 0x14, |
| 160 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 161 | [BQ27XXX_REG_TTE] = 0x16, |
| 162 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 163 | [BQ27XXX_REG_TTES] = 0x1a, |
| 164 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 165 | [BQ27XXX_REG_NAC] = 0x0c, |
| 166 | [BQ27XXX_REG_FCC] = 0x12, |
| 167 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 168 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 169 | [BQ27XXX_REG_SOC] = 0x2c, |
| 170 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 171 | [BQ27XXX_REG_AP] = INVALID_REG_ADDR, |
| 172 | }, |
Chris Lapa | 6da6e4b | 2017-01-11 12:44:39 +1100 | [diff] [blame] | 173 | [BQ2751X] = { |
Andrew F. Davis | 3bee9ea | 2016-11-04 13:33:13 -0500 | [diff] [blame] | 174 | [BQ27XXX_REG_CTRL] = 0x00, |
| 175 | [BQ27XXX_REG_TEMP] = 0x06, |
| 176 | [BQ27XXX_REG_INT_TEMP] = 0x28, |
| 177 | [BQ27XXX_REG_VOLT] = 0x08, |
| 178 | [BQ27XXX_REG_AI] = 0x14, |
| 179 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 180 | [BQ27XXX_REG_TTE] = 0x16, |
| 181 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 182 | [BQ27XXX_REG_TTES] = 0x1a, |
| 183 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 184 | [BQ27XXX_REG_NAC] = 0x0c, |
| 185 | [BQ27XXX_REG_FCC] = 0x12, |
| 186 | [BQ27XXX_REG_CYCT] = 0x1e, |
| 187 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 188 | [BQ27XXX_REG_SOC] = 0x20, |
| 189 | [BQ27XXX_REG_DCAP] = 0x2e, |
| 190 | [BQ27XXX_REG_AP] = INVALID_REG_ADDR, |
| 191 | }, |
Chris Lapa | 3283363 | 2017-01-11 12:44:40 +1100 | [diff] [blame] | 192 | [BQ27500] = { |
| 193 | [BQ27XXX_REG_CTRL] = 0x00, |
| 194 | [BQ27XXX_REG_TEMP] = 0x06, |
| 195 | [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, |
| 196 | [BQ27XXX_REG_VOLT] = 0x08, |
| 197 | [BQ27XXX_REG_AI] = 0x14, |
| 198 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 199 | [BQ27XXX_REG_TTE] = 0x16, |
| 200 | [BQ27XXX_REG_TTF] = 0x18, |
| 201 | [BQ27XXX_REG_TTES] = 0x1c, |
| 202 | [BQ27XXX_REG_TTECP] = 0x26, |
| 203 | [BQ27XXX_REG_NAC] = 0x0c, |
| 204 | [BQ27XXX_REG_FCC] = 0x12, |
| 205 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 206 | [BQ27XXX_REG_AE] = 0x22, |
| 207 | [BQ27XXX_REG_SOC] = 0x2c, |
| 208 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 209 | [BQ27XXX_REG_AP] = 0x24, |
| 210 | }, |
Chris Lapa | bd28177 | 2017-01-11 12:44:41 +1100 | [diff] [blame] | 211 | [BQ27510G1] = { |
| 212 | [BQ27XXX_REG_CTRL] = 0x00, |
| 213 | [BQ27XXX_REG_TEMP] = 0x06, |
| 214 | [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, |
| 215 | [BQ27XXX_REG_VOLT] = 0x08, |
| 216 | [BQ27XXX_REG_AI] = 0x14, |
| 217 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 218 | [BQ27XXX_REG_TTE] = 0x16, |
| 219 | [BQ27XXX_REG_TTF] = 0x18, |
| 220 | [BQ27XXX_REG_TTES] = 0x1c, |
| 221 | [BQ27XXX_REG_TTECP] = 0x26, |
| 222 | [BQ27XXX_REG_NAC] = 0x0c, |
| 223 | [BQ27XXX_REG_FCC] = 0x12, |
| 224 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 225 | [BQ27XXX_REG_AE] = 0x22, |
| 226 | [BQ27XXX_REG_SOC] = 0x2c, |
| 227 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 228 | [BQ27XXX_REG_AP] = 0x24, |
| 229 | }, |
Chris Lapa | 698a2bf | 2017-01-11 12:44:42 +1100 | [diff] [blame] | 230 | [BQ27510G2] = { |
| 231 | [BQ27XXX_REG_CTRL] = 0x00, |
| 232 | [BQ27XXX_REG_TEMP] = 0x06, |
| 233 | [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, |
| 234 | [BQ27XXX_REG_VOLT] = 0x08, |
| 235 | [BQ27XXX_REG_AI] = 0x14, |
| 236 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 237 | [BQ27XXX_REG_TTE] = 0x16, |
| 238 | [BQ27XXX_REG_TTF] = 0x18, |
| 239 | [BQ27XXX_REG_TTES] = 0x1c, |
| 240 | [BQ27XXX_REG_TTECP] = 0x26, |
| 241 | [BQ27XXX_REG_NAC] = 0x0c, |
| 242 | [BQ27XXX_REG_FCC] = 0x12, |
| 243 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 244 | [BQ27XXX_REG_AE] = 0x22, |
| 245 | [BQ27XXX_REG_SOC] = 0x2c, |
| 246 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 247 | [BQ27XXX_REG_AP] = 0x24, |
| 248 | }, |
Chris Lapa | 71375aa | 2017-01-11 12:44:43 +1100 | [diff] [blame] | 249 | [BQ27510G3] = { |
| 250 | [BQ27XXX_REG_CTRL] = 0x00, |
| 251 | [BQ27XXX_REG_TEMP] = 0x06, |
| 252 | [BQ27XXX_REG_INT_TEMP] = 0x28, |
| 253 | [BQ27XXX_REG_VOLT] = 0x08, |
| 254 | [BQ27XXX_REG_AI] = 0x14, |
| 255 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 256 | [BQ27XXX_REG_TTE] = 0x16, |
| 257 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 258 | [BQ27XXX_REG_TTES] = 0x1a, |
| 259 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 260 | [BQ27XXX_REG_NAC] = 0x0c, |
| 261 | [BQ27XXX_REG_FCC] = 0x12, |
| 262 | [BQ27XXX_REG_CYCT] = 0x1e, |
| 263 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 264 | [BQ27XXX_REG_SOC] = 0x20, |
| 265 | [BQ27XXX_REG_DCAP] = 0x2e, |
| 266 | [BQ27XXX_REG_AP] = INVALID_REG_ADDR, |
| 267 | }, |
Chris Lapa | 68f2a81 | 2017-01-11 12:44:44 +1100 | [diff] [blame] | 268 | [BQ27520G1] = { |
| 269 | [BQ27XXX_REG_CTRL] = 0x00, |
| 270 | [BQ27XXX_REG_TEMP] = 0x06, |
| 271 | [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, |
| 272 | [BQ27XXX_REG_VOLT] = 0x08, |
| 273 | [BQ27XXX_REG_AI] = 0x14, |
| 274 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 275 | [BQ27XXX_REG_TTE] = 0x16, |
| 276 | [BQ27XXX_REG_TTF] = 0x18, |
| 277 | [BQ27XXX_REG_TTES] = 0x1c, |
| 278 | [BQ27XXX_REG_TTECP] = 0x26, |
| 279 | [BQ27XXX_REG_NAC] = 0x0c, |
| 280 | [BQ27XXX_REG_FCC] = 0x12, |
| 281 | [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR, |
| 282 | [BQ27XXX_REG_AE] = 0x22, |
| 283 | [BQ27XXX_REG_SOC] = 0x2c, |
| 284 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 285 | [BQ27XXX_REG_AP] = 0x24, |
| 286 | }, |
Chris Lapa | a5deb9a | 2017-01-11 12:44:45 +1100 | [diff] [blame] | 287 | [BQ27520G2] = { |
| 288 | [BQ27XXX_REG_CTRL] = 0x00, |
| 289 | [BQ27XXX_REG_TEMP] = 0x06, |
| 290 | [BQ27XXX_REG_INT_TEMP] = 0x36, |
| 291 | [BQ27XXX_REG_VOLT] = 0x08, |
| 292 | [BQ27XXX_REG_AI] = 0x14, |
| 293 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 294 | [BQ27XXX_REG_TTE] = 0x16, |
| 295 | [BQ27XXX_REG_TTF] = 0x18, |
| 296 | [BQ27XXX_REG_TTES] = 0x1c, |
| 297 | [BQ27XXX_REG_TTECP] = 0x26, |
| 298 | [BQ27XXX_REG_NAC] = 0x0c, |
| 299 | [BQ27XXX_REG_FCC] = 0x12, |
| 300 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 301 | [BQ27XXX_REG_AE] = 0x22, |
| 302 | [BQ27XXX_REG_SOC] = 0x2c, |
| 303 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 304 | [BQ27XXX_REG_AP] = 0x24, |
| 305 | }, |
Chris Lapa | 825e915 | 2017-01-11 12:44:46 +1100 | [diff] [blame] | 306 | [BQ27520G3] = { |
| 307 | [BQ27XXX_REG_CTRL] = 0x00, |
| 308 | [BQ27XXX_REG_TEMP] = 0x06, |
| 309 | [BQ27XXX_REG_INT_TEMP] = 0x36, |
| 310 | [BQ27XXX_REG_VOLT] = 0x08, |
| 311 | [BQ27XXX_REG_AI] = 0x14, |
| 312 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 313 | [BQ27XXX_REG_TTE] = 0x16, |
| 314 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 315 | [BQ27XXX_REG_TTES] = 0x1c, |
| 316 | [BQ27XXX_REG_TTECP] = 0x26, |
| 317 | [BQ27XXX_REG_NAC] = 0x0c, |
| 318 | [BQ27XXX_REG_FCC] = 0x12, |
| 319 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 320 | [BQ27XXX_REG_AE] = 0x22, |
| 321 | [BQ27XXX_REG_SOC] = 0x2c, |
| 322 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 323 | [BQ27XXX_REG_AP] = 0x24, |
| 324 | }, |
Chris Lapa | 8835cae | 2017-01-11 12:44:47 +1100 | [diff] [blame] | 325 | [BQ27520G4] = { |
| 326 | [BQ27XXX_REG_CTRL] = 0x00, |
| 327 | [BQ27XXX_REG_TEMP] = 0x06, |
| 328 | [BQ27XXX_REG_INT_TEMP] = 0x28, |
| 329 | [BQ27XXX_REG_VOLT] = 0x08, |
| 330 | [BQ27XXX_REG_AI] = 0x14, |
| 331 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 332 | [BQ27XXX_REG_TTE] = 0x16, |
| 333 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 334 | [BQ27XXX_REG_TTES] = 0x1c, |
| 335 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 336 | [BQ27XXX_REG_NAC] = 0x0c, |
| 337 | [BQ27XXX_REG_FCC] = 0x12, |
| 338 | [BQ27XXX_REG_CYCT] = 0x1e, |
| 339 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 340 | [BQ27XXX_REG_SOC] = 0x20, |
| 341 | [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, |
| 342 | [BQ27XXX_REG_AP] = INVALID_REG_ADDR, |
| 343 | }, |
Andrew F. Davis | 2e05b51 | 2016-06-17 17:02:35 -0500 | [diff] [blame] | 344 | [BQ27530] = { |
| 345 | [BQ27XXX_REG_CTRL] = 0x00, |
| 346 | [BQ27XXX_REG_TEMP] = 0x06, |
| 347 | [BQ27XXX_REG_INT_TEMP] = 0x32, |
| 348 | [BQ27XXX_REG_VOLT] = 0x08, |
| 349 | [BQ27XXX_REG_AI] = 0x14, |
| 350 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 351 | [BQ27XXX_REG_TTE] = 0x16, |
| 352 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 353 | [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, |
| 354 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 355 | [BQ27XXX_REG_NAC] = 0x0c, |
| 356 | [BQ27XXX_REG_FCC] = 0x12, |
| 357 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 358 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 359 | [BQ27XXX_REG_SOC] = 0x2c, |
| 360 | [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, |
| 361 | [BQ27XXX_REG_AP] = 0x24, |
| 362 | }, |
| 363 | [BQ27541] = { |
| 364 | [BQ27XXX_REG_CTRL] = 0x00, |
| 365 | [BQ27XXX_REG_TEMP] = 0x06, |
| 366 | [BQ27XXX_REG_INT_TEMP] = 0x28, |
| 367 | [BQ27XXX_REG_VOLT] = 0x08, |
| 368 | [BQ27XXX_REG_AI] = 0x14, |
| 369 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 370 | [BQ27XXX_REG_TTE] = 0x16, |
| 371 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 372 | [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, |
| 373 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 374 | [BQ27XXX_REG_NAC] = 0x0c, |
| 375 | [BQ27XXX_REG_FCC] = 0x12, |
| 376 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 377 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 378 | [BQ27XXX_REG_SOC] = 0x2c, |
| 379 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 380 | [BQ27XXX_REG_AP] = 0x24, |
| 381 | }, |
| 382 | [BQ27545] = { |
| 383 | [BQ27XXX_REG_CTRL] = 0x00, |
| 384 | [BQ27XXX_REG_TEMP] = 0x06, |
| 385 | [BQ27XXX_REG_INT_TEMP] = 0x28, |
| 386 | [BQ27XXX_REG_VOLT] = 0x08, |
| 387 | [BQ27XXX_REG_AI] = 0x14, |
| 388 | [BQ27XXX_REG_FLAGS] = 0x0a, |
| 389 | [BQ27XXX_REG_TTE] = 0x16, |
| 390 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 391 | [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, |
| 392 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 393 | [BQ27XXX_REG_NAC] = 0x0c, |
| 394 | [BQ27XXX_REG_FCC] = 0x12, |
| 395 | [BQ27XXX_REG_CYCT] = 0x2a, |
| 396 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 397 | [BQ27XXX_REG_SOC] = 0x2c, |
| 398 | [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, |
| 399 | [BQ27XXX_REG_AP] = 0x24, |
| 400 | }, |
| 401 | [BQ27421] = { |
| 402 | [BQ27XXX_REG_CTRL] = 0x00, |
| 403 | [BQ27XXX_REG_TEMP] = 0x02, |
| 404 | [BQ27XXX_REG_INT_TEMP] = 0x1e, |
| 405 | [BQ27XXX_REG_VOLT] = 0x04, |
| 406 | [BQ27XXX_REG_AI] = 0x10, |
| 407 | [BQ27XXX_REG_FLAGS] = 0x06, |
| 408 | [BQ27XXX_REG_TTE] = INVALID_REG_ADDR, |
| 409 | [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, |
| 410 | [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, |
| 411 | [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, |
| 412 | [BQ27XXX_REG_NAC] = 0x08, |
| 413 | [BQ27XXX_REG_FCC] = 0x0e, |
| 414 | [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR, |
| 415 | [BQ27XXX_REG_AE] = INVALID_REG_ADDR, |
| 416 | [BQ27XXX_REG_SOC] = 0x1c, |
| 417 | [BQ27XXX_REG_DCAP] = 0x3c, |
| 418 | [BQ27XXX_REG_AP] = 0x18, |
| 419 | }, |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | static enum power_supply_property bq27000_battery_props[] = { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 423 | POWER_SUPPLY_PROP_STATUS, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 424 | POWER_SUPPLY_PROP_PRESENT, |
| 425 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 426 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 427 | POWER_SUPPLY_PROP_CAPACITY, |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 428 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 429 | POWER_SUPPLY_PROP_TEMP, |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 430 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 431 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 432 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 433 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 434 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 435 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 436 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 437 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 438 | POWER_SUPPLY_PROP_ENERGY_NOW, |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 439 | POWER_SUPPLY_PROP_POWER_AVG, |
| 440 | POWER_SUPPLY_PROP_HEALTH, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 441 | POWER_SUPPLY_PROP_MANUFACTURER, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 444 | static enum power_supply_property bq27010_battery_props[] = { |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 445 | POWER_SUPPLY_PROP_STATUS, |
| 446 | POWER_SUPPLY_PROP_PRESENT, |
| 447 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 448 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 449 | POWER_SUPPLY_PROP_CAPACITY, |
| 450 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 451 | POWER_SUPPLY_PROP_TEMP, |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 452 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 453 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 454 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 455 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 456 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 457 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 458 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 459 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 460 | POWER_SUPPLY_PROP_HEALTH, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 461 | POWER_SUPPLY_PROP_MANUFACTURER, |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Chris Lapa | 818e301 | 2017-01-11 12:44:38 +1100 | [diff] [blame] | 464 | static enum power_supply_property bq2750x_battery_props[] = { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 465 | POWER_SUPPLY_PROP_STATUS, |
| 466 | POWER_SUPPLY_PROP_PRESENT, |
| 467 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 468 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 469 | POWER_SUPPLY_PROP_CAPACITY, |
| 470 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 471 | POWER_SUPPLY_PROP_TEMP, |
| 472 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 473 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 474 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 475 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 476 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 477 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 478 | POWER_SUPPLY_PROP_HEALTH, |
| 479 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 480 | }; |
| 481 | |
Chris Lapa | 6da6e4b | 2017-01-11 12:44:39 +1100 | [diff] [blame] | 482 | static enum power_supply_property bq2751x_battery_props[] = { |
Andrew F. Davis | 3bee9ea | 2016-11-04 13:33:13 -0500 | [diff] [blame] | 483 | POWER_SUPPLY_PROP_STATUS, |
| 484 | POWER_SUPPLY_PROP_PRESENT, |
| 485 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 486 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 487 | POWER_SUPPLY_PROP_CAPACITY, |
| 488 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 489 | POWER_SUPPLY_PROP_TEMP, |
| 490 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 491 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 492 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 493 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 494 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 495 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 496 | POWER_SUPPLY_PROP_HEALTH, |
| 497 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 498 | }; |
| 499 | |
Chris Lapa | 3283363 | 2017-01-11 12:44:40 +1100 | [diff] [blame] | 500 | static enum power_supply_property bq27500_battery_props[] = { |
| 501 | POWER_SUPPLY_PROP_STATUS, |
| 502 | POWER_SUPPLY_PROP_PRESENT, |
| 503 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 504 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 505 | POWER_SUPPLY_PROP_CAPACITY, |
| 506 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 507 | POWER_SUPPLY_PROP_TEMP, |
| 508 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 509 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 510 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 511 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 512 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 513 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 514 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 515 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 516 | POWER_SUPPLY_PROP_POWER_AVG, |
| 517 | POWER_SUPPLY_PROP_HEALTH, |
| 518 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 519 | }; |
| 520 | |
Chris Lapa | bd28177 | 2017-01-11 12:44:41 +1100 | [diff] [blame] | 521 | static enum power_supply_property bq27510g1_battery_props[] = { |
| 522 | POWER_SUPPLY_PROP_STATUS, |
| 523 | POWER_SUPPLY_PROP_PRESENT, |
| 524 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 525 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 526 | POWER_SUPPLY_PROP_CAPACITY, |
| 527 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 528 | POWER_SUPPLY_PROP_TEMP, |
| 529 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 530 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 531 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 532 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 533 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 534 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 535 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 536 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 537 | POWER_SUPPLY_PROP_POWER_AVG, |
| 538 | POWER_SUPPLY_PROP_HEALTH, |
| 539 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 540 | }; |
| 541 | |
Chris Lapa | 698a2bf | 2017-01-11 12:44:42 +1100 | [diff] [blame] | 542 | static enum power_supply_property bq27510g2_battery_props[] = { |
| 543 | POWER_SUPPLY_PROP_STATUS, |
| 544 | POWER_SUPPLY_PROP_PRESENT, |
| 545 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 546 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 547 | POWER_SUPPLY_PROP_CAPACITY, |
| 548 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 549 | POWER_SUPPLY_PROP_TEMP, |
| 550 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 551 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 552 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 553 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 554 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 555 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 556 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 557 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 558 | POWER_SUPPLY_PROP_POWER_AVG, |
| 559 | POWER_SUPPLY_PROP_HEALTH, |
| 560 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 561 | }; |
| 562 | |
Chris Lapa | 71375aa | 2017-01-11 12:44:43 +1100 | [diff] [blame] | 563 | static enum power_supply_property bq27510g3_battery_props[] = { |
| 564 | POWER_SUPPLY_PROP_STATUS, |
| 565 | POWER_SUPPLY_PROP_PRESENT, |
| 566 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 567 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 568 | POWER_SUPPLY_PROP_CAPACITY, |
| 569 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 570 | POWER_SUPPLY_PROP_TEMP, |
| 571 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 572 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 573 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 574 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 575 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 576 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 577 | POWER_SUPPLY_PROP_HEALTH, |
| 578 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 579 | }; |
| 580 | |
Chris Lapa | 68f2a81 | 2017-01-11 12:44:44 +1100 | [diff] [blame] | 581 | static enum power_supply_property bq27520g1_battery_props[] = { |
| 582 | POWER_SUPPLY_PROP_STATUS, |
| 583 | POWER_SUPPLY_PROP_PRESENT, |
| 584 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 585 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 586 | POWER_SUPPLY_PROP_CAPACITY, |
| 587 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 588 | POWER_SUPPLY_PROP_TEMP, |
| 589 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 590 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 591 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 592 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 593 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 594 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 595 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 596 | POWER_SUPPLY_PROP_POWER_AVG, |
| 597 | POWER_SUPPLY_PROP_HEALTH, |
| 598 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 599 | }; |
| 600 | |
Chris Lapa | a5deb9a | 2017-01-11 12:44:45 +1100 | [diff] [blame] | 601 | static enum power_supply_property bq27520g2_battery_props[] = { |
| 602 | POWER_SUPPLY_PROP_STATUS, |
| 603 | POWER_SUPPLY_PROP_PRESENT, |
| 604 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 605 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 606 | POWER_SUPPLY_PROP_CAPACITY, |
| 607 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 608 | POWER_SUPPLY_PROP_TEMP, |
| 609 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 610 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 611 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 612 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 613 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 614 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 615 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 616 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 617 | POWER_SUPPLY_PROP_POWER_AVG, |
| 618 | POWER_SUPPLY_PROP_HEALTH, |
| 619 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 620 | }; |
| 621 | |
Chris Lapa | 825e915 | 2017-01-11 12:44:46 +1100 | [diff] [blame] | 622 | static enum power_supply_property bq27520g3_battery_props[] = { |
| 623 | POWER_SUPPLY_PROP_STATUS, |
| 624 | POWER_SUPPLY_PROP_PRESENT, |
| 625 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 626 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 627 | POWER_SUPPLY_PROP_CAPACITY, |
| 628 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 629 | POWER_SUPPLY_PROP_TEMP, |
| 630 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 631 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 632 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 633 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 634 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 635 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 636 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 637 | POWER_SUPPLY_PROP_POWER_AVG, |
| 638 | POWER_SUPPLY_PROP_HEALTH, |
| 639 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 640 | }; |
| 641 | |
Chris Lapa | 8835cae | 2017-01-11 12:44:47 +1100 | [diff] [blame] | 642 | static enum power_supply_property bq27520g4_battery_props[] = { |
| 643 | POWER_SUPPLY_PROP_STATUS, |
| 644 | POWER_SUPPLY_PROP_PRESENT, |
| 645 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 646 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 647 | POWER_SUPPLY_PROP_CAPACITY, |
| 648 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 649 | POWER_SUPPLY_PROP_TEMP, |
| 650 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 651 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 652 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 653 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 654 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 655 | POWER_SUPPLY_PROP_HEALTH, |
| 656 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 657 | }; |
| 658 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 659 | static enum power_supply_property bq27530_battery_props[] = { |
| 660 | POWER_SUPPLY_PROP_STATUS, |
| 661 | POWER_SUPPLY_PROP_PRESENT, |
| 662 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 663 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 664 | POWER_SUPPLY_PROP_CAPACITY, |
| 665 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 666 | POWER_SUPPLY_PROP_TEMP, |
| 667 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 668 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 669 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 670 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 671 | POWER_SUPPLY_PROP_POWER_AVG, |
| 672 | POWER_SUPPLY_PROP_HEALTH, |
| 673 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 674 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 675 | }; |
| 676 | |
| 677 | static enum power_supply_property bq27541_battery_props[] = { |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 678 | POWER_SUPPLY_PROP_STATUS, |
| 679 | POWER_SUPPLY_PROP_PRESENT, |
| 680 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 681 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 682 | POWER_SUPPLY_PROP_CAPACITY, |
| 683 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 684 | POWER_SUPPLY_PROP_TEMP, |
| 685 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 686 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 687 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 688 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 689 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 690 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 691 | POWER_SUPPLY_PROP_POWER_AVG, |
| 692 | POWER_SUPPLY_PROP_HEALTH, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 693 | POWER_SUPPLY_PROP_MANUFACTURER, |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 694 | }; |
| 695 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 696 | static enum power_supply_property bq27545_battery_props[] = { |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 697 | POWER_SUPPLY_PROP_STATUS, |
| 698 | POWER_SUPPLY_PROP_PRESENT, |
| 699 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 700 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 701 | POWER_SUPPLY_PROP_CAPACITY, |
| 702 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 703 | POWER_SUPPLY_PROP_TEMP, |
| 704 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 705 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 706 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 707 | POWER_SUPPLY_PROP_CHARGE_NOW, |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 708 | POWER_SUPPLY_PROP_HEALTH, |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 709 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 710 | POWER_SUPPLY_PROP_POWER_AVG, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 711 | POWER_SUPPLY_PROP_MANUFACTURER, |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 712 | }; |
| 713 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 714 | static enum power_supply_property bq27421_battery_props[] = { |
| 715 | POWER_SUPPLY_PROP_STATUS, |
| 716 | POWER_SUPPLY_PROP_PRESENT, |
| 717 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 718 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 719 | POWER_SUPPLY_PROP_CAPACITY, |
| 720 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 721 | POWER_SUPPLY_PROP_TEMP, |
| 722 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 723 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 724 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 725 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 726 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 727 | }; |
| 728 | |
| 729 | #define BQ27XXX_PROP(_id, _prop) \ |
| 730 | [_id] = { \ |
| 731 | .props = _prop, \ |
| 732 | .size = ARRAY_SIZE(_prop), \ |
| 733 | } |
| 734 | |
| 735 | static struct { |
| 736 | enum power_supply_property *props; |
| 737 | size_t size; |
| 738 | } bq27xxx_battery_props[] = { |
| 739 | BQ27XXX_PROP(BQ27000, bq27000_battery_props), |
| 740 | BQ27XXX_PROP(BQ27010, bq27010_battery_props), |
Chris Lapa | 818e301 | 2017-01-11 12:44:38 +1100 | [diff] [blame] | 741 | BQ27XXX_PROP(BQ2750X, bq2750x_battery_props), |
Chris Lapa | 6da6e4b | 2017-01-11 12:44:39 +1100 | [diff] [blame] | 742 | BQ27XXX_PROP(BQ2751X, bq2751x_battery_props), |
Chris Lapa | 3283363 | 2017-01-11 12:44:40 +1100 | [diff] [blame] | 743 | BQ27XXX_PROP(BQ27500, bq27500_battery_props), |
Chris Lapa | bd28177 | 2017-01-11 12:44:41 +1100 | [diff] [blame] | 744 | BQ27XXX_PROP(BQ27510G1, bq27510g1_battery_props), |
Chris Lapa | 698a2bf | 2017-01-11 12:44:42 +1100 | [diff] [blame] | 745 | BQ27XXX_PROP(BQ27510G2, bq27510g2_battery_props), |
Chris Lapa | 71375aa | 2017-01-11 12:44:43 +1100 | [diff] [blame] | 746 | BQ27XXX_PROP(BQ27510G3, bq27510g3_battery_props), |
Chris Lapa | 68f2a81 | 2017-01-11 12:44:44 +1100 | [diff] [blame] | 747 | BQ27XXX_PROP(BQ27520G1, bq27520g1_battery_props), |
Chris Lapa | a5deb9a | 2017-01-11 12:44:45 +1100 | [diff] [blame] | 748 | BQ27XXX_PROP(BQ27520G2, bq27520g2_battery_props), |
Chris Lapa | 825e915 | 2017-01-11 12:44:46 +1100 | [diff] [blame] | 749 | BQ27XXX_PROP(BQ27520G3, bq27520g3_battery_props), |
Chris Lapa | 8835cae | 2017-01-11 12:44:47 +1100 | [diff] [blame] | 750 | BQ27XXX_PROP(BQ27520G4, bq27520g4_battery_props), |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 751 | BQ27XXX_PROP(BQ27530, bq27530_battery_props), |
| 752 | BQ27XXX_PROP(BQ27541, bq27541_battery_props), |
| 753 | BQ27XXX_PROP(BQ27545, bq27545_battery_props), |
| 754 | BQ27XXX_PROP(BQ27421, bq27421_battery_props), |
| 755 | }; |
| 756 | |
Matt Ranostay | 1d72706 | 2016-09-19 20:43:02 -0700 | [diff] [blame] | 757 | static DEFINE_MUTEX(bq27xxx_list_lock); |
| 758 | static LIST_HEAD(bq27xxx_battery_devices); |
| 759 | |
| 760 | static int poll_interval_param_set(const char *val, const struct kernel_param *kp) |
| 761 | { |
| 762 | struct bq27xxx_device_info *di; |
Matt Ranostay | 950b6c2 | 2016-09-30 21:45:30 -0700 | [diff] [blame] | 763 | unsigned int prev_val = *(unsigned int *) kp->arg; |
Matt Ranostay | 1d72706 | 2016-09-19 20:43:02 -0700 | [diff] [blame] | 764 | int ret; |
| 765 | |
| 766 | ret = param_set_uint(val, kp); |
Matt Ranostay | 950b6c2 | 2016-09-30 21:45:30 -0700 | [diff] [blame] | 767 | if (ret < 0 || prev_val == *(unsigned int *) kp->arg) |
Matt Ranostay | 1d72706 | 2016-09-19 20:43:02 -0700 | [diff] [blame] | 768 | return ret; |
| 769 | |
| 770 | mutex_lock(&bq27xxx_list_lock); |
| 771 | list_for_each_entry(di, &bq27xxx_battery_devices, list) { |
| 772 | cancel_delayed_work_sync(&di->work); |
| 773 | schedule_delayed_work(&di->work, 0); |
| 774 | } |
| 775 | mutex_unlock(&bq27xxx_list_lock); |
| 776 | |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | static const struct kernel_param_ops param_ops_poll_interval = { |
| 781 | .get = param_get_uint, |
| 782 | .set = poll_interval_param_set, |
| 783 | }; |
| 784 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 785 | static unsigned int poll_interval = 360; |
Matt Ranostay | 1d72706 | 2016-09-19 20:43:02 -0700 | [diff] [blame] | 786 | module_param_cb(poll_interval, ¶m_ops_poll_interval, &poll_interval, 0644); |
Andrew F. Davis | 6d0a181 | 2015-07-22 16:51:56 -0500 | [diff] [blame] | 787 | MODULE_PARM_DESC(poll_interval, |
| 788 | "battery poll interval in seconds - 0 disables polling"); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 789 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 790 | /* |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 791 | * Common code for BQ27xxx devices |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 792 | */ |
| 793 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 794 | static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 795 | bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 796 | { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 797 | /* Reports EINVAL for invalid/missing registers */ |
| 798 | if (!di || di->regs[reg_index] == INVALID_REG_ADDR) |
| 799 | return -EINVAL; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 800 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 801 | return di->bus.read(di, di->regs[reg_index], single); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 805 | * Return the battery State-of-Charge |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 806 | * Or < 0 if something fails. |
| 807 | */ |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 808 | static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 809 | { |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 810 | int soc; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 811 | |
H. Nikolaus Schaller | 549d7b3 | 2015-12-17 11:12:53 +0100 | [diff] [blame] | 812 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 813 | soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true); |
| 814 | else |
| 815 | soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 816 | |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 817 | if (soc < 0) |
| 818 | dev_dbg(di->dev, "error reading State-of-Charge\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 819 | |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 820 | return soc; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 824 | * Return a battery charge value in µAh |
| 825 | * Or < 0 if something fails. |
| 826 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 827 | static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg) |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 828 | { |
| 829 | int charge; |
| 830 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 831 | charge = bq27xxx_read(di, reg, false); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 832 | if (charge < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 833 | dev_dbg(di->dev, "error reading charge register %02x: %d\n", |
| 834 | reg, charge); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 835 | return charge; |
| 836 | } |
| 837 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 838 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 839 | charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 840 | else |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 841 | charge *= 1000; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 842 | |
| 843 | return charge; |
| 844 | } |
| 845 | |
| 846 | /* |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 847 | * Return the battery Nominal available capacity in µAh |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 848 | * Or < 0 if something fails. |
| 849 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 850 | static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di) |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 851 | { |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 852 | int flags; |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 853 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 854 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
| 855 | flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); |
| 856 | if (flags >= 0 && (flags & BQ27000_FLAG_CI)) |
| 857 | return -ENODATA; |
Andrew F. Davis | 6d0a181 | 2015-07-22 16:51:56 -0500 | [diff] [blame] | 858 | } |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 859 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 860 | return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC); |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Return the battery Full Charge Capacity in µAh |
| 865 | * Or < 0 if something fails. |
| 866 | */ |
| 867 | static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di) |
| 868 | { |
| 869 | return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC); |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Return the Design Capacity in µAh |
| 874 | * Or < 0 if something fails. |
| 875 | */ |
| 876 | static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di) |
| 877 | { |
| 878 | int dcap; |
| 879 | |
H. Nikolaus Schaller | 549d7b3 | 2015-12-17 11:12:53 +0100 | [diff] [blame] | 880 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 881 | dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true); |
| 882 | else |
| 883 | dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 884 | |
| 885 | if (dcap < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 886 | dev_dbg(di->dev, "error reading initial last measured discharge\n"); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 887 | return dcap; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 888 | } |
| 889 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 890 | if (di->chip == BQ27000 || di->chip == BQ27010) |
H. Nikolaus Schaller | 549d7b3 | 2015-12-17 11:12:53 +0100 | [diff] [blame] | 891 | dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 892 | else |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 893 | dcap *= 1000; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 894 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 895 | return dcap; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 899 | * Return the battery Available energy in µWh |
| 900 | * Or < 0 if something fails. |
| 901 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 902 | static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di) |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 903 | { |
| 904 | int ae; |
| 905 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 906 | ae = bq27xxx_read(di, BQ27XXX_REG_AE, false); |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 907 | if (ae < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 908 | dev_dbg(di->dev, "error reading available energy\n"); |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 909 | return ae; |
| 910 | } |
| 911 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 912 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 913 | ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 914 | else |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 915 | ae *= 1000; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 916 | |
| 917 | return ae; |
| 918 | } |
| 919 | |
| 920 | /* |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 921 | * Return the battery temperature in tenths of degree Kelvin |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 922 | * Or < 0 if something fails. |
| 923 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 924 | static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di) |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 925 | { |
| 926 | int temp; |
| 927 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 928 | temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false); |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 929 | if (temp < 0) { |
| 930 | dev_err(di->dev, "error reading temperature\n"); |
| 931 | return temp; |
| 932 | } |
| 933 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 934 | if (di->chip == BQ27000 || di->chip == BQ27010) |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 935 | temp = 5 * temp / 2; |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 936 | |
| 937 | return temp; |
| 938 | } |
| 939 | |
| 940 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 941 | * Return the battery Cycle count total |
| 942 | * Or < 0 if something fails. |
| 943 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 944 | static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di) |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 945 | { |
| 946 | int cyct; |
| 947 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 948 | cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 949 | if (cyct < 0) |
| 950 | dev_err(di->dev, "error reading cycle count total\n"); |
| 951 | |
| 952 | return cyct; |
| 953 | } |
| 954 | |
| 955 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 956 | * Read a time register. |
| 957 | * Return < 0 if something fails. |
| 958 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 959 | static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 960 | { |
| 961 | int tval; |
| 962 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 963 | tval = bq27xxx_read(di, reg, false); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 964 | if (tval < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 965 | dev_dbg(di->dev, "error reading time register %02x: %d\n", |
| 966 | reg, tval); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 967 | return tval; |
| 968 | } |
| 969 | |
| 970 | if (tval == 65535) |
| 971 | return -ENODATA; |
| 972 | |
| 973 | return tval * 60; |
| 974 | } |
| 975 | |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 976 | /* |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 977 | * Read an average power register. |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 978 | * Return < 0 if something fails. |
| 979 | */ |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 980 | static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di) |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 981 | { |
| 982 | int tval; |
| 983 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 984 | tval = bq27xxx_read(di, BQ27XXX_REG_AP, false); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 985 | if (tval < 0) { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 986 | dev_err(di->dev, "error reading average power register %02x: %d\n", |
| 987 | BQ27XXX_REG_AP, tval); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 988 | return tval; |
| 989 | } |
| 990 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 991 | if (di->chip == BQ27000 || di->chip == BQ27010) |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 992 | return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 993 | else |
| 994 | return tval; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Returns true if a battery over temperature condition is detected |
| 999 | */ |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 1000 | static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1001 | { |
Chris Lapa | e839a44 | 2017-01-09 11:47:35 +1100 | [diff] [blame] | 1002 | switch (di->chip) { |
Chris Lapa | 818e301 | 2017-01-11 12:44:38 +1100 | [diff] [blame] | 1003 | case BQ2750X: |
Chris Lapa | 6da6e4b | 2017-01-11 12:44:39 +1100 | [diff] [blame] | 1004 | case BQ2751X: |
Chris Lapa | 3283363 | 2017-01-11 12:44:40 +1100 | [diff] [blame] | 1005 | case BQ27500: |
Chris Lapa | bd28177 | 2017-01-11 12:44:41 +1100 | [diff] [blame] | 1006 | case BQ27510G1: |
Chris Lapa | 698a2bf | 2017-01-11 12:44:42 +1100 | [diff] [blame] | 1007 | case BQ27510G2: |
Chris Lapa | 71375aa | 2017-01-11 12:44:43 +1100 | [diff] [blame] | 1008 | case BQ27510G3: |
Chris Lapa | 68f2a81 | 2017-01-11 12:44:44 +1100 | [diff] [blame] | 1009 | case BQ27520G1: |
Chris Lapa | a5deb9a | 2017-01-11 12:44:45 +1100 | [diff] [blame] | 1010 | case BQ27520G2: |
Chris Lapa | 825e915 | 2017-01-11 12:44:46 +1100 | [diff] [blame] | 1011 | case BQ27520G3: |
Chris Lapa | 8835cae | 2017-01-11 12:44:47 +1100 | [diff] [blame] | 1012 | case BQ27520G4: |
Chris Lapa | e839a44 | 2017-01-09 11:47:35 +1100 | [diff] [blame] | 1013 | case BQ27541: |
| 1014 | case BQ27545: |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1015 | return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD); |
Chris Lapa | e839a44 | 2017-01-09 11:47:35 +1100 | [diff] [blame] | 1016 | case BQ27530: |
| 1017 | case BQ27421: |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 1018 | return flags & BQ27XXX_FLAG_OT; |
Chris Lapa | e839a44 | 2017-01-09 11:47:35 +1100 | [diff] [blame] | 1019 | default: |
| 1020 | return false; |
| 1021 | } |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * Returns true if a battery under temperature condition is detected |
| 1026 | */ |
| 1027 | static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags) |
| 1028 | { |
| 1029 | if (di->chip == BQ27530 || di->chip == BQ27421) |
| 1030 | return flags & BQ27XXX_FLAG_UT; |
| 1031 | |
| 1032 | return false; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Returns true if a low state of charge condition is detected |
| 1037 | */ |
| 1038 | static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags) |
| 1039 | { |
| 1040 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 1041 | return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1042 | else |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 1043 | return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * Read flag register. |
| 1048 | * Return < 0 if something fails. |
| 1049 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1050 | static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di) |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1051 | { |
Andrzej Hajda | 2edd69a | 2015-09-28 10:51:27 +0200 | [diff] [blame] | 1052 | int flags; |
H. Nikolaus Schaller | e4a404a | 2016-07-18 18:12:09 +0200 | [diff] [blame] | 1053 | bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1054 | |
H. Nikolaus Schaller | e4a404a | 2016-07-18 18:12:09 +0200 | [diff] [blame] | 1055 | flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 1056 | if (flags < 0) { |
| 1057 | dev_err(di->dev, "error reading flag register:%d\n", flags); |
| 1058 | return flags; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1059 | } |
| 1060 | |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 1061 | /* Unlikely but important to return first */ |
| 1062 | if (unlikely(bq27xxx_battery_overtemp(di, flags))) |
| 1063 | return POWER_SUPPLY_HEALTH_OVERHEAT; |
| 1064 | if (unlikely(bq27xxx_battery_undertemp(di, flags))) |
| 1065 | return POWER_SUPPLY_HEALTH_COLD; |
| 1066 | if (unlikely(bq27xxx_battery_dead(di, flags))) |
| 1067 | return POWER_SUPPLY_HEALTH_DEAD; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1068 | |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 1069 | return POWER_SUPPLY_HEALTH_GOOD; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1070 | } |
| 1071 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1072 | void bq27xxx_battery_update(struct bq27xxx_device_info *di) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1073 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1074 | struct bq27xxx_reg_cache cache = {0, }; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1075 | bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010; |
| 1076 | bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1077 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1078 | cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); |
Marek Belisko | 3dd843e | 2014-06-30 21:49:56 +0200 | [diff] [blame] | 1079 | if ((cache.flags & 0xff) == 0xff) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1080 | cache.flags = -1; /* read error */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1081 | if (cache.flags >= 0) { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1082 | cache.temperature = bq27xxx_battery_read_temperature(di); |
| 1083 | if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) { |
NeilBrown | 1f94b25 | 2015-10-07 20:42:38 +0200 | [diff] [blame] | 1084 | dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n"); |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 1085 | cache.capacity = -ENODATA; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 1086 | cache.energy = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 1087 | cache.time_to_empty = -ENODATA; |
| 1088 | cache.time_to_empty_avg = -ENODATA; |
| 1089 | cache.time_to_full = -ENODATA; |
| 1090 | cache.charge_full = -ENODATA; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1091 | cache.health = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 1092 | } else { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1093 | if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR) |
| 1094 | cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE); |
| 1095 | if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR) |
| 1096 | cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP); |
| 1097 | if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR) |
| 1098 | cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF); |
| 1099 | cache.charge_full = bq27xxx_battery_read_fcc(di); |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 1100 | cache.capacity = bq27xxx_battery_read_soc(di); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1101 | if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR) |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1102 | cache.energy = bq27xxx_battery_read_energy(di); |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1103 | cache.health = bq27xxx_battery_read_health(di); |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 1104 | } |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1105 | if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR) |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1106 | cache.cycle_count = bq27xxx_battery_read_cyct(di); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1107 | if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR) |
| 1108 | cache.power_avg = bq27xxx_battery_read_pwr_avg(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1109 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 1110 | /* We only have to read charge design full once */ |
| 1111 | if (di->charge_design_full <= 0) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1112 | di->charge_design_full = bq27xxx_battery_read_dcap(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Puthikorn Voravootivat | 90f04a2 | 2014-10-21 18:15:37 -0700 | [diff] [blame] | 1115 | if (di->cache.capacity != cache.capacity) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1116 | power_supply_changed(di->bat); |
Puthikorn Voravootivat | 90f04a2 | 2014-10-21 18:15:37 -0700 | [diff] [blame] | 1117 | |
| 1118 | if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) |
| 1119 | di->cache = cache; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1120 | |
| 1121 | di->last_update = jiffies; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1122 | } |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1123 | EXPORT_SYMBOL_GPL(bq27xxx_battery_update); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1124 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1125 | static void bq27xxx_battery_poll(struct work_struct *work) |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1126 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1127 | struct bq27xxx_device_info *di = |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1128 | container_of(work, struct bq27xxx_device_info, |
| 1129 | work.work); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1130 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1131 | bq27xxx_battery_update(di); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1132 | |
Thomas Gleixner | 53bf837 | 2016-07-04 09:50:31 +0000 | [diff] [blame] | 1133 | if (poll_interval > 0) |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1134 | schedule_delayed_work(&di->work, poll_interval * HZ); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1137 | /* |
Pali Rohár | bf7d414 | 2011-02-01 00:23:13 +0100 | [diff] [blame] | 1138 | * Return the battery average current in µA |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1139 | * Note that current can be negative signed as well |
| 1140 | * Or 0 if something fails. |
| 1141 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1142 | static int bq27xxx_battery_current(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 1143 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1144 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1145 | int curr; |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 1146 | int flags; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1147 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1148 | curr = bq27xxx_read(di, BQ27XXX_REG_AI, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 1149 | if (curr < 0) { |
| 1150 | dev_err(di->dev, "error reading current\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1151 | return curr; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 1152 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 1153 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1154 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
H. Nikolaus Schaller | e4a404a | 2016-07-18 18:12:09 +0200 | [diff] [blame] | 1155 | flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 1156 | if (flags & BQ27000_FLAG_CHGS) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 1157 | dev_dbg(di->dev, "negative current!\n"); |
Grazvydas Ignotas | afbc74f | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 1158 | curr = -curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 1159 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1160 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1161 | val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; |
| 1162 | } else { |
| 1163 | /* Other gauges return signed value */ |
| 1164 | val->intval = (int)((s16)curr) * 1000; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1165 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 1166 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1167 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1170 | static int bq27xxx_battery_status(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 1171 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1172 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1173 | int status; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1174 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1175 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 1176 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 1177 | status = POWER_SUPPLY_STATUS_FULL; |
| 1178 | else if (di->cache.flags & BQ27000_FLAG_CHGS) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1179 | status = POWER_SUPPLY_STATUS_CHARGING; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1180 | else if (power_supply_am_i_supplied(di->bat)) |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 1181 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1182 | else |
| 1183 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1184 | } else { |
| 1185 | if (di->cache.flags & BQ27XXX_FLAG_FC) |
| 1186 | status = POWER_SUPPLY_STATUS_FULL; |
| 1187 | else if (di->cache.flags & BQ27XXX_FLAG_DSC) |
| 1188 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 1189 | else |
| 1190 | status = POWER_SUPPLY_STATUS_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | val->intval = status; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1194 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1195 | return 0; |
| 1196 | } |
| 1197 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1198 | static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 1199 | union power_supply_propval *val) |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 1200 | { |
| 1201 | int level; |
| 1202 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1203 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 1204 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 1205 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 1206 | else if (di->cache.flags & BQ27000_FLAG_EDV1) |
| 1207 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 1208 | else if (di->cache.flags & BQ27000_FLAG_EDVF) |
| 1209 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 1210 | else |
| 1211 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1212 | } else { |
| 1213 | if (di->cache.flags & BQ27XXX_FLAG_FC) |
| 1214 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 1215 | else if (di->cache.flags & BQ27XXX_FLAG_SOC1) |
| 1216 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 1217 | else if (di->cache.flags & BQ27XXX_FLAG_SOCF) |
| 1218 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 1219 | else |
| 1220 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | val->intval = level; |
| 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1228 | /* |
Thomas Weber | 91fe4d5 | 2012-02-17 17:46:21 +0100 | [diff] [blame] | 1229 | * Return the battery Voltage in millivolts |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1230 | * Or < 0 if something fails. |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1231 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1232 | static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 1233 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1234 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1235 | int volt; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1236 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1237 | volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 1238 | if (volt < 0) { |
| 1239 | dev_err(di->dev, "error reading voltage\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1240 | return volt; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 1241 | } |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1242 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1243 | val->intval = volt * 1000; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1244 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1245 | return 0; |
| 1246 | } |
| 1247 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1248 | static int bq27xxx_simple_value(int value, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 1249 | union power_supply_propval *val) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1250 | { |
| 1251 | if (value < 0) |
| 1252 | return value; |
| 1253 | |
| 1254 | val->intval = value; |
| 1255 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1256 | return 0; |
| 1257 | } |
| 1258 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1259 | static int bq27xxx_battery_get_property(struct power_supply *psy, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1260 | enum power_supply_property psp, |
| 1261 | union power_supply_propval *val) |
| 1262 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1263 | int ret = 0; |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1264 | struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 1265 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1266 | mutex_lock(&di->lock); |
| 1267 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) { |
| 1268 | cancel_delayed_work_sync(&di->work); |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1269 | bq27xxx_battery_poll(&di->work.work); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1270 | } |
| 1271 | mutex_unlock(&di->lock); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1272 | |
| 1273 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 1274 | return -ENODEV; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1275 | |
| 1276 | switch (psp) { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1277 | case POWER_SUPPLY_PROP_STATUS: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1278 | ret = bq27xxx_battery_status(di, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1279 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1280 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1281 | ret = bq27xxx_battery_voltage(di, val); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 1282 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1283 | case POWER_SUPPLY_PROP_PRESENT: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1284 | val->intval = di->cache.flags < 0 ? 0 : 1; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1285 | break; |
| 1286 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1287 | ret = bq27xxx_battery_current(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1288 | break; |
| 1289 | case POWER_SUPPLY_PROP_CAPACITY: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1290 | ret = bq27xxx_simple_value(di->cache.capacity, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1291 | break; |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 1292 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1293 | ret = bq27xxx_battery_capacity_level(di, val); |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 1294 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1295 | case POWER_SUPPLY_PROP_TEMP: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1296 | ret = bq27xxx_simple_value(di->cache.temperature, val); |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 1297 | if (ret == 0) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1298 | val->intval -= 2731; /* convert decidegree k to c */ |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1299 | break; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1300 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1301 | ret = bq27xxx_simple_value(di->cache.time_to_empty, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1302 | break; |
| 1303 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1304 | ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1305 | break; |
| 1306 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1307 | ret = bq27xxx_simple_value(di->cache.time_to_full, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1308 | break; |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 1309 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 1310 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 1311 | break; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 1312 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1313 | ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 1314 | break; |
| 1315 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1316 | ret = bq27xxx_simple_value(di->cache.charge_full, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 1317 | break; |
| 1318 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1319 | ret = bq27xxx_simple_value(di->charge_design_full, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 1320 | break; |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 1321 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1322 | ret = bq27xxx_simple_value(di->cache.cycle_count, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 1323 | break; |
| 1324 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1325 | ret = bq27xxx_simple_value(di->cache.energy, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 1326 | break; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1327 | case POWER_SUPPLY_PROP_POWER_AVG: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1328 | ret = bq27xxx_simple_value(di->cache.power_avg, val); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1329 | break; |
| 1330 | case POWER_SUPPLY_PROP_HEALTH: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1331 | ret = bq27xxx_simple_value(di->cache.health, val); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 1332 | break; |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 1333 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 1334 | val->strval = BQ27XXX_MANUFACTURER; |
| 1335 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1336 | default: |
| 1337 | return -EINVAL; |
| 1338 | } |
| 1339 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 1340 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1343 | static void bq27xxx_external_power_changed(struct power_supply *psy) |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1344 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1345 | struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1346 | |
| 1347 | cancel_delayed_work_sync(&di->work); |
| 1348 | schedule_delayed_work(&di->work, 0); |
| 1349 | } |
| 1350 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1351 | int bq27xxx_battery_setup(struct bq27xxx_device_info *di) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1352 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1353 | struct power_supply_desc *psy_desc; |
| 1354 | struct power_supply_config psy_cfg = { .drv_data = di, }; |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 1355 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1356 | INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll); |
| 1357 | mutex_init(&di->lock); |
| 1358 | di->regs = bq27xxx_regs[di->chip]; |
| 1359 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1360 | psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL); |
| 1361 | if (!psy_desc) |
| 1362 | return -ENOMEM; |
| 1363 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1364 | psy_desc->name = di->name; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1365 | psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 1366 | psy_desc->properties = bq27xxx_battery_props[di->chip].props; |
| 1367 | psy_desc->num_properties = bq27xxx_battery_props[di->chip].size; |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1368 | psy_desc->get_property = bq27xxx_battery_get_property; |
| 1369 | psy_desc->external_power_changed = bq27xxx_external_power_changed; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1370 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1371 | di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg); |
| 1372 | if (IS_ERR(di->bat)) { |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1373 | dev_err(di->dev, "failed to register battery\n"); |
| 1374 | return PTR_ERR(di->bat); |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 1378 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1379 | bq27xxx_battery_update(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1380 | |
Matt Ranostay | 1d72706 | 2016-09-19 20:43:02 -0700 | [diff] [blame] | 1381 | mutex_lock(&bq27xxx_list_lock); |
| 1382 | list_add(&di->list, &bq27xxx_battery_devices); |
| 1383 | mutex_unlock(&bq27xxx_list_lock); |
| 1384 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 1385 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1386 | } |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1387 | EXPORT_SYMBOL_GPL(bq27xxx_battery_setup); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1388 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1389 | void bq27xxx_battery_teardown(struct bq27xxx_device_info *di) |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1390 | { |
Pali Rohár | 8cfaaa8 | 2011-11-01 01:43:11 +0100 | [diff] [blame] | 1391 | /* |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1392 | * power_supply_unregister call bq27xxx_battery_get_property which |
| 1393 | * call bq27xxx_battery_poll. |
| 1394 | * Make sure that bq27xxx_battery_poll will not call |
Pali Rohár | 8cfaaa8 | 2011-11-01 01:43:11 +0100 | [diff] [blame] | 1395 | * schedule_delayed_work again after unregister (which cause OOPS). |
| 1396 | */ |
| 1397 | poll_interval = 0; |
| 1398 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1399 | cancel_delayed_work_sync(&di->work); |
| 1400 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1401 | power_supply_unregister(di->bat); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1402 | |
Matt Ranostay | 1d72706 | 2016-09-19 20:43:02 -0700 | [diff] [blame] | 1403 | mutex_lock(&bq27xxx_list_lock); |
| 1404 | list_del(&di->list); |
| 1405 | mutex_unlock(&bq27xxx_list_lock); |
| 1406 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1407 | mutex_destroy(&di->lock); |
| 1408 | } |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1409 | EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1410 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1411 | static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg, |
| 1412 | bool single) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1413 | { |
| 1414 | struct device *dev = di->dev; |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1415 | struct bq27xxx_platform_data *pdata = dev->platform_data; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1416 | unsigned int timeout = 3; |
| 1417 | int upper, lower; |
| 1418 | int temp; |
| 1419 | |
| 1420 | if (!single) { |
| 1421 | /* Make sure the value has not changed in between reading the |
| 1422 | * lower and the upper part */ |
| 1423 | upper = pdata->read(dev, reg + 1); |
| 1424 | do { |
| 1425 | temp = upper; |
| 1426 | if (upper < 0) |
| 1427 | return upper; |
| 1428 | |
| 1429 | lower = pdata->read(dev, reg); |
| 1430 | if (lower < 0) |
| 1431 | return lower; |
| 1432 | |
| 1433 | upper = pdata->read(dev, reg + 1); |
| 1434 | } while (temp != upper && --timeout); |
| 1435 | |
| 1436 | if (timeout == 0) |
| 1437 | return -EIO; |
| 1438 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1439 | return (upper << 8) | lower; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1440 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1441 | |
| 1442 | return pdata->read(dev, reg); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1445 | static int bq27xxx_battery_platform_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1446 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1447 | struct bq27xxx_device_info *di; |
| 1448 | struct bq27xxx_platform_data *pdata = pdev->dev.platform_data; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1449 | |
| 1450 | if (!pdata) { |
| 1451 | dev_err(&pdev->dev, "no platform_data supplied\n"); |
| 1452 | return -EINVAL; |
| 1453 | } |
| 1454 | |
| 1455 | if (!pdata->read) { |
| 1456 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); |
| 1457 | return -EINVAL; |
| 1458 | } |
| 1459 | |
Andrew F. Davis | 424cfde | 2015-09-22 14:35:07 -0500 | [diff] [blame] | 1460 | if (!pdata->chip) { |
| 1461 | dev_err(&pdev->dev, "no device supplied\n"); |
| 1462 | return -EINVAL; |
| 1463 | } |
| 1464 | |
Himangi Saraogi | 1cb82fd | 2014-05-28 22:58:49 +0530 | [diff] [blame] | 1465 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
Andrew F. Davis | 6d0a181 | 2015-07-22 16:51:56 -0500 | [diff] [blame] | 1466 | if (!di) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1467 | return -ENOMEM; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1468 | |
| 1469 | platform_set_drvdata(pdev, di); |
| 1470 | |
| 1471 | di->dev = &pdev->dev; |
Andrew F. Davis | 424cfde | 2015-09-22 14:35:07 -0500 | [diff] [blame] | 1472 | di->chip = pdata->chip; |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1473 | di->name = pdata->name ?: dev_name(&pdev->dev); |
| 1474 | di->bus.read = bq27xxx_battery_platform_read; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1475 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1476 | return bq27xxx_battery_setup(di); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1477 | } |
| 1478 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1479 | static int bq27xxx_battery_platform_remove(struct platform_device *pdev) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1480 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1481 | struct bq27xxx_device_info *di = platform_get_drvdata(pdev); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1482 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1483 | bq27xxx_battery_teardown(di); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1484 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1485 | return 0; |
| 1486 | } |
| 1487 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1488 | static const struct platform_device_id bq27xxx_battery_platform_id_table[] = { |
| 1489 | { "bq27000-battery", }, |
| 1490 | { /* sentinel */ } |
| 1491 | }; |
| 1492 | MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table); |
| 1493 | |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 1494 | #ifdef CONFIG_OF |
| 1495 | static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = { |
| 1496 | { .compatible = "ti,bq27000" }, |
| 1497 | {}, |
| 1498 | }; |
| 1499 | MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table); |
| 1500 | #endif |
| 1501 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1502 | static struct platform_driver bq27xxx_battery_platform_driver = { |
| 1503 | .probe = bq27xxx_battery_platform_probe, |
| 1504 | .remove = bq27xxx_battery_platform_remove, |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1505 | .driver = { |
| 1506 | .name = "bq27000-battery", |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 1507 | .of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table), |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1508 | }, |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1509 | .id_table = bq27xxx_battery_platform_id_table, |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1510 | }; |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1511 | module_platform_driver(bq27xxx_battery_platform_driver); |
Marek Belisko | 8ebb7e9 | 2015-04-30 22:05:58 +0200 | [diff] [blame] | 1512 | |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 1513 | MODULE_ALIAS("platform:bq27000-battery"); |
| 1514 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1515 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1516 | MODULE_DESCRIPTION("BQ27xxx battery monitor driver"); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1517 | MODULE_LICENSE("GPL"); |