1 /* 2 * BQ27xxx battery driver 3 * 4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> 5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it> 6 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de> 7 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com> 8 * Copyright (C) 2017 Liam Breck <kernel@networkimprov.net> 9 * 10 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. 11 * 12 * This package is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License version 2 as 14 * published by the Free Software Foundation. 15 * 16 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * Datasheets: 21 * http://www.ti.com/product/bq27000 22 * http://www.ti.com/product/bq27200 23 * http://www.ti.com/product/bq27010 24 * http://www.ti.com/product/bq27210 25 * http://www.ti.com/product/bq27500 26 * http://www.ti.com/product/bq27510-g1 27 * http://www.ti.com/product/bq27510-g2 28 * http://www.ti.com/product/bq27510-g3 29 * http://www.ti.com/product/bq27520-g4 30 * http://www.ti.com/product/bq27520-g1 31 * http://www.ti.com/product/bq27520-g2 32 * http://www.ti.com/product/bq27520-g3 33 * http://www.ti.com/product/bq27520-g4 34 * http://www.ti.com/product/bq27530-g1 35 * http://www.ti.com/product/bq27531-g1 36 * http://www.ti.com/product/bq27541-g1 37 * http://www.ti.com/product/bq27542-g1 38 * http://www.ti.com/product/bq27546-g1 39 * http://www.ti.com/product/bq27742-g1 40 * http://www.ti.com/product/bq27545-g1 41 * http://www.ti.com/product/bq27421-g1 42 * http://www.ti.com/product/bq27425-g1 43 * http://www.ti.com/product/bq27411-g1 44 * http://www.ti.com/product/bq27621-g1 45 */ 46 47 #include <linux/device.h> 48 #include <linux/module.h> 49 #include <linux/mutex.h> 50 #include <linux/param.h> 51 #include <linux/jiffies.h> 52 #include <linux/workqueue.h> 53 #include <linux/delay.h> 54 #include <linux/platform_device.h> 55 #include <linux/power_supply.h> 56 #include <linux/slab.h> 57 #include <linux/of.h> 58 59 #include <linux/power/bq27xxx_battery.h> 60 61 #define DRIVER_VERSION "1.2.0" 62 63 #define BQ27XXX_MANUFACTURER "Texas Instruments" 64 65 /* BQ27XXX Flags */ 66 #define BQ27XXX_FLAG_DSC BIT(0) 67 #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */ 68 #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */ 69 #define BQ27XXX_FLAG_CFGUP BIT(4) 70 #define BQ27XXX_FLAG_FC BIT(9) 71 #define BQ27XXX_FLAG_OTD BIT(14) 72 #define BQ27XXX_FLAG_OTC BIT(15) 73 #define BQ27XXX_FLAG_UT BIT(14) 74 #define BQ27XXX_FLAG_OT BIT(15) 75 76 /* BQ27000 has different layout for Flags register */ 77 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */ 78 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */ 79 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */ 80 #define BQ27000_FLAG_FC BIT(5) 81 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */ 82 83 /* control register params */ 84 #define BQ27XXX_SEALED 0x20 85 #define BQ27XXX_SET_CFGUPDATE 0x13 86 #define BQ27XXX_SOFT_RESET 0x42 87 #define BQ27XXX_RESET 0x41 88 89 #define BQ27XXX_RS (20) /* Resistor sense mOhm */ 90 #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */ 91 #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */ 92 93 #define INVALID_REG_ADDR 0xff 94 95 /* 96 * bq27xxx_reg_index - Register names 97 * 98 * These are indexes into a device's register mapping array. 99 */ 100 101 enum bq27xxx_reg_index { 102 BQ27XXX_REG_CTRL = 0, /* Control */ 103 BQ27XXX_REG_TEMP, /* Temperature */ 104 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */ 105 BQ27XXX_REG_VOLT, /* Voltage */ 106 BQ27XXX_REG_AI, /* Average Current */ 107 BQ27XXX_REG_FLAGS, /* Flags */ 108 BQ27XXX_REG_TTE, /* Time-to-Empty */ 109 BQ27XXX_REG_TTF, /* Time-to-Full */ 110 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */ 111 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */ 112 BQ27XXX_REG_NAC, /* Nominal Available Capacity */ 113 BQ27XXX_REG_FCC, /* Full Charge Capacity */ 114 BQ27XXX_REG_CYCT, /* Cycle Count */ 115 BQ27XXX_REG_AE, /* Available Energy */ 116 BQ27XXX_REG_SOC, /* State-of-Charge */ 117 BQ27XXX_REG_DCAP, /* Design Capacity */ 118 BQ27XXX_REG_AP, /* Average Power */ 119 BQ27XXX_DM_CTRL, /* Block Data Control */ 120 BQ27XXX_DM_CLASS, /* Data Class */ 121 BQ27XXX_DM_BLOCK, /* Data Block */ 122 BQ27XXX_DM_DATA, /* Block Data */ 123 BQ27XXX_DM_CKSUM, /* Block Data Checksum */ 124 BQ27XXX_REG_MAX, /* sentinel */ 125 }; 126 127 #define BQ27XXX_DM_REG_ROWS \ 128 [BQ27XXX_DM_CTRL] = 0x61, \ 129 [BQ27XXX_DM_CLASS] = 0x3e, \ 130 [BQ27XXX_DM_BLOCK] = 0x3f, \ 131 [BQ27XXX_DM_DATA] = 0x40, \ 132 [BQ27XXX_DM_CKSUM] = 0x60 133 134 /* Register mappings */ 135 static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = { 136 [BQ27000] = { 137 [BQ27XXX_REG_CTRL] = 0x00, 138 [BQ27XXX_REG_TEMP] = 0x06, 139 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, 140 [BQ27XXX_REG_VOLT] = 0x08, 141 [BQ27XXX_REG_AI] = 0x14, 142 [BQ27XXX_REG_FLAGS] = 0x0a, 143 [BQ27XXX_REG_TTE] = 0x16, 144 [BQ27XXX_REG_TTF] = 0x18, 145 [BQ27XXX_REG_TTES] = 0x1c, 146 [BQ27XXX_REG_TTECP] = 0x26, 147 [BQ27XXX_REG_NAC] = 0x0c, 148 [BQ27XXX_REG_FCC] = 0x12, 149 [BQ27XXX_REG_CYCT] = 0x2a, 150 [BQ27XXX_REG_AE] = 0x22, 151 [BQ27XXX_REG_SOC] = 0x0b, 152 [BQ27XXX_REG_DCAP] = 0x76, 153 [BQ27XXX_REG_AP] = 0x24, 154 [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR, 155 [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR, 156 [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR, 157 [BQ27XXX_DM_DATA] = INVALID_REG_ADDR, 158 [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR, 159 }, 160 [BQ27010] = { 161 [BQ27XXX_REG_CTRL] = 0x00, 162 [BQ27XXX_REG_TEMP] = 0x06, 163 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, 164 [BQ27XXX_REG_VOLT] = 0x08, 165 [BQ27XXX_REG_AI] = 0x14, 166 [BQ27XXX_REG_FLAGS] = 0x0a, 167 [BQ27XXX_REG_TTE] = 0x16, 168 [BQ27XXX_REG_TTF] = 0x18, 169 [BQ27XXX_REG_TTES] = 0x1c, 170 [BQ27XXX_REG_TTECP] = 0x26, 171 [BQ27XXX_REG_NAC] = 0x0c, 172 [BQ27XXX_REG_FCC] = 0x12, 173 [BQ27XXX_REG_CYCT] = 0x2a, 174 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 175 [BQ27XXX_REG_SOC] = 0x0b, 176 [BQ27XXX_REG_DCAP] = 0x76, 177 [BQ27XXX_REG_AP] = INVALID_REG_ADDR, 178 [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR, 179 [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR, 180 [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR, 181 [BQ27XXX_DM_DATA] = INVALID_REG_ADDR, 182 [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR, 183 }, 184 [BQ2750X] = { 185 [BQ27XXX_REG_CTRL] = 0x00, 186 [BQ27XXX_REG_TEMP] = 0x06, 187 [BQ27XXX_REG_INT_TEMP] = 0x28, 188 [BQ27XXX_REG_VOLT] = 0x08, 189 [BQ27XXX_REG_AI] = 0x14, 190 [BQ27XXX_REG_FLAGS] = 0x0a, 191 [BQ27XXX_REG_TTE] = 0x16, 192 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 193 [BQ27XXX_REG_TTES] = 0x1a, 194 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 195 [BQ27XXX_REG_NAC] = 0x0c, 196 [BQ27XXX_REG_FCC] = 0x12, 197 [BQ27XXX_REG_CYCT] = 0x2a, 198 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 199 [BQ27XXX_REG_SOC] = 0x2c, 200 [BQ27XXX_REG_DCAP] = 0x3c, 201 [BQ27XXX_REG_AP] = INVALID_REG_ADDR, 202 BQ27XXX_DM_REG_ROWS, 203 }, 204 [BQ2751X] = { 205 [BQ27XXX_REG_CTRL] = 0x00, 206 [BQ27XXX_REG_TEMP] = 0x06, 207 [BQ27XXX_REG_INT_TEMP] = 0x28, 208 [BQ27XXX_REG_VOLT] = 0x08, 209 [BQ27XXX_REG_AI] = 0x14, 210 [BQ27XXX_REG_FLAGS] = 0x0a, 211 [BQ27XXX_REG_TTE] = 0x16, 212 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 213 [BQ27XXX_REG_TTES] = 0x1a, 214 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 215 [BQ27XXX_REG_NAC] = 0x0c, 216 [BQ27XXX_REG_FCC] = 0x12, 217 [BQ27XXX_REG_CYCT] = 0x1e, 218 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 219 [BQ27XXX_REG_SOC] = 0x20, 220 [BQ27XXX_REG_DCAP] = 0x2e, 221 [BQ27XXX_REG_AP] = INVALID_REG_ADDR, 222 BQ27XXX_DM_REG_ROWS, 223 }, 224 [BQ27500] = { 225 [BQ27XXX_REG_CTRL] = 0x00, 226 [BQ27XXX_REG_TEMP] = 0x06, 227 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, 228 [BQ27XXX_REG_VOLT] = 0x08, 229 [BQ27XXX_REG_AI] = 0x14, 230 [BQ27XXX_REG_FLAGS] = 0x0a, 231 [BQ27XXX_REG_TTE] = 0x16, 232 [BQ27XXX_REG_TTF] = 0x18, 233 [BQ27XXX_REG_TTES] = 0x1c, 234 [BQ27XXX_REG_TTECP] = 0x26, 235 [BQ27XXX_REG_NAC] = 0x0c, 236 [BQ27XXX_REG_FCC] = 0x12, 237 [BQ27XXX_REG_CYCT] = 0x2a, 238 [BQ27XXX_REG_AE] = 0x22, 239 [BQ27XXX_REG_SOC] = 0x2c, 240 [BQ27XXX_REG_DCAP] = 0x3c, 241 [BQ27XXX_REG_AP] = 0x24, 242 BQ27XXX_DM_REG_ROWS, 243 }, 244 [BQ27510G1] = { 245 [BQ27XXX_REG_CTRL] = 0x00, 246 [BQ27XXX_REG_TEMP] = 0x06, 247 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, 248 [BQ27XXX_REG_VOLT] = 0x08, 249 [BQ27XXX_REG_AI] = 0x14, 250 [BQ27XXX_REG_FLAGS] = 0x0a, 251 [BQ27XXX_REG_TTE] = 0x16, 252 [BQ27XXX_REG_TTF] = 0x18, 253 [BQ27XXX_REG_TTES] = 0x1c, 254 [BQ27XXX_REG_TTECP] = 0x26, 255 [BQ27XXX_REG_NAC] = 0x0c, 256 [BQ27XXX_REG_FCC] = 0x12, 257 [BQ27XXX_REG_CYCT] = 0x2a, 258 [BQ27XXX_REG_AE] = 0x22, 259 [BQ27XXX_REG_SOC] = 0x2c, 260 [BQ27XXX_REG_DCAP] = 0x3c, 261 [BQ27XXX_REG_AP] = 0x24, 262 BQ27XXX_DM_REG_ROWS, 263 }, 264 [BQ27510G2] = { 265 [BQ27XXX_REG_CTRL] = 0x00, 266 [BQ27XXX_REG_TEMP] = 0x06, 267 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, 268 [BQ27XXX_REG_VOLT] = 0x08, 269 [BQ27XXX_REG_AI] = 0x14, 270 [BQ27XXX_REG_FLAGS] = 0x0a, 271 [BQ27XXX_REG_TTE] = 0x16, 272 [BQ27XXX_REG_TTF] = 0x18, 273 [BQ27XXX_REG_TTES] = 0x1c, 274 [BQ27XXX_REG_TTECP] = 0x26, 275 [BQ27XXX_REG_NAC] = 0x0c, 276 [BQ27XXX_REG_FCC] = 0x12, 277 [BQ27XXX_REG_CYCT] = 0x2a, 278 [BQ27XXX_REG_AE] = 0x22, 279 [BQ27XXX_REG_SOC] = 0x2c, 280 [BQ27XXX_REG_DCAP] = 0x3c, 281 [BQ27XXX_REG_AP] = 0x24, 282 BQ27XXX_DM_REG_ROWS, 283 }, 284 [BQ27510G3] = { 285 [BQ27XXX_REG_CTRL] = 0x00, 286 [BQ27XXX_REG_TEMP] = 0x06, 287 [BQ27XXX_REG_INT_TEMP] = 0x28, 288 [BQ27XXX_REG_VOLT] = 0x08, 289 [BQ27XXX_REG_AI] = 0x14, 290 [BQ27XXX_REG_FLAGS] = 0x0a, 291 [BQ27XXX_REG_TTE] = 0x16, 292 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 293 [BQ27XXX_REG_TTES] = 0x1a, 294 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 295 [BQ27XXX_REG_NAC] = 0x0c, 296 [BQ27XXX_REG_FCC] = 0x12, 297 [BQ27XXX_REG_CYCT] = 0x1e, 298 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 299 [BQ27XXX_REG_SOC] = 0x20, 300 [BQ27XXX_REG_DCAP] = 0x2e, 301 [BQ27XXX_REG_AP] = INVALID_REG_ADDR, 302 BQ27XXX_DM_REG_ROWS, 303 }, 304 [BQ27520G1] = { 305 [BQ27XXX_REG_CTRL] = 0x00, 306 [BQ27XXX_REG_TEMP] = 0x06, 307 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR, 308 [BQ27XXX_REG_VOLT] = 0x08, 309 [BQ27XXX_REG_AI] = 0x14, 310 [BQ27XXX_REG_FLAGS] = 0x0a, 311 [BQ27XXX_REG_TTE] = 0x16, 312 [BQ27XXX_REG_TTF] = 0x18, 313 [BQ27XXX_REG_TTES] = 0x1c, 314 [BQ27XXX_REG_TTECP] = 0x26, 315 [BQ27XXX_REG_NAC] = 0x0c, 316 [BQ27XXX_REG_FCC] = 0x12, 317 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR, 318 [BQ27XXX_REG_AE] = 0x22, 319 [BQ27XXX_REG_SOC] = 0x2c, 320 [BQ27XXX_REG_DCAP] = 0x3c, 321 [BQ27XXX_REG_AP] = 0x24, 322 BQ27XXX_DM_REG_ROWS, 323 }, 324 [BQ27520G2] = { 325 [BQ27XXX_REG_CTRL] = 0x00, 326 [BQ27XXX_REG_TEMP] = 0x06, 327 [BQ27XXX_REG_INT_TEMP] = 0x36, 328 [BQ27XXX_REG_VOLT] = 0x08, 329 [BQ27XXX_REG_AI] = 0x14, 330 [BQ27XXX_REG_FLAGS] = 0x0a, 331 [BQ27XXX_REG_TTE] = 0x16, 332 [BQ27XXX_REG_TTF] = 0x18, 333 [BQ27XXX_REG_TTES] = 0x1c, 334 [BQ27XXX_REG_TTECP] = 0x26, 335 [BQ27XXX_REG_NAC] = 0x0c, 336 [BQ27XXX_REG_FCC] = 0x12, 337 [BQ27XXX_REG_CYCT] = 0x2a, 338 [BQ27XXX_REG_AE] = 0x22, 339 [BQ27XXX_REG_SOC] = 0x2c, 340 [BQ27XXX_REG_DCAP] = 0x3c, 341 [BQ27XXX_REG_AP] = 0x24, 342 BQ27XXX_DM_REG_ROWS, 343 }, 344 [BQ27520G3] = { 345 [BQ27XXX_REG_CTRL] = 0x00, 346 [BQ27XXX_REG_TEMP] = 0x06, 347 [BQ27XXX_REG_INT_TEMP] = 0x36, 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] = 0x1c, 354 [BQ27XXX_REG_TTECP] = 0x26, 355 [BQ27XXX_REG_NAC] = 0x0c, 356 [BQ27XXX_REG_FCC] = 0x12, 357 [BQ27XXX_REG_CYCT] = 0x2a, 358 [BQ27XXX_REG_AE] = 0x22, 359 [BQ27XXX_REG_SOC] = 0x2c, 360 [BQ27XXX_REG_DCAP] = 0x3c, 361 [BQ27XXX_REG_AP] = 0x24, 362 BQ27XXX_DM_REG_ROWS, 363 }, 364 [BQ27520G4] = { 365 [BQ27XXX_REG_CTRL] = 0x00, 366 [BQ27XXX_REG_TEMP] = 0x06, 367 [BQ27XXX_REG_INT_TEMP] = 0x28, 368 [BQ27XXX_REG_VOLT] = 0x08, 369 [BQ27XXX_REG_AI] = 0x14, 370 [BQ27XXX_REG_FLAGS] = 0x0a, 371 [BQ27XXX_REG_TTE] = 0x16, 372 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 373 [BQ27XXX_REG_TTES] = 0x1c, 374 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 375 [BQ27XXX_REG_NAC] = 0x0c, 376 [BQ27XXX_REG_FCC] = 0x12, 377 [BQ27XXX_REG_CYCT] = 0x1e, 378 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 379 [BQ27XXX_REG_SOC] = 0x20, 380 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, 381 [BQ27XXX_REG_AP] = INVALID_REG_ADDR, 382 BQ27XXX_DM_REG_ROWS, 383 }, 384 [BQ27530] = { 385 [BQ27XXX_REG_CTRL] = 0x00, 386 [BQ27XXX_REG_TEMP] = 0x06, 387 [BQ27XXX_REG_INT_TEMP] = 0x32, 388 [BQ27XXX_REG_VOLT] = 0x08, 389 [BQ27XXX_REG_AI] = 0x14, 390 [BQ27XXX_REG_FLAGS] = 0x0a, 391 [BQ27XXX_REG_TTE] = 0x16, 392 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 393 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, 394 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 395 [BQ27XXX_REG_NAC] = 0x0c, 396 [BQ27XXX_REG_FCC] = 0x12, 397 [BQ27XXX_REG_CYCT] = 0x2a, 398 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 399 [BQ27XXX_REG_SOC] = 0x2c, 400 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, 401 [BQ27XXX_REG_AP] = 0x24, 402 BQ27XXX_DM_REG_ROWS, 403 }, 404 [BQ27541] = { 405 [BQ27XXX_REG_CTRL] = 0x00, 406 [BQ27XXX_REG_TEMP] = 0x06, 407 [BQ27XXX_REG_INT_TEMP] = 0x28, 408 [BQ27XXX_REG_VOLT] = 0x08, 409 [BQ27XXX_REG_AI] = 0x14, 410 [BQ27XXX_REG_FLAGS] = 0x0a, 411 [BQ27XXX_REG_TTE] = 0x16, 412 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 413 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, 414 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 415 [BQ27XXX_REG_NAC] = 0x0c, 416 [BQ27XXX_REG_FCC] = 0x12, 417 [BQ27XXX_REG_CYCT] = 0x2a, 418 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 419 [BQ27XXX_REG_SOC] = 0x2c, 420 [BQ27XXX_REG_DCAP] = 0x3c, 421 [BQ27XXX_REG_AP] = 0x24, 422 BQ27XXX_DM_REG_ROWS, 423 }, 424 [BQ27545] = { 425 [BQ27XXX_REG_CTRL] = 0x00, 426 [BQ27XXX_REG_TEMP] = 0x06, 427 [BQ27XXX_REG_INT_TEMP] = 0x28, 428 [BQ27XXX_REG_VOLT] = 0x08, 429 [BQ27XXX_REG_AI] = 0x14, 430 [BQ27XXX_REG_FLAGS] = 0x0a, 431 [BQ27XXX_REG_TTE] = 0x16, 432 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 433 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, 434 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 435 [BQ27XXX_REG_NAC] = 0x0c, 436 [BQ27XXX_REG_FCC] = 0x12, 437 [BQ27XXX_REG_CYCT] = 0x2a, 438 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 439 [BQ27XXX_REG_SOC] = 0x2c, 440 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR, 441 [BQ27XXX_REG_AP] = 0x24, 442 BQ27XXX_DM_REG_ROWS, 443 }, 444 [BQ27421] = { 445 [BQ27XXX_REG_CTRL] = 0x00, 446 [BQ27XXX_REG_TEMP] = 0x02, 447 [BQ27XXX_REG_INT_TEMP] = 0x1e, 448 [BQ27XXX_REG_VOLT] = 0x04, 449 [BQ27XXX_REG_AI] = 0x10, 450 [BQ27XXX_REG_FLAGS] = 0x06, 451 [BQ27XXX_REG_TTE] = INVALID_REG_ADDR, 452 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR, 453 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR, 454 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR, 455 [BQ27XXX_REG_NAC] = 0x08, 456 [BQ27XXX_REG_FCC] = 0x0e, 457 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR, 458 [BQ27XXX_REG_AE] = INVALID_REG_ADDR, 459 [BQ27XXX_REG_SOC] = 0x1c, 460 [BQ27XXX_REG_DCAP] = 0x3c, 461 [BQ27XXX_REG_AP] = 0x18, 462 BQ27XXX_DM_REG_ROWS, 463 }, 464 }; 465 466 static enum power_supply_property bq27000_battery_props[] = { 467 POWER_SUPPLY_PROP_STATUS, 468 POWER_SUPPLY_PROP_PRESENT, 469 POWER_SUPPLY_PROP_VOLTAGE_NOW, 470 POWER_SUPPLY_PROP_CURRENT_NOW, 471 POWER_SUPPLY_PROP_CAPACITY, 472 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 473 POWER_SUPPLY_PROP_TEMP, 474 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 475 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 476 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 477 POWER_SUPPLY_PROP_TECHNOLOGY, 478 POWER_SUPPLY_PROP_CHARGE_FULL, 479 POWER_SUPPLY_PROP_CHARGE_NOW, 480 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 481 POWER_SUPPLY_PROP_CYCLE_COUNT, 482 POWER_SUPPLY_PROP_ENERGY_NOW, 483 POWER_SUPPLY_PROP_POWER_AVG, 484 POWER_SUPPLY_PROP_HEALTH, 485 POWER_SUPPLY_PROP_MANUFACTURER, 486 }; 487 488 static enum power_supply_property bq27010_battery_props[] = { 489 POWER_SUPPLY_PROP_STATUS, 490 POWER_SUPPLY_PROP_PRESENT, 491 POWER_SUPPLY_PROP_VOLTAGE_NOW, 492 POWER_SUPPLY_PROP_CURRENT_NOW, 493 POWER_SUPPLY_PROP_CAPACITY, 494 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 495 POWER_SUPPLY_PROP_TEMP, 496 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 497 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 498 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 499 POWER_SUPPLY_PROP_TECHNOLOGY, 500 POWER_SUPPLY_PROP_CHARGE_FULL, 501 POWER_SUPPLY_PROP_CHARGE_NOW, 502 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 503 POWER_SUPPLY_PROP_CYCLE_COUNT, 504 POWER_SUPPLY_PROP_HEALTH, 505 POWER_SUPPLY_PROP_MANUFACTURER, 506 }; 507 508 static enum power_supply_property bq2750x_battery_props[] = { 509 POWER_SUPPLY_PROP_STATUS, 510 POWER_SUPPLY_PROP_PRESENT, 511 POWER_SUPPLY_PROP_VOLTAGE_NOW, 512 POWER_SUPPLY_PROP_CURRENT_NOW, 513 POWER_SUPPLY_PROP_CAPACITY, 514 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 515 POWER_SUPPLY_PROP_TEMP, 516 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 517 POWER_SUPPLY_PROP_TECHNOLOGY, 518 POWER_SUPPLY_PROP_CHARGE_FULL, 519 POWER_SUPPLY_PROP_CHARGE_NOW, 520 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 521 POWER_SUPPLY_PROP_CYCLE_COUNT, 522 POWER_SUPPLY_PROP_HEALTH, 523 POWER_SUPPLY_PROP_MANUFACTURER, 524 }; 525 526 static enum power_supply_property bq2751x_battery_props[] = { 527 POWER_SUPPLY_PROP_STATUS, 528 POWER_SUPPLY_PROP_PRESENT, 529 POWER_SUPPLY_PROP_VOLTAGE_NOW, 530 POWER_SUPPLY_PROP_CURRENT_NOW, 531 POWER_SUPPLY_PROP_CAPACITY, 532 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 533 POWER_SUPPLY_PROP_TEMP, 534 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 535 POWER_SUPPLY_PROP_TECHNOLOGY, 536 POWER_SUPPLY_PROP_CHARGE_FULL, 537 POWER_SUPPLY_PROP_CHARGE_NOW, 538 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 539 POWER_SUPPLY_PROP_CYCLE_COUNT, 540 POWER_SUPPLY_PROP_HEALTH, 541 POWER_SUPPLY_PROP_MANUFACTURER, 542 }; 543 544 static enum power_supply_property bq27500_battery_props[] = { 545 POWER_SUPPLY_PROP_STATUS, 546 POWER_SUPPLY_PROP_PRESENT, 547 POWER_SUPPLY_PROP_VOLTAGE_NOW, 548 POWER_SUPPLY_PROP_CURRENT_NOW, 549 POWER_SUPPLY_PROP_CAPACITY, 550 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 551 POWER_SUPPLY_PROP_TEMP, 552 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 553 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 554 POWER_SUPPLY_PROP_TECHNOLOGY, 555 POWER_SUPPLY_PROP_CHARGE_FULL, 556 POWER_SUPPLY_PROP_CHARGE_NOW, 557 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 558 POWER_SUPPLY_PROP_CYCLE_COUNT, 559 POWER_SUPPLY_PROP_ENERGY_NOW, 560 POWER_SUPPLY_PROP_POWER_AVG, 561 POWER_SUPPLY_PROP_HEALTH, 562 POWER_SUPPLY_PROP_MANUFACTURER, 563 }; 564 565 static enum power_supply_property bq27510g1_battery_props[] = { 566 POWER_SUPPLY_PROP_STATUS, 567 POWER_SUPPLY_PROP_PRESENT, 568 POWER_SUPPLY_PROP_VOLTAGE_NOW, 569 POWER_SUPPLY_PROP_CURRENT_NOW, 570 POWER_SUPPLY_PROP_CAPACITY, 571 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 572 POWER_SUPPLY_PROP_TEMP, 573 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 574 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 575 POWER_SUPPLY_PROP_TECHNOLOGY, 576 POWER_SUPPLY_PROP_CHARGE_FULL, 577 POWER_SUPPLY_PROP_CHARGE_NOW, 578 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 579 POWER_SUPPLY_PROP_CYCLE_COUNT, 580 POWER_SUPPLY_PROP_ENERGY_NOW, 581 POWER_SUPPLY_PROP_POWER_AVG, 582 POWER_SUPPLY_PROP_HEALTH, 583 POWER_SUPPLY_PROP_MANUFACTURER, 584 }; 585 586 static enum power_supply_property bq27510g2_battery_props[] = { 587 POWER_SUPPLY_PROP_STATUS, 588 POWER_SUPPLY_PROP_PRESENT, 589 POWER_SUPPLY_PROP_VOLTAGE_NOW, 590 POWER_SUPPLY_PROP_CURRENT_NOW, 591 POWER_SUPPLY_PROP_CAPACITY, 592 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 593 POWER_SUPPLY_PROP_TEMP, 594 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 595 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 596 POWER_SUPPLY_PROP_TECHNOLOGY, 597 POWER_SUPPLY_PROP_CHARGE_FULL, 598 POWER_SUPPLY_PROP_CHARGE_NOW, 599 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 600 POWER_SUPPLY_PROP_CYCLE_COUNT, 601 POWER_SUPPLY_PROP_ENERGY_NOW, 602 POWER_SUPPLY_PROP_POWER_AVG, 603 POWER_SUPPLY_PROP_HEALTH, 604 POWER_SUPPLY_PROP_MANUFACTURER, 605 }; 606 607 static enum power_supply_property bq27510g3_battery_props[] = { 608 POWER_SUPPLY_PROP_STATUS, 609 POWER_SUPPLY_PROP_PRESENT, 610 POWER_SUPPLY_PROP_VOLTAGE_NOW, 611 POWER_SUPPLY_PROP_CURRENT_NOW, 612 POWER_SUPPLY_PROP_CAPACITY, 613 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 614 POWER_SUPPLY_PROP_TEMP, 615 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 616 POWER_SUPPLY_PROP_TECHNOLOGY, 617 POWER_SUPPLY_PROP_CHARGE_FULL, 618 POWER_SUPPLY_PROP_CHARGE_NOW, 619 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 620 POWER_SUPPLY_PROP_CYCLE_COUNT, 621 POWER_SUPPLY_PROP_HEALTH, 622 POWER_SUPPLY_PROP_MANUFACTURER, 623 }; 624 625 static enum power_supply_property bq27520g1_battery_props[] = { 626 POWER_SUPPLY_PROP_STATUS, 627 POWER_SUPPLY_PROP_PRESENT, 628 POWER_SUPPLY_PROP_VOLTAGE_NOW, 629 POWER_SUPPLY_PROP_CURRENT_NOW, 630 POWER_SUPPLY_PROP_CAPACITY, 631 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 632 POWER_SUPPLY_PROP_TEMP, 633 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 634 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 635 POWER_SUPPLY_PROP_TECHNOLOGY, 636 POWER_SUPPLY_PROP_CHARGE_FULL, 637 POWER_SUPPLY_PROP_CHARGE_NOW, 638 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 639 POWER_SUPPLY_PROP_ENERGY_NOW, 640 POWER_SUPPLY_PROP_POWER_AVG, 641 POWER_SUPPLY_PROP_HEALTH, 642 POWER_SUPPLY_PROP_MANUFACTURER, 643 }; 644 645 static enum power_supply_property bq27520g2_battery_props[] = { 646 POWER_SUPPLY_PROP_STATUS, 647 POWER_SUPPLY_PROP_PRESENT, 648 POWER_SUPPLY_PROP_VOLTAGE_NOW, 649 POWER_SUPPLY_PROP_CURRENT_NOW, 650 POWER_SUPPLY_PROP_CAPACITY, 651 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 652 POWER_SUPPLY_PROP_TEMP, 653 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 654 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 655 POWER_SUPPLY_PROP_TECHNOLOGY, 656 POWER_SUPPLY_PROP_CHARGE_FULL, 657 POWER_SUPPLY_PROP_CHARGE_NOW, 658 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 659 POWER_SUPPLY_PROP_CYCLE_COUNT, 660 POWER_SUPPLY_PROP_ENERGY_NOW, 661 POWER_SUPPLY_PROP_POWER_AVG, 662 POWER_SUPPLY_PROP_HEALTH, 663 POWER_SUPPLY_PROP_MANUFACTURER, 664 }; 665 666 static enum power_supply_property bq27520g3_battery_props[] = { 667 POWER_SUPPLY_PROP_STATUS, 668 POWER_SUPPLY_PROP_PRESENT, 669 POWER_SUPPLY_PROP_VOLTAGE_NOW, 670 POWER_SUPPLY_PROP_CURRENT_NOW, 671 POWER_SUPPLY_PROP_CAPACITY, 672 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 673 POWER_SUPPLY_PROP_TEMP, 674 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 675 POWER_SUPPLY_PROP_TECHNOLOGY, 676 POWER_SUPPLY_PROP_CHARGE_FULL, 677 POWER_SUPPLY_PROP_CHARGE_NOW, 678 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 679 POWER_SUPPLY_PROP_CYCLE_COUNT, 680 POWER_SUPPLY_PROP_ENERGY_NOW, 681 POWER_SUPPLY_PROP_POWER_AVG, 682 POWER_SUPPLY_PROP_HEALTH, 683 POWER_SUPPLY_PROP_MANUFACTURER, 684 }; 685 686 static enum power_supply_property bq27520g4_battery_props[] = { 687 POWER_SUPPLY_PROP_STATUS, 688 POWER_SUPPLY_PROP_PRESENT, 689 POWER_SUPPLY_PROP_VOLTAGE_NOW, 690 POWER_SUPPLY_PROP_CURRENT_NOW, 691 POWER_SUPPLY_PROP_CAPACITY, 692 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 693 POWER_SUPPLY_PROP_TEMP, 694 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 695 POWER_SUPPLY_PROP_TECHNOLOGY, 696 POWER_SUPPLY_PROP_CHARGE_FULL, 697 POWER_SUPPLY_PROP_CHARGE_NOW, 698 POWER_SUPPLY_PROP_CYCLE_COUNT, 699 POWER_SUPPLY_PROP_HEALTH, 700 POWER_SUPPLY_PROP_MANUFACTURER, 701 }; 702 703 static enum power_supply_property bq27530_battery_props[] = { 704 POWER_SUPPLY_PROP_STATUS, 705 POWER_SUPPLY_PROP_PRESENT, 706 POWER_SUPPLY_PROP_VOLTAGE_NOW, 707 POWER_SUPPLY_PROP_CURRENT_NOW, 708 POWER_SUPPLY_PROP_CAPACITY, 709 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 710 POWER_SUPPLY_PROP_TEMP, 711 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 712 POWER_SUPPLY_PROP_TECHNOLOGY, 713 POWER_SUPPLY_PROP_CHARGE_FULL, 714 POWER_SUPPLY_PROP_CHARGE_NOW, 715 POWER_SUPPLY_PROP_POWER_AVG, 716 POWER_SUPPLY_PROP_HEALTH, 717 POWER_SUPPLY_PROP_CYCLE_COUNT, 718 POWER_SUPPLY_PROP_MANUFACTURER, 719 }; 720 721 static enum power_supply_property bq27541_battery_props[] = { 722 POWER_SUPPLY_PROP_STATUS, 723 POWER_SUPPLY_PROP_PRESENT, 724 POWER_SUPPLY_PROP_VOLTAGE_NOW, 725 POWER_SUPPLY_PROP_CURRENT_NOW, 726 POWER_SUPPLY_PROP_CAPACITY, 727 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 728 POWER_SUPPLY_PROP_TEMP, 729 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 730 POWER_SUPPLY_PROP_TECHNOLOGY, 731 POWER_SUPPLY_PROP_CHARGE_FULL, 732 POWER_SUPPLY_PROP_CHARGE_NOW, 733 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 734 POWER_SUPPLY_PROP_CYCLE_COUNT, 735 POWER_SUPPLY_PROP_POWER_AVG, 736 POWER_SUPPLY_PROP_HEALTH, 737 POWER_SUPPLY_PROP_MANUFACTURER, 738 }; 739 740 static enum power_supply_property bq27545_battery_props[] = { 741 POWER_SUPPLY_PROP_STATUS, 742 POWER_SUPPLY_PROP_PRESENT, 743 POWER_SUPPLY_PROP_VOLTAGE_NOW, 744 POWER_SUPPLY_PROP_CURRENT_NOW, 745 POWER_SUPPLY_PROP_CAPACITY, 746 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 747 POWER_SUPPLY_PROP_TEMP, 748 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 749 POWER_SUPPLY_PROP_TECHNOLOGY, 750 POWER_SUPPLY_PROP_CHARGE_FULL, 751 POWER_SUPPLY_PROP_CHARGE_NOW, 752 POWER_SUPPLY_PROP_HEALTH, 753 POWER_SUPPLY_PROP_CYCLE_COUNT, 754 POWER_SUPPLY_PROP_POWER_AVG, 755 POWER_SUPPLY_PROP_MANUFACTURER, 756 }; 757 758 static enum power_supply_property bq27421_battery_props[] = { 759 POWER_SUPPLY_PROP_STATUS, 760 POWER_SUPPLY_PROP_PRESENT, 761 POWER_SUPPLY_PROP_VOLTAGE_NOW, 762 POWER_SUPPLY_PROP_CURRENT_NOW, 763 POWER_SUPPLY_PROP_CAPACITY, 764 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 765 POWER_SUPPLY_PROP_TEMP, 766 POWER_SUPPLY_PROP_TECHNOLOGY, 767 POWER_SUPPLY_PROP_CHARGE_FULL, 768 POWER_SUPPLY_PROP_CHARGE_NOW, 769 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 770 POWER_SUPPLY_PROP_MANUFACTURER, 771 }; 772 773 #define BQ27XXX_PROP(_id, _prop) \ 774 [_id] = { \ 775 .props = _prop, \ 776 .size = ARRAY_SIZE(_prop), \ 777 } 778 779 static struct { 780 enum power_supply_property *props; 781 size_t size; 782 } bq27xxx_battery_props[] = { 783 BQ27XXX_PROP(BQ27000, bq27000_battery_props), 784 BQ27XXX_PROP(BQ27010, bq27010_battery_props), 785 BQ27XXX_PROP(BQ2750X, bq2750x_battery_props), 786 BQ27XXX_PROP(BQ2751X, bq2751x_battery_props), 787 BQ27XXX_PROP(BQ27500, bq27500_battery_props), 788 BQ27XXX_PROP(BQ27510G1, bq27510g1_battery_props), 789 BQ27XXX_PROP(BQ27510G2, bq27510g2_battery_props), 790 BQ27XXX_PROP(BQ27510G3, bq27510g3_battery_props), 791 BQ27XXX_PROP(BQ27520G1, bq27520g1_battery_props), 792 BQ27XXX_PROP(BQ27520G2, bq27520g2_battery_props), 793 BQ27XXX_PROP(BQ27520G3, bq27520g3_battery_props), 794 BQ27XXX_PROP(BQ27520G4, bq27520g4_battery_props), 795 BQ27XXX_PROP(BQ27530, bq27530_battery_props), 796 BQ27XXX_PROP(BQ27541, bq27541_battery_props), 797 BQ27XXX_PROP(BQ27545, bq27545_battery_props), 798 BQ27XXX_PROP(BQ27421, bq27421_battery_props), 799 }; 800 801 static DEFINE_MUTEX(bq27xxx_list_lock); 802 static LIST_HEAD(bq27xxx_battery_devices); 803 804 #define BQ27XXX_MSLEEP(i) usleep_range((i)*1000, (i)*1000+500) 805 806 #define BQ27XXX_DM_SZ 32 807 808 struct bq27xxx_dm_reg { 809 u8 subclass_id; 810 u8 offset; 811 u8 bytes; 812 u16 min, max; 813 }; 814 815 /** 816 * struct bq27xxx_dm_buf - chip data memory buffer 817 * @class: data memory subclass_id 818 * @block: data memory block number 819 * @data: data from/for the block 820 * @has_data: true if data has been filled by read 821 * @dirty: true if data has changed since last read/write 822 * 823 * Encapsulates info required to manage chip data memory blocks. 824 */ 825 struct bq27xxx_dm_buf { 826 u8 class; 827 u8 block; 828 u8 data[BQ27XXX_DM_SZ]; 829 bool has_data, dirty; 830 }; 831 832 #define BQ27XXX_DM_BUF(di, i) { \ 833 .class = (di)->dm_regs[i].subclass_id, \ 834 .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \ 835 } 836 837 static inline u16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf, 838 struct bq27xxx_dm_reg *reg) 839 { 840 if (buf->class == reg->subclass_id && 841 buf->block == reg->offset / BQ27XXX_DM_SZ) 842 return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ); 843 844 return NULL; 845 } 846 847 enum bq27xxx_dm_reg_id { 848 BQ27XXX_DM_DESIGN_CAPACITY = 0, 849 BQ27XXX_DM_DESIGN_ENERGY, 850 BQ27XXX_DM_TERMINATE_VOLTAGE, 851 }; 852 853 static const char * const bq27xxx_dm_reg_name[] = { 854 [BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity", 855 [BQ27XXX_DM_DESIGN_ENERGY] = "design-energy", 856 [BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage", 857 }; 858 859 860 static bool bq27xxx_dt_to_nvm = true; 861 module_param_named(dt_monitored_battery_updates_nvm, bq27xxx_dt_to_nvm, bool, 0444); 862 MODULE_PARM_DESC(dt_monitored_battery_updates_nvm, 863 "Devicetree monitored-battery config updates data memory on NVM/flash chips.\n" 864 "Users must set this =0 when installing a different type of battery!\n" 865 "Default is =1." 866 #ifndef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM 867 "\nSetting this affects future kernel updates, not the current configuration." 868 #endif 869 ); 870 871 static int poll_interval_param_set(const char *val, const struct kernel_param *kp) 872 { 873 struct bq27xxx_device_info *di; 874 unsigned int prev_val = *(unsigned int *) kp->arg; 875 int ret; 876 877 ret = param_set_uint(val, kp); 878 if (ret < 0 || prev_val == *(unsigned int *) kp->arg) 879 return ret; 880 881 mutex_lock(&bq27xxx_list_lock); 882 list_for_each_entry(di, &bq27xxx_battery_devices, list) { 883 cancel_delayed_work_sync(&di->work); 884 schedule_delayed_work(&di->work, 0); 885 } 886 mutex_unlock(&bq27xxx_list_lock); 887 888 return ret; 889 } 890 891 static const struct kernel_param_ops param_ops_poll_interval = { 892 .get = param_get_uint, 893 .set = poll_interval_param_set, 894 }; 895 896 static unsigned int poll_interval = 360; 897 module_param_cb(poll_interval, ¶m_ops_poll_interval, &poll_interval, 0644); 898 MODULE_PARM_DESC(poll_interval, 899 "battery poll interval in seconds - 0 disables polling"); 900 901 /* 902 * Common code for BQ27xxx devices 903 */ 904 905 static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index, 906 bool single) 907 { 908 int ret; 909 910 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) 911 return -EINVAL; 912 913 ret = di->bus.read(di, di->regs[reg_index], single); 914 if (ret < 0) 915 dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n", 916 di->regs[reg_index], reg_index); 917 918 return ret; 919 } 920 921 static inline int bq27xxx_write(struct bq27xxx_device_info *di, int reg_index, 922 u16 value, bool single) 923 { 924 int ret; 925 926 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) 927 return -EINVAL; 928 929 if (!di->bus.write) 930 return -EPERM; 931 932 ret = di->bus.write(di, di->regs[reg_index], value, single); 933 if (ret < 0) 934 dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n", 935 di->regs[reg_index], reg_index); 936 937 return ret; 938 } 939 940 static inline int bq27xxx_read_block(struct bq27xxx_device_info *di, int reg_index, 941 u8 *data, int len) 942 { 943 int ret; 944 945 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) 946 return -EINVAL; 947 948 if (!di->bus.read_bulk) 949 return -EPERM; 950 951 ret = di->bus.read_bulk(di, di->regs[reg_index], data, len); 952 if (ret < 0) 953 dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n", 954 di->regs[reg_index], reg_index); 955 956 return ret; 957 } 958 959 static inline int bq27xxx_write_block(struct bq27xxx_device_info *di, int reg_index, 960 u8 *data, int len) 961 { 962 int ret; 963 964 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) 965 return -EINVAL; 966 967 if (!di->bus.write_bulk) 968 return -EPERM; 969 970 ret = di->bus.write_bulk(di, di->regs[reg_index], data, len); 971 if (ret < 0) 972 dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n", 973 di->regs[reg_index], reg_index); 974 975 return ret; 976 } 977 978 static int bq27xxx_battery_seal(struct bq27xxx_device_info *di) 979 { 980 int ret; 981 982 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_SEALED, false); 983 if (ret < 0) { 984 dev_err(di->dev, "bus error on seal: %d\n", ret); 985 return ret; 986 } 987 988 return 0; 989 } 990 991 static int bq27xxx_battery_unseal(struct bq27xxx_device_info *di) 992 { 993 int ret; 994 995 if (di->unseal_key == 0) { 996 dev_err(di->dev, "unseal failed due to missing key\n"); 997 return -EINVAL; 998 } 999 1000 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false); 1001 if (ret < 0) 1002 goto out; 1003 1004 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false); 1005 if (ret < 0) 1006 goto out; 1007 1008 return 0; 1009 1010 out: 1011 dev_err(di->dev, "bus error on unseal: %d\n", ret); 1012 return ret; 1013 } 1014 1015 static u8 bq27xxx_battery_checksum_dm_block(struct bq27xxx_dm_buf *buf) 1016 { 1017 u16 sum = 0; 1018 int i; 1019 1020 for (i = 0; i < BQ27XXX_DM_SZ; i++) 1021 sum += buf->data[i]; 1022 sum &= 0xff; 1023 1024 return 0xff - sum; 1025 } 1026 1027 static int bq27xxx_battery_read_dm_block(struct bq27xxx_device_info *di, 1028 struct bq27xxx_dm_buf *buf) 1029 { 1030 int ret; 1031 1032 buf->has_data = false; 1033 1034 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true); 1035 if (ret < 0) 1036 goto out; 1037 1038 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true); 1039 if (ret < 0) 1040 goto out; 1041 1042 BQ27XXX_MSLEEP(1); 1043 1044 ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ); 1045 if (ret < 0) 1046 goto out; 1047 1048 ret = bq27xxx_read(di, BQ27XXX_DM_CKSUM, true); 1049 if (ret < 0) 1050 goto out; 1051 1052 if ((u8)ret != bq27xxx_battery_checksum_dm_block(buf)) { 1053 ret = -EINVAL; 1054 goto out; 1055 } 1056 1057 buf->has_data = true; 1058 buf->dirty = false; 1059 1060 return 0; 1061 1062 out: 1063 dev_err(di->dev, "bus error reading chip memory: %d\n", ret); 1064 return ret; 1065 } 1066 1067 static void bq27xxx_battery_update_dm_block(struct bq27xxx_device_info *di, 1068 struct bq27xxx_dm_buf *buf, 1069 enum bq27xxx_dm_reg_id reg_id, 1070 unsigned int val) 1071 { 1072 struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id]; 1073 const char *str = bq27xxx_dm_reg_name[reg_id]; 1074 u16 *prev = bq27xxx_dm_reg_ptr(buf, reg); 1075 1076 if (prev == NULL) { 1077 dev_warn(di->dev, "buffer does not match %s dm spec\n", str); 1078 return; 1079 } 1080 1081 if (reg->bytes != 2) { 1082 dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str); 1083 return; 1084 } 1085 1086 if (!buf->has_data) 1087 return; 1088 1089 if (be16_to_cpup(prev) == val) { 1090 dev_info(di->dev, "%s has %u\n", str, val); 1091 return; 1092 } 1093 1094 #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM 1095 if (!di->ram_chip && !bq27xxx_dt_to_nvm) { 1096 #else 1097 if (!di->ram_chip) { 1098 #endif 1099 /* devicetree and NVM differ; defer to NVM */ 1100 dev_warn(di->dev, "%s has %u; update to %u disallowed " 1101 #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM 1102 "by dt_monitored_battery_updates_nvm=0" 1103 #else 1104 "for flash/NVM data memory" 1105 #endif 1106 "\n", str, be16_to_cpup(prev), val); 1107 return; 1108 } 1109 1110 dev_info(di->dev, "update %s to %u\n", str, val); 1111 1112 *prev = cpu_to_be16(val); 1113 buf->dirty = true; 1114 } 1115 1116 static int bq27xxx_battery_cfgupdate_priv(struct bq27xxx_device_info *di, bool active) 1117 { 1118 const int limit = 100; 1119 u16 cmd = active ? BQ27XXX_SET_CFGUPDATE : BQ27XXX_SOFT_RESET; 1120 int ret, try = limit; 1121 1122 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, cmd, false); 1123 if (ret < 0) 1124 return ret; 1125 1126 do { 1127 BQ27XXX_MSLEEP(25); 1128 ret = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false); 1129 if (ret < 0) 1130 return ret; 1131 } while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try); 1132 1133 if (!try) { 1134 dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active); 1135 return -EINVAL; 1136 } 1137 1138 if (limit - try > 3) 1139 dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try); 1140 1141 return 0; 1142 } 1143 1144 static inline int bq27xxx_battery_set_cfgupdate(struct bq27xxx_device_info *di) 1145 { 1146 int ret = bq27xxx_battery_cfgupdate_priv(di, true); 1147 if (ret < 0 && ret != -EINVAL) 1148 dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret); 1149 1150 return ret; 1151 } 1152 1153 static inline int bq27xxx_battery_soft_reset(struct bq27xxx_device_info *di) 1154 { 1155 int ret = bq27xxx_battery_cfgupdate_priv(di, false); 1156 if (ret < 0 && ret != -EINVAL) 1157 dev_err(di->dev, "bus error on soft_reset: %d\n", ret); 1158 1159 return ret; 1160 } 1161 1162 static int bq27xxx_battery_write_dm_block(struct bq27xxx_device_info *di, 1163 struct bq27xxx_dm_buf *buf) 1164 { 1165 bool cfgup = di->chip == BQ27421; /* assume related chips need cfgupdate */ 1166 int ret; 1167 1168 if (!buf->dirty) 1169 return 0; 1170 1171 if (cfgup) { 1172 ret = bq27xxx_battery_set_cfgupdate(di); 1173 if (ret < 0) 1174 return ret; 1175 } 1176 1177 ret = bq27xxx_write(di, BQ27XXX_DM_CTRL, 0, true); 1178 if (ret < 0) 1179 goto out; 1180 1181 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true); 1182 if (ret < 0) 1183 goto out; 1184 1185 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true); 1186 if (ret < 0) 1187 goto out; 1188 1189 BQ27XXX_MSLEEP(1); 1190 1191 ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ); 1192 if (ret < 0) 1193 goto out; 1194 1195 ret = bq27xxx_write(di, BQ27XXX_DM_CKSUM, 1196 bq27xxx_battery_checksum_dm_block(buf), true); 1197 if (ret < 0) 1198 goto out; 1199 1200 /* DO NOT read BQ27XXX_DM_CKSUM here to verify it! That may cause NVM 1201 * corruption on the '425 chip (and perhaps others), which can damage 1202 * the chip. 1203 */ 1204 1205 if (cfgup) { 1206 BQ27XXX_MSLEEP(1); 1207 ret = bq27xxx_battery_soft_reset(di); 1208 if (ret < 0) 1209 return ret; 1210 } else { 1211 BQ27XXX_MSLEEP(100); /* flash DM updates in <100ms */ 1212 } 1213 1214 buf->dirty = false; 1215 1216 return 0; 1217 1218 out: 1219 if (cfgup) 1220 bq27xxx_battery_soft_reset(di); 1221 1222 dev_err(di->dev, "bus error writing chip memory: %d\n", ret); 1223 return ret; 1224 } 1225 1226 static void bq27xxx_battery_set_config(struct bq27xxx_device_info *di, 1227 struct power_supply_battery_info *info) 1228 { 1229 struct bq27xxx_dm_buf bd = BQ27XXX_DM_BUF(di, BQ27XXX_DM_DESIGN_CAPACITY); 1230 struct bq27xxx_dm_buf bt = BQ27XXX_DM_BUF(di, BQ27XXX_DM_TERMINATE_VOLTAGE); 1231 bool updated; 1232 1233 if (bq27xxx_battery_unseal(di) < 0) 1234 return; 1235 1236 if (info->charge_full_design_uah != -EINVAL && 1237 info->energy_full_design_uwh != -EINVAL) { 1238 bq27xxx_battery_read_dm_block(di, &bd); 1239 /* assume design energy & capacity are in same block */ 1240 bq27xxx_battery_update_dm_block(di, &bd, 1241 BQ27XXX_DM_DESIGN_CAPACITY, 1242 info->charge_full_design_uah / 1000); 1243 bq27xxx_battery_update_dm_block(di, &bd, 1244 BQ27XXX_DM_DESIGN_ENERGY, 1245 info->energy_full_design_uwh / 1000); 1246 } 1247 1248 if (info->voltage_min_design_uv != -EINVAL) { 1249 bool same = bd.class == bt.class && bd.block == bt.block; 1250 if (!same) 1251 bq27xxx_battery_read_dm_block(di, &bt); 1252 bq27xxx_battery_update_dm_block(di, same ? &bd : &bt, 1253 BQ27XXX_DM_TERMINATE_VOLTAGE, 1254 info->voltage_min_design_uv / 1000); 1255 } 1256 1257 updated = bd.dirty || bt.dirty; 1258 1259 bq27xxx_battery_write_dm_block(di, &bd); 1260 bq27xxx_battery_write_dm_block(di, &bt); 1261 1262 bq27xxx_battery_seal(di); 1263 1264 if (updated && di->chip != BQ27421) { /* not a cfgupdate chip, so reset */ 1265 bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_RESET, false); 1266 BQ27XXX_MSLEEP(300); /* reset time is not documented */ 1267 } 1268 /* assume bq27xxx_battery_update() is called hereafter */ 1269 } 1270 1271 static void bq27xxx_battery_settings(struct bq27xxx_device_info *di) 1272 { 1273 struct power_supply_battery_info info = {}; 1274 unsigned int min, max; 1275 1276 if (power_supply_get_battery_info(di->bat, &info) < 0) 1277 return; 1278 1279 if (!di->dm_regs) { 1280 dev_warn(di->dev, "data memory update not supported for chip\n"); 1281 return; 1282 } 1283 1284 if (info.energy_full_design_uwh != info.charge_full_design_uah) { 1285 if (info.energy_full_design_uwh == -EINVAL) 1286 dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n"); 1287 else if (info.charge_full_design_uah == -EINVAL) 1288 dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n"); 1289 } 1290 1291 /* assume min == 0 */ 1292 max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max; 1293 if (info.energy_full_design_uwh > max * 1000) { 1294 dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n", 1295 info.energy_full_design_uwh); 1296 info.energy_full_design_uwh = -EINVAL; 1297 } 1298 1299 /* assume min == 0 */ 1300 max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max; 1301 if (info.charge_full_design_uah > max * 1000) { 1302 dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n", 1303 info.charge_full_design_uah); 1304 info.charge_full_design_uah = -EINVAL; 1305 } 1306 1307 min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min; 1308 max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max; 1309 if ((info.voltage_min_design_uv < min * 1000 || 1310 info.voltage_min_design_uv > max * 1000) && 1311 info.voltage_min_design_uv != -EINVAL) { 1312 dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n", 1313 info.voltage_min_design_uv); 1314 info.voltage_min_design_uv = -EINVAL; 1315 } 1316 1317 if ((info.energy_full_design_uwh != -EINVAL && 1318 info.charge_full_design_uah != -EINVAL) || 1319 info.voltage_min_design_uv != -EINVAL) 1320 bq27xxx_battery_set_config(di, &info); 1321 } 1322 1323 /* 1324 * Return the battery State-of-Charge 1325 * Or < 0 if something fails. 1326 */ 1327 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di) 1328 { 1329 int soc; 1330 1331 if (di->chip == BQ27000 || di->chip == BQ27010) 1332 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true); 1333 else 1334 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false); 1335 1336 if (soc < 0) 1337 dev_dbg(di->dev, "error reading State-of-Charge\n"); 1338 1339 return soc; 1340 } 1341 1342 /* 1343 * Return a battery charge value in µAh 1344 * Or < 0 if something fails. 1345 */ 1346 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg) 1347 { 1348 int charge; 1349 1350 charge = bq27xxx_read(di, reg, false); 1351 if (charge < 0) { 1352 dev_dbg(di->dev, "error reading charge register %02x: %d\n", 1353 reg, charge); 1354 return charge; 1355 } 1356 1357 if (di->chip == BQ27000 || di->chip == BQ27010) 1358 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 1359 else 1360 charge *= 1000; 1361 1362 return charge; 1363 } 1364 1365 /* 1366 * Return the battery Nominal available capacity in µAh 1367 * Or < 0 if something fails. 1368 */ 1369 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di) 1370 { 1371 int flags; 1372 1373 if (di->chip == BQ27000 || di->chip == BQ27010) { 1374 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); 1375 if (flags >= 0 && (flags & BQ27000_FLAG_CI)) 1376 return -ENODATA; 1377 } 1378 1379 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC); 1380 } 1381 1382 /* 1383 * Return the battery Full Charge Capacity in µAh 1384 * Or < 0 if something fails. 1385 */ 1386 static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di) 1387 { 1388 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC); 1389 } 1390 1391 /* 1392 * Return the Design Capacity in µAh 1393 * Or < 0 if something fails. 1394 */ 1395 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di) 1396 { 1397 int dcap; 1398 1399 if (di->chip == BQ27000 || di->chip == BQ27010) 1400 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true); 1401 else 1402 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false); 1403 1404 if (dcap < 0) { 1405 dev_dbg(di->dev, "error reading initial last measured discharge\n"); 1406 return dcap; 1407 } 1408 1409 if (di->chip == BQ27000 || di->chip == BQ27010) 1410 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 1411 else 1412 dcap *= 1000; 1413 1414 return dcap; 1415 } 1416 1417 /* 1418 * Return the battery Available energy in µWh 1419 * Or < 0 if something fails. 1420 */ 1421 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di) 1422 { 1423 int ae; 1424 1425 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false); 1426 if (ae < 0) { 1427 dev_dbg(di->dev, "error reading available energy\n"); 1428 return ae; 1429 } 1430 1431 if (di->chip == BQ27000 || di->chip == BQ27010) 1432 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS; 1433 else 1434 ae *= 1000; 1435 1436 return ae; 1437 } 1438 1439 /* 1440 * Return the battery temperature in tenths of degree Kelvin 1441 * Or < 0 if something fails. 1442 */ 1443 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di) 1444 { 1445 int temp; 1446 1447 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false); 1448 if (temp < 0) { 1449 dev_err(di->dev, "error reading temperature\n"); 1450 return temp; 1451 } 1452 1453 if (di->chip == BQ27000 || di->chip == BQ27010) 1454 temp = 5 * temp / 2; 1455 1456 return temp; 1457 } 1458 1459 /* 1460 * Return the battery Cycle count total 1461 * Or < 0 if something fails. 1462 */ 1463 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di) 1464 { 1465 int cyct; 1466 1467 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false); 1468 if (cyct < 0) 1469 dev_err(di->dev, "error reading cycle count total\n"); 1470 1471 return cyct; 1472 } 1473 1474 /* 1475 * Read a time register. 1476 * Return < 0 if something fails. 1477 */ 1478 static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg) 1479 { 1480 int tval; 1481 1482 tval = bq27xxx_read(di, reg, false); 1483 if (tval < 0) { 1484 dev_dbg(di->dev, "error reading time register %02x: %d\n", 1485 reg, tval); 1486 return tval; 1487 } 1488 1489 if (tval == 65535) 1490 return -ENODATA; 1491 1492 return tval * 60; 1493 } 1494 1495 /* 1496 * Read an average power register. 1497 * Return < 0 if something fails. 1498 */ 1499 static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di) 1500 { 1501 int tval; 1502 1503 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false); 1504 if (tval < 0) { 1505 dev_err(di->dev, "error reading average power register %02x: %d\n", 1506 BQ27XXX_REG_AP, tval); 1507 return tval; 1508 } 1509 1510 if (di->chip == BQ27000 || di->chip == BQ27010) 1511 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS; 1512 else 1513 return tval; 1514 } 1515 1516 /* 1517 * Returns true if a battery over temperature condition is detected 1518 */ 1519 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags) 1520 { 1521 switch (di->chip) { 1522 case BQ2750X: 1523 case BQ2751X: 1524 case BQ27500: 1525 case BQ27510G1: 1526 case BQ27510G2: 1527 case BQ27510G3: 1528 case BQ27520G1: 1529 case BQ27520G2: 1530 case BQ27520G3: 1531 case BQ27520G4: 1532 case BQ27541: 1533 case BQ27545: 1534 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD); 1535 case BQ27530: 1536 case BQ27421: 1537 return flags & BQ27XXX_FLAG_OT; 1538 default: 1539 return false; 1540 } 1541 } 1542 1543 /* 1544 * Returns true if a battery under temperature condition is detected 1545 */ 1546 static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags) 1547 { 1548 if (di->chip == BQ27530 || di->chip == BQ27421) 1549 return flags & BQ27XXX_FLAG_UT; 1550 1551 return false; 1552 } 1553 1554 /* 1555 * Returns true if a low state of charge condition is detected 1556 */ 1557 static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags) 1558 { 1559 if (di->chip == BQ27000 || di->chip == BQ27010) 1560 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF); 1561 else 1562 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF); 1563 } 1564 1565 /* 1566 * Read flag register. 1567 * Return < 0 if something fails. 1568 */ 1569 static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di) 1570 { 1571 int flags; 1572 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; 1573 1574 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); 1575 if (flags < 0) { 1576 dev_err(di->dev, "error reading flag register:%d\n", flags); 1577 return flags; 1578 } 1579 1580 /* Unlikely but important to return first */ 1581 if (unlikely(bq27xxx_battery_overtemp(di, flags))) 1582 return POWER_SUPPLY_HEALTH_OVERHEAT; 1583 if (unlikely(bq27xxx_battery_undertemp(di, flags))) 1584 return POWER_SUPPLY_HEALTH_COLD; 1585 if (unlikely(bq27xxx_battery_dead(di, flags))) 1586 return POWER_SUPPLY_HEALTH_DEAD; 1587 1588 return POWER_SUPPLY_HEALTH_GOOD; 1589 } 1590 1591 void bq27xxx_battery_update(struct bq27xxx_device_info *di) 1592 { 1593 struct bq27xxx_reg_cache cache = {0, }; 1594 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010; 1595 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; 1596 1597 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); 1598 if ((cache.flags & 0xff) == 0xff) 1599 cache.flags = -1; /* read error */ 1600 if (cache.flags >= 0) { 1601 cache.temperature = bq27xxx_battery_read_temperature(di); 1602 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) { 1603 dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n"); 1604 cache.capacity = -ENODATA; 1605 cache.energy = -ENODATA; 1606 cache.time_to_empty = -ENODATA; 1607 cache.time_to_empty_avg = -ENODATA; 1608 cache.time_to_full = -ENODATA; 1609 cache.charge_full = -ENODATA; 1610 cache.health = -ENODATA; 1611 } else { 1612 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR) 1613 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE); 1614 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR) 1615 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP); 1616 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR) 1617 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF); 1618 cache.charge_full = bq27xxx_battery_read_fcc(di); 1619 cache.capacity = bq27xxx_battery_read_soc(di); 1620 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR) 1621 cache.energy = bq27xxx_battery_read_energy(di); 1622 cache.health = bq27xxx_battery_read_health(di); 1623 } 1624 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR) 1625 cache.cycle_count = bq27xxx_battery_read_cyct(di); 1626 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR) 1627 cache.power_avg = bq27xxx_battery_read_pwr_avg(di); 1628 1629 /* We only have to read charge design full once */ 1630 if (di->charge_design_full <= 0) 1631 di->charge_design_full = bq27xxx_battery_read_dcap(di); 1632 } 1633 1634 if (di->cache.capacity != cache.capacity) 1635 power_supply_changed(di->bat); 1636 1637 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) 1638 di->cache = cache; 1639 1640 di->last_update = jiffies; 1641 } 1642 EXPORT_SYMBOL_GPL(bq27xxx_battery_update); 1643 1644 static void bq27xxx_battery_poll(struct work_struct *work) 1645 { 1646 struct bq27xxx_device_info *di = 1647 container_of(work, struct bq27xxx_device_info, 1648 work.work); 1649 1650 bq27xxx_battery_update(di); 1651 1652 if (poll_interval > 0) 1653 schedule_delayed_work(&di->work, poll_interval * HZ); 1654 } 1655 1656 /* 1657 * Return the battery average current in µA 1658 * Note that current can be negative signed as well 1659 * Or 0 if something fails. 1660 */ 1661 static int bq27xxx_battery_current(struct bq27xxx_device_info *di, 1662 union power_supply_propval *val) 1663 { 1664 int curr; 1665 int flags; 1666 1667 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false); 1668 if (curr < 0) { 1669 dev_err(di->dev, "error reading current\n"); 1670 return curr; 1671 } 1672 1673 if (di->chip == BQ27000 || di->chip == BQ27010) { 1674 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); 1675 if (flags & BQ27000_FLAG_CHGS) { 1676 dev_dbg(di->dev, "negative current!\n"); 1677 curr = -curr; 1678 } 1679 1680 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; 1681 } else { 1682 /* Other gauges return signed value */ 1683 val->intval = (int)((s16)curr) * 1000; 1684 } 1685 1686 return 0; 1687 } 1688 1689 static int bq27xxx_battery_status(struct bq27xxx_device_info *di, 1690 union power_supply_propval *val) 1691 { 1692 int status; 1693 1694 if (di->chip == BQ27000 || di->chip == BQ27010) { 1695 if (di->cache.flags & BQ27000_FLAG_FC) 1696 status = POWER_SUPPLY_STATUS_FULL; 1697 else if (di->cache.flags & BQ27000_FLAG_CHGS) 1698 status = POWER_SUPPLY_STATUS_CHARGING; 1699 else if (power_supply_am_i_supplied(di->bat)) 1700 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1701 else 1702 status = POWER_SUPPLY_STATUS_DISCHARGING; 1703 } else { 1704 if (di->cache.flags & BQ27XXX_FLAG_FC) 1705 status = POWER_SUPPLY_STATUS_FULL; 1706 else if (di->cache.flags & BQ27XXX_FLAG_DSC) 1707 status = POWER_SUPPLY_STATUS_DISCHARGING; 1708 else 1709 status = POWER_SUPPLY_STATUS_CHARGING; 1710 } 1711 1712 val->intval = status; 1713 1714 return 0; 1715 } 1716 1717 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di, 1718 union power_supply_propval *val) 1719 { 1720 int level; 1721 1722 if (di->chip == BQ27000 || di->chip == BQ27010) { 1723 if (di->cache.flags & BQ27000_FLAG_FC) 1724 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1725 else if (di->cache.flags & BQ27000_FLAG_EDV1) 1726 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 1727 else if (di->cache.flags & BQ27000_FLAG_EDVF) 1728 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1729 else 1730 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 1731 } else { 1732 if (di->cache.flags & BQ27XXX_FLAG_FC) 1733 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1734 else if (di->cache.flags & BQ27XXX_FLAG_SOC1) 1735 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 1736 else if (di->cache.flags & BQ27XXX_FLAG_SOCF) 1737 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1738 else 1739 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 1740 } 1741 1742 val->intval = level; 1743 1744 return 0; 1745 } 1746 1747 /* 1748 * Return the battery Voltage in millivolts 1749 * Or < 0 if something fails. 1750 */ 1751 static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di, 1752 union power_supply_propval *val) 1753 { 1754 int volt; 1755 1756 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false); 1757 if (volt < 0) { 1758 dev_err(di->dev, "error reading voltage\n"); 1759 return volt; 1760 } 1761 1762 val->intval = volt * 1000; 1763 1764 return 0; 1765 } 1766 1767 static int bq27xxx_simple_value(int value, 1768 union power_supply_propval *val) 1769 { 1770 if (value < 0) 1771 return value; 1772 1773 val->intval = value; 1774 1775 return 0; 1776 } 1777 1778 static int bq27xxx_battery_get_property(struct power_supply *psy, 1779 enum power_supply_property psp, 1780 union power_supply_propval *val) 1781 { 1782 int ret = 0; 1783 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); 1784 1785 mutex_lock(&di->lock); 1786 if (time_is_before_jiffies(di->last_update + 5 * HZ)) { 1787 cancel_delayed_work_sync(&di->work); 1788 bq27xxx_battery_poll(&di->work.work); 1789 } 1790 mutex_unlock(&di->lock); 1791 1792 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) 1793 return -ENODEV; 1794 1795 switch (psp) { 1796 case POWER_SUPPLY_PROP_STATUS: 1797 ret = bq27xxx_battery_status(di, val); 1798 break; 1799 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1800 ret = bq27xxx_battery_voltage(di, val); 1801 break; 1802 case POWER_SUPPLY_PROP_PRESENT: 1803 val->intval = di->cache.flags < 0 ? 0 : 1; 1804 break; 1805 case POWER_SUPPLY_PROP_CURRENT_NOW: 1806 ret = bq27xxx_battery_current(di, val); 1807 break; 1808 case POWER_SUPPLY_PROP_CAPACITY: 1809 ret = bq27xxx_simple_value(di->cache.capacity, val); 1810 break; 1811 case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 1812 ret = bq27xxx_battery_capacity_level(di, val); 1813 break; 1814 case POWER_SUPPLY_PROP_TEMP: 1815 ret = bq27xxx_simple_value(di->cache.temperature, val); 1816 if (ret == 0) 1817 val->intval -= 2731; /* convert decidegree k to c */ 1818 break; 1819 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 1820 ret = bq27xxx_simple_value(di->cache.time_to_empty, val); 1821 break; 1822 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 1823 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val); 1824 break; 1825 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: 1826 ret = bq27xxx_simple_value(di->cache.time_to_full, val); 1827 break; 1828 case POWER_SUPPLY_PROP_TECHNOLOGY: 1829 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 1830 break; 1831 case POWER_SUPPLY_PROP_CHARGE_NOW: 1832 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val); 1833 break; 1834 case POWER_SUPPLY_PROP_CHARGE_FULL: 1835 ret = bq27xxx_simple_value(di->cache.charge_full, val); 1836 break; 1837 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 1838 ret = bq27xxx_simple_value(di->charge_design_full, val); 1839 break; 1840 /* 1841 * TODO: Implement these to make registers set from 1842 * power_supply_battery_info visible in sysfs. 1843 */ 1844 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 1845 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 1846 return -EINVAL; 1847 case POWER_SUPPLY_PROP_CYCLE_COUNT: 1848 ret = bq27xxx_simple_value(di->cache.cycle_count, val); 1849 break; 1850 case POWER_SUPPLY_PROP_ENERGY_NOW: 1851 ret = bq27xxx_simple_value(di->cache.energy, val); 1852 break; 1853 case POWER_SUPPLY_PROP_POWER_AVG: 1854 ret = bq27xxx_simple_value(di->cache.power_avg, val); 1855 break; 1856 case POWER_SUPPLY_PROP_HEALTH: 1857 ret = bq27xxx_simple_value(di->cache.health, val); 1858 break; 1859 case POWER_SUPPLY_PROP_MANUFACTURER: 1860 val->strval = BQ27XXX_MANUFACTURER; 1861 break; 1862 default: 1863 return -EINVAL; 1864 } 1865 1866 return ret; 1867 } 1868 1869 static void bq27xxx_external_power_changed(struct power_supply *psy) 1870 { 1871 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); 1872 1873 cancel_delayed_work_sync(&di->work); 1874 schedule_delayed_work(&di->work, 0); 1875 } 1876 1877 int bq27xxx_battery_setup(struct bq27xxx_device_info *di) 1878 { 1879 struct power_supply_desc *psy_desc; 1880 struct power_supply_config psy_cfg = { 1881 .of_node = di->dev->of_node, 1882 .drv_data = di, 1883 }; 1884 1885 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll); 1886 mutex_init(&di->lock); 1887 di->regs = bq27xxx_regs[di->chip]; 1888 1889 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL); 1890 if (!psy_desc) 1891 return -ENOMEM; 1892 1893 psy_desc->name = di->name; 1894 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; 1895 psy_desc->properties = bq27xxx_battery_props[di->chip].props; 1896 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size; 1897 psy_desc->get_property = bq27xxx_battery_get_property; 1898 psy_desc->external_power_changed = bq27xxx_external_power_changed; 1899 1900 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg); 1901 if (IS_ERR(di->bat)) { 1902 dev_err(di->dev, "failed to register battery\n"); 1903 return PTR_ERR(di->bat); 1904 } 1905 1906 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); 1907 1908 bq27xxx_battery_settings(di); 1909 bq27xxx_battery_update(di); 1910 1911 mutex_lock(&bq27xxx_list_lock); 1912 list_add(&di->list, &bq27xxx_battery_devices); 1913 mutex_unlock(&bq27xxx_list_lock); 1914 1915 return 0; 1916 } 1917 EXPORT_SYMBOL_GPL(bq27xxx_battery_setup); 1918 1919 void bq27xxx_battery_teardown(struct bq27xxx_device_info *di) 1920 { 1921 /* 1922 * power_supply_unregister call bq27xxx_battery_get_property which 1923 * call bq27xxx_battery_poll. 1924 * Make sure that bq27xxx_battery_poll will not call 1925 * schedule_delayed_work again after unregister (which cause OOPS). 1926 */ 1927 poll_interval = 0; 1928 1929 cancel_delayed_work_sync(&di->work); 1930 1931 power_supply_unregister(di->bat); 1932 1933 mutex_lock(&bq27xxx_list_lock); 1934 list_del(&di->list); 1935 mutex_unlock(&bq27xxx_list_lock); 1936 1937 mutex_destroy(&di->lock); 1938 } 1939 EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown); 1940 1941 static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg, 1942 bool single) 1943 { 1944 struct device *dev = di->dev; 1945 struct bq27xxx_platform_data *pdata = dev->platform_data; 1946 unsigned int timeout = 3; 1947 int upper, lower; 1948 int temp; 1949 1950 if (!single) { 1951 /* Make sure the value has not changed in between reading the 1952 * lower and the upper part */ 1953 upper = pdata->read(dev, reg + 1); 1954 do { 1955 temp = upper; 1956 if (upper < 0) 1957 return upper; 1958 1959 lower = pdata->read(dev, reg); 1960 if (lower < 0) 1961 return lower; 1962 1963 upper = pdata->read(dev, reg + 1); 1964 } while (temp != upper && --timeout); 1965 1966 if (timeout == 0) 1967 return -EIO; 1968 1969 return (upper << 8) | lower; 1970 } 1971 1972 return pdata->read(dev, reg); 1973 } 1974 1975 static int bq27xxx_battery_platform_probe(struct platform_device *pdev) 1976 { 1977 struct bq27xxx_device_info *di; 1978 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data; 1979 1980 if (!pdata) { 1981 dev_err(&pdev->dev, "no platform_data supplied\n"); 1982 return -EINVAL; 1983 } 1984 1985 if (!pdata->read) { 1986 dev_err(&pdev->dev, "no hdq read callback supplied\n"); 1987 return -EINVAL; 1988 } 1989 1990 if (!pdata->chip) { 1991 dev_err(&pdev->dev, "no device supplied\n"); 1992 return -EINVAL; 1993 } 1994 1995 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); 1996 if (!di) 1997 return -ENOMEM; 1998 1999 platform_set_drvdata(pdev, di); 2000 2001 di->dev = &pdev->dev; 2002 di->chip = pdata->chip; 2003 di->name = pdata->name ?: dev_name(&pdev->dev); 2004 di->bus.read = bq27xxx_battery_platform_read; 2005 2006 return bq27xxx_battery_setup(di); 2007 } 2008 2009 static int bq27xxx_battery_platform_remove(struct platform_device *pdev) 2010 { 2011 struct bq27xxx_device_info *di = platform_get_drvdata(pdev); 2012 2013 bq27xxx_battery_teardown(di); 2014 2015 return 0; 2016 } 2017 2018 static const struct platform_device_id bq27xxx_battery_platform_id_table[] = { 2019 { "bq27000-battery", }, 2020 { /* sentinel */ } 2021 }; 2022 MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table); 2023 2024 #ifdef CONFIG_OF 2025 static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = { 2026 { .compatible = "ti,bq27000" }, 2027 {}, 2028 }; 2029 MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table); 2030 #endif 2031 2032 static struct platform_driver bq27xxx_battery_platform_driver = { 2033 .probe = bq27xxx_battery_platform_probe, 2034 .remove = bq27xxx_battery_platform_remove, 2035 .driver = { 2036 .name = "bq27000-battery", 2037 .of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table), 2038 }, 2039 .id_table = bq27xxx_battery_platform_id_table, 2040 }; 2041 module_platform_driver(bq27xxx_battery_platform_driver); 2042 2043 MODULE_ALIAS("platform:bq27000-battery"); 2044 2045 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); 2046 MODULE_DESCRIPTION("BQ27xxx battery monitor driver"); 2047 MODULE_LICENSE("GPL"); 2048