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 |
| 25 | * http://www.ti.com/product/bq27510-g3 |
| 26 | * http://www.ti.com/product/bq27520-g4 |
| 27 | * http://www.ti.com/product/bq27530-g1 |
| 28 | * http://www.ti.com/product/bq27531-g1 |
| 29 | * http://www.ti.com/product/bq27541-g1 |
| 30 | * http://www.ti.com/product/bq27542-g1 |
| 31 | * http://www.ti.com/product/bq27546-g1 |
| 32 | * http://www.ti.com/product/bq27742-g1 |
| 33 | * http://www.ti.com/product/bq27545-g1 |
| 34 | * http://www.ti.com/product/bq27421-g1 |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 35 | * http://www.ti.com/product/bq27425-g1 |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 36 | * http://www.ti.com/product/bq27411-g1 |
| 37 | * http://www.ti.com/product/bq27621-g1 |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 38 | */ |
| 39 | |
Himangi Saraogi | 1cb82fd | 2014-05-28 22:58:49 +0530 | [diff] [blame] | 40 | #include <linux/device.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/param.h> |
| 43 | #include <linux/jiffies.h> |
| 44 | #include <linux/workqueue.h> |
| 45 | #include <linux/delay.h> |
| 46 | #include <linux/platform_device.h> |
| 47 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 48 | #include <linux/slab.h> |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 49 | #include <linux/of.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 50 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 51 | #include <linux/power/bq27xxx_battery.h> |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 52 | |
Andrew F. Davis | 6eb207f | 2015-07-22 16:51:54 -0500 | [diff] [blame] | 53 | #define DRIVER_VERSION "1.2.0" |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 54 | |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 55 | #define BQ27XXX_MANUFACTURER "Texas Instruments" |
| 56 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 57 | /* BQ27XXX Flags */ |
| 58 | #define BQ27XXX_FLAG_DSC BIT(0) |
| 59 | #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */ |
| 60 | #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */ |
| 61 | #define BQ27XXX_FLAG_FC BIT(9) |
| 62 | #define BQ27XXX_FLAG_OTD BIT(14) |
| 63 | #define BQ27XXX_FLAG_OTC BIT(15) |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 64 | #define BQ27XXX_FLAG_UT BIT(14) |
| 65 | #define BQ27XXX_FLAG_OT BIT(15) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 66 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 67 | /* BQ27000 has different layout for Flags register */ |
Andrew F. Davis | 6eb207f | 2015-07-22 16:51:54 -0500 | [diff] [blame] | 68 | #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */ |
| 69 | #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */ |
| 70 | #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */ |
| 71 | #define BQ27000_FLAG_FC BIT(5) |
| 72 | #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */ |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 73 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 74 | #define BQ27XXX_RS (20) /* Resistor sense mOhm */ |
| 75 | #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */ |
| 76 | #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */ |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 77 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 78 | #define INVALID_REG_ADDR 0xff |
| 79 | |
| 80 | /* |
| 81 | * bq27xxx_reg_index - Register names |
| 82 | * |
| 83 | * These are indexes into a device's register mapping array. |
| 84 | */ |
| 85 | enum bq27xxx_reg_index { |
| 86 | BQ27XXX_REG_CTRL = 0, /* Control */ |
| 87 | BQ27XXX_REG_TEMP, /* Temperature */ |
| 88 | BQ27XXX_REG_INT_TEMP, /* Internal Temperature */ |
| 89 | BQ27XXX_REG_VOLT, /* Voltage */ |
| 90 | BQ27XXX_REG_AI, /* Average Current */ |
| 91 | BQ27XXX_REG_FLAGS, /* Flags */ |
| 92 | BQ27XXX_REG_TTE, /* Time-to-Empty */ |
| 93 | BQ27XXX_REG_TTF, /* Time-to-Full */ |
| 94 | BQ27XXX_REG_TTES, /* Time-to-Empty Standby */ |
| 95 | BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */ |
| 96 | BQ27XXX_REG_NAC, /* Nominal Available Capacity */ |
| 97 | BQ27XXX_REG_FCC, /* Full Charge Capacity */ |
| 98 | BQ27XXX_REG_CYCT, /* Cycle Count */ |
| 99 | BQ27XXX_REG_AE, /* Available Energy */ |
| 100 | BQ27XXX_REG_SOC, /* State-of-Charge */ |
| 101 | BQ27XXX_REG_DCAP, /* Design Capacity */ |
| 102 | BQ27XXX_REG_AP, /* Average Power */ |
| 103 | }; |
| 104 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 105 | /* Register mappings */ |
| 106 | static u8 bq27000_regs[] = { |
| 107 | 0x00, /* CONTROL */ |
| 108 | 0x06, /* TEMP */ |
| 109 | INVALID_REG_ADDR, /* INT TEMP - NA*/ |
| 110 | 0x08, /* VOLT */ |
| 111 | 0x14, /* AVG CURR */ |
| 112 | 0x0a, /* FLAGS */ |
| 113 | 0x16, /* TTE */ |
| 114 | 0x18, /* TTF */ |
| 115 | 0x1c, /* TTES */ |
| 116 | 0x26, /* TTECP */ |
| 117 | 0x0c, /* NAC */ |
| 118 | 0x12, /* LMD(FCC) */ |
| 119 | 0x2a, /* CYCT */ |
| 120 | 0x22, /* AE */ |
| 121 | 0x0b, /* SOC(RSOC) */ |
| 122 | 0x76, /* DCAP(ILMD) */ |
| 123 | 0x24, /* AP */ |
| 124 | }; |
| 125 | |
| 126 | static u8 bq27010_regs[] = { |
| 127 | 0x00, /* CONTROL */ |
| 128 | 0x06, /* TEMP */ |
| 129 | INVALID_REG_ADDR, /* INT TEMP - NA*/ |
| 130 | 0x08, /* VOLT */ |
| 131 | 0x14, /* AVG CURR */ |
| 132 | 0x0a, /* FLAGS */ |
| 133 | 0x16, /* TTE */ |
| 134 | 0x18, /* TTF */ |
| 135 | 0x1c, /* TTES */ |
| 136 | 0x26, /* TTECP */ |
| 137 | 0x0c, /* NAC */ |
| 138 | 0x12, /* LMD(FCC) */ |
| 139 | 0x2a, /* CYCT */ |
| 140 | INVALID_REG_ADDR, /* AE - NA */ |
| 141 | 0x0b, /* SOC(RSOC) */ |
| 142 | 0x76, /* DCAP(ILMD) */ |
| 143 | INVALID_REG_ADDR, /* AP - NA */ |
| 144 | }; |
| 145 | |
| 146 | static u8 bq27500_regs[] = { |
| 147 | 0x00, /* CONTROL */ |
| 148 | 0x06, /* TEMP */ |
| 149 | 0x28, /* INT TEMP */ |
| 150 | 0x08, /* VOLT */ |
| 151 | 0x14, /* AVG CURR */ |
| 152 | 0x0a, /* FLAGS */ |
| 153 | 0x16, /* TTE */ |
| 154 | INVALID_REG_ADDR, /* TTF - NA */ |
| 155 | 0x1a, /* TTES */ |
| 156 | INVALID_REG_ADDR, /* TTECP - NA */ |
| 157 | 0x0c, /* NAC */ |
| 158 | 0x12, /* LMD(FCC) */ |
H. Nikolaus Schaller | 099867a | 2015-12-17 11:12:54 +0100 | [diff] [blame] | 159 | 0x2a, /* CYCT */ |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 160 | INVALID_REG_ADDR, /* AE - NA */ |
H. Nikolaus Schaller | 099867a | 2015-12-17 11:12:54 +0100 | [diff] [blame] | 161 | 0x2c, /* SOC(RSOC) */ |
| 162 | 0x3c, /* DCAP(ILMD) */ |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 163 | INVALID_REG_ADDR, /* AP - NA */ |
| 164 | }; |
| 165 | |
| 166 | static u8 bq27530_regs[] = { |
| 167 | 0x00, /* CONTROL */ |
| 168 | 0x06, /* TEMP */ |
| 169 | 0x32, /* INT TEMP */ |
| 170 | 0x08, /* VOLT */ |
| 171 | 0x14, /* AVG CURR */ |
| 172 | 0x0a, /* FLAGS */ |
| 173 | 0x16, /* TTE */ |
| 174 | INVALID_REG_ADDR, /* TTF - NA */ |
| 175 | INVALID_REG_ADDR, /* TTES - NA */ |
| 176 | INVALID_REG_ADDR, /* TTECP - NA */ |
| 177 | 0x0c, /* NAC */ |
| 178 | 0x12, /* LMD(FCC) */ |
| 179 | 0x2a, /* CYCT */ |
| 180 | INVALID_REG_ADDR, /* AE - NA */ |
| 181 | 0x2c, /* SOC(RSOC) */ |
| 182 | INVALID_REG_ADDR, /* DCAP - NA */ |
| 183 | 0x24, /* AP */ |
| 184 | }; |
| 185 | |
| 186 | static u8 bq27541_regs[] = { |
| 187 | 0x00, /* CONTROL */ |
| 188 | 0x06, /* TEMP */ |
| 189 | 0x28, /* INT TEMP */ |
| 190 | 0x08, /* VOLT */ |
| 191 | 0x14, /* AVG CURR */ |
| 192 | 0x0a, /* FLAGS */ |
| 193 | 0x16, /* TTE */ |
| 194 | INVALID_REG_ADDR, /* TTF - NA */ |
| 195 | INVALID_REG_ADDR, /* TTES - NA */ |
| 196 | INVALID_REG_ADDR, /* TTECP - NA */ |
| 197 | 0x0c, /* NAC */ |
| 198 | 0x12, /* LMD(FCC) */ |
| 199 | 0x2a, /* CYCT */ |
| 200 | INVALID_REG_ADDR, /* AE - NA */ |
| 201 | 0x2c, /* SOC(RSOC) */ |
| 202 | 0x3c, /* DCAP */ |
Liu Xiang | 265b604 | 2016-01-09 22:10:39 +0800 | [diff] [blame] | 203 | 0x24, /* AP */ |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | static u8 bq27545_regs[] = { |
| 207 | 0x00, /* CONTROL */ |
| 208 | 0x06, /* TEMP */ |
| 209 | 0x28, /* INT TEMP */ |
| 210 | 0x08, /* VOLT */ |
| 211 | 0x14, /* AVG CURR */ |
| 212 | 0x0a, /* FLAGS */ |
| 213 | 0x16, /* TTE */ |
| 214 | INVALID_REG_ADDR, /* TTF - NA */ |
| 215 | INVALID_REG_ADDR, /* TTES - NA */ |
| 216 | INVALID_REG_ADDR, /* TTECP - NA */ |
| 217 | 0x0c, /* NAC */ |
| 218 | 0x12, /* LMD(FCC) */ |
| 219 | 0x2a, /* CYCT */ |
| 220 | INVALID_REG_ADDR, /* AE - NA */ |
| 221 | 0x2c, /* SOC(RSOC) */ |
| 222 | INVALID_REG_ADDR, /* DCAP - NA */ |
| 223 | 0x24, /* AP */ |
| 224 | }; |
| 225 | |
| 226 | static u8 bq27421_regs[] = { |
| 227 | 0x00, /* CONTROL */ |
| 228 | 0x02, /* TEMP */ |
| 229 | 0x1e, /* INT TEMP */ |
| 230 | 0x04, /* VOLT */ |
| 231 | 0x10, /* AVG CURR */ |
| 232 | 0x06, /* FLAGS */ |
| 233 | INVALID_REG_ADDR, /* TTE - NA */ |
| 234 | INVALID_REG_ADDR, /* TTF - NA */ |
| 235 | INVALID_REG_ADDR, /* TTES - NA */ |
| 236 | INVALID_REG_ADDR, /* TTECP - NA */ |
| 237 | 0x08, /* NAC */ |
| 238 | 0x0e, /* FCC */ |
| 239 | INVALID_REG_ADDR, /* CYCT - NA */ |
| 240 | INVALID_REG_ADDR, /* AE - NA */ |
| 241 | 0x1c, /* SOC */ |
| 242 | 0x3c, /* DCAP */ |
| 243 | 0x18, /* AP */ |
| 244 | }; |
| 245 | |
| 246 | static u8 *bq27xxx_regs[] = { |
| 247 | [BQ27000] = bq27000_regs, |
| 248 | [BQ27010] = bq27010_regs, |
| 249 | [BQ27500] = bq27500_regs, |
| 250 | [BQ27530] = bq27530_regs, |
| 251 | [BQ27541] = bq27541_regs, |
| 252 | [BQ27545] = bq27545_regs, |
| 253 | [BQ27421] = bq27421_regs, |
| 254 | }; |
| 255 | |
| 256 | static enum power_supply_property bq27000_battery_props[] = { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 257 | POWER_SUPPLY_PROP_STATUS, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 258 | POWER_SUPPLY_PROP_PRESENT, |
| 259 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 260 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 261 | POWER_SUPPLY_PROP_CAPACITY, |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 262 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 263 | POWER_SUPPLY_PROP_TEMP, |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 264 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 265 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 266 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 267 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 268 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 269 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 270 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 271 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 272 | POWER_SUPPLY_PROP_ENERGY_NOW, |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 273 | POWER_SUPPLY_PROP_POWER_AVG, |
| 274 | POWER_SUPPLY_PROP_HEALTH, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 275 | POWER_SUPPLY_PROP_MANUFACTURER, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 276 | }; |
| 277 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 278 | static enum power_supply_property bq27010_battery_props[] = { |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 279 | POWER_SUPPLY_PROP_STATUS, |
| 280 | POWER_SUPPLY_PROP_PRESENT, |
| 281 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 282 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 283 | POWER_SUPPLY_PROP_CAPACITY, |
| 284 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 285 | POWER_SUPPLY_PROP_TEMP, |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 286 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 287 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 288 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 289 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 290 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 291 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 292 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 293 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 294 | POWER_SUPPLY_PROP_HEALTH, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 295 | POWER_SUPPLY_PROP_MANUFACTURER, |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 298 | static enum power_supply_property bq27500_battery_props[] = { |
| 299 | POWER_SUPPLY_PROP_STATUS, |
| 300 | POWER_SUPPLY_PROP_PRESENT, |
| 301 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 302 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 303 | POWER_SUPPLY_PROP_CAPACITY, |
| 304 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 305 | POWER_SUPPLY_PROP_TEMP, |
| 306 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 307 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 308 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 309 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 310 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 311 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 312 | POWER_SUPPLY_PROP_HEALTH, |
| 313 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 314 | }; |
| 315 | |
| 316 | static enum power_supply_property bq27530_battery_props[] = { |
| 317 | POWER_SUPPLY_PROP_STATUS, |
| 318 | POWER_SUPPLY_PROP_PRESENT, |
| 319 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 320 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 321 | POWER_SUPPLY_PROP_CAPACITY, |
| 322 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 323 | POWER_SUPPLY_PROP_TEMP, |
| 324 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 325 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 326 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 327 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 328 | POWER_SUPPLY_PROP_POWER_AVG, |
| 329 | POWER_SUPPLY_PROP_HEALTH, |
| 330 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 331 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 332 | }; |
| 333 | |
| 334 | static enum power_supply_property bq27541_battery_props[] = { |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 335 | POWER_SUPPLY_PROP_STATUS, |
| 336 | POWER_SUPPLY_PROP_PRESENT, |
| 337 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 338 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 339 | POWER_SUPPLY_PROP_CAPACITY, |
| 340 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 341 | POWER_SUPPLY_PROP_TEMP, |
| 342 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 343 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 344 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 345 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 346 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 347 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 348 | POWER_SUPPLY_PROP_POWER_AVG, |
| 349 | POWER_SUPPLY_PROP_HEALTH, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 350 | POWER_SUPPLY_PROP_MANUFACTURER, |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 351 | }; |
| 352 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 353 | static enum power_supply_property bq27545_battery_props[] = { |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 354 | POWER_SUPPLY_PROP_STATUS, |
| 355 | POWER_SUPPLY_PROP_PRESENT, |
| 356 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 357 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 358 | POWER_SUPPLY_PROP_CAPACITY, |
| 359 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 360 | POWER_SUPPLY_PROP_TEMP, |
| 361 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 362 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 363 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 364 | POWER_SUPPLY_PROP_CHARGE_NOW, |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 365 | POWER_SUPPLY_PROP_HEALTH, |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 366 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 367 | POWER_SUPPLY_PROP_POWER_AVG, |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 368 | POWER_SUPPLY_PROP_MANUFACTURER, |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 369 | }; |
| 370 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 371 | static enum power_supply_property bq27421_battery_props[] = { |
| 372 | POWER_SUPPLY_PROP_STATUS, |
| 373 | POWER_SUPPLY_PROP_PRESENT, |
| 374 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 375 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 376 | POWER_SUPPLY_PROP_CAPACITY, |
| 377 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 378 | POWER_SUPPLY_PROP_TEMP, |
| 379 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 380 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 381 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 382 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 383 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 384 | }; |
| 385 | |
| 386 | #define BQ27XXX_PROP(_id, _prop) \ |
| 387 | [_id] = { \ |
| 388 | .props = _prop, \ |
| 389 | .size = ARRAY_SIZE(_prop), \ |
| 390 | } |
| 391 | |
| 392 | static struct { |
| 393 | enum power_supply_property *props; |
| 394 | size_t size; |
| 395 | } bq27xxx_battery_props[] = { |
| 396 | BQ27XXX_PROP(BQ27000, bq27000_battery_props), |
| 397 | BQ27XXX_PROP(BQ27010, bq27010_battery_props), |
| 398 | BQ27XXX_PROP(BQ27500, bq27500_battery_props), |
| 399 | BQ27XXX_PROP(BQ27530, bq27530_battery_props), |
| 400 | BQ27XXX_PROP(BQ27541, bq27541_battery_props), |
| 401 | BQ27XXX_PROP(BQ27545, bq27545_battery_props), |
| 402 | BQ27XXX_PROP(BQ27421, bq27421_battery_props), |
| 403 | }; |
| 404 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 405 | static unsigned int poll_interval = 360; |
| 406 | module_param(poll_interval, uint, 0644); |
Andrew F. Davis | 6d0a181 | 2015-07-22 16:51:56 -0500 | [diff] [blame] | 407 | MODULE_PARM_DESC(poll_interval, |
| 408 | "battery poll interval in seconds - 0 disables polling"); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 409 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 410 | /* |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 411 | * Common code for BQ27xxx devices |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 412 | */ |
| 413 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 414 | 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] | 415 | bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 416 | { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 417 | /* Reports EINVAL for invalid/missing registers */ |
| 418 | if (!di || di->regs[reg_index] == INVALID_REG_ADDR) |
| 419 | return -EINVAL; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 420 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 421 | return di->bus.read(di, di->regs[reg_index], single); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 425 | * Return the battery State-of-Charge |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 426 | * Or < 0 if something fails. |
| 427 | */ |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 428 | static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 429 | { |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 430 | int soc; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 431 | |
H. Nikolaus Schaller | 549d7b3 | 2015-12-17 11:12:53 +0100 | [diff] [blame] | 432 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 433 | soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true); |
| 434 | else |
| 435 | soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 436 | |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 437 | if (soc < 0) |
| 438 | dev_dbg(di->dev, "error reading State-of-Charge\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 439 | |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 440 | return soc; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 444 | * Return a battery charge value in µAh |
| 445 | * Or < 0 if something fails. |
| 446 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 447 | 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] | 448 | { |
| 449 | int charge; |
| 450 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 451 | charge = bq27xxx_read(di, reg, false); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 452 | if (charge < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 453 | dev_dbg(di->dev, "error reading charge register %02x: %d\n", |
| 454 | reg, charge); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 455 | return charge; |
| 456 | } |
| 457 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 458 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 459 | charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 460 | else |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 461 | charge *= 1000; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 462 | |
| 463 | return charge; |
| 464 | } |
| 465 | |
| 466 | /* |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 467 | * Return the battery Nominal available capacity in µAh |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 468 | * Or < 0 if something fails. |
| 469 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 470 | 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] | 471 | { |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 472 | int flags; |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 473 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 474 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
| 475 | flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true); |
| 476 | if (flags >= 0 && (flags & BQ27000_FLAG_CI)) |
| 477 | return -ENODATA; |
Andrew F. Davis | 6d0a181 | 2015-07-22 16:51:56 -0500 | [diff] [blame] | 478 | } |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 479 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 480 | return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Return the battery Full Charge Capacity in µAh |
| 485 | * Or < 0 if something fails. |
| 486 | */ |
| 487 | static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di) |
| 488 | { |
| 489 | return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC); |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Return the Design Capacity in µAh |
| 494 | * Or < 0 if something fails. |
| 495 | */ |
| 496 | static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di) |
| 497 | { |
| 498 | int dcap; |
| 499 | |
H. Nikolaus Schaller | 549d7b3 | 2015-12-17 11:12:53 +0100 | [diff] [blame] | 500 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 501 | dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true); |
| 502 | else |
| 503 | dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 504 | |
| 505 | if (dcap < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 506 | dev_dbg(di->dev, "error reading initial last measured discharge\n"); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 507 | return dcap; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 508 | } |
| 509 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 510 | if (di->chip == BQ27000 || di->chip == BQ27010) |
H. Nikolaus Schaller | 549d7b3 | 2015-12-17 11:12:53 +0100 | [diff] [blame] | 511 | dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 512 | else |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 513 | dcap *= 1000; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 514 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 515 | return dcap; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 519 | * Return the battery Available energy in µWh |
| 520 | * Or < 0 if something fails. |
| 521 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 522 | static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di) |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 523 | { |
| 524 | int ae; |
| 525 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 526 | ae = bq27xxx_read(di, BQ27XXX_REG_AE, false); |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 527 | if (ae < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 528 | dev_dbg(di->dev, "error reading available energy\n"); |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 529 | return ae; |
| 530 | } |
| 531 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 532 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 533 | ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 534 | else |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 535 | ae *= 1000; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 536 | |
| 537 | return ae; |
| 538 | } |
| 539 | |
| 540 | /* |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 541 | * Return the battery temperature in tenths of degree Kelvin |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 542 | * Or < 0 if something fails. |
| 543 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 544 | static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di) |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 545 | { |
| 546 | int temp; |
| 547 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 548 | temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false); |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 549 | if (temp < 0) { |
| 550 | dev_err(di->dev, "error reading temperature\n"); |
| 551 | return temp; |
| 552 | } |
| 553 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 554 | if (di->chip == BQ27000 || di->chip == BQ27010) |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 555 | temp = 5 * temp / 2; |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 556 | |
| 557 | return temp; |
| 558 | } |
| 559 | |
| 560 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 561 | * Return the battery Cycle count total |
| 562 | * Or < 0 if something fails. |
| 563 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 564 | static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di) |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 565 | { |
| 566 | int cyct; |
| 567 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 568 | cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 569 | if (cyct < 0) |
| 570 | dev_err(di->dev, "error reading cycle count total\n"); |
| 571 | |
| 572 | return cyct; |
| 573 | } |
| 574 | |
| 575 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 576 | * Read a time register. |
| 577 | * Return < 0 if something fails. |
| 578 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 579 | 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] | 580 | { |
| 581 | int tval; |
| 582 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 583 | tval = bq27xxx_read(di, reg, false); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 584 | if (tval < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 585 | dev_dbg(di->dev, "error reading time register %02x: %d\n", |
| 586 | reg, tval); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 587 | return tval; |
| 588 | } |
| 589 | |
| 590 | if (tval == 65535) |
| 591 | return -ENODATA; |
| 592 | |
| 593 | return tval * 60; |
| 594 | } |
| 595 | |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 596 | /* |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 597 | * Read an average power register. |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 598 | * Return < 0 if something fails. |
| 599 | */ |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 600 | static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di) |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 601 | { |
| 602 | int tval; |
| 603 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 604 | tval = bq27xxx_read(di, BQ27XXX_REG_AP, false); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 605 | if (tval < 0) { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 606 | dev_err(di->dev, "error reading average power register %02x: %d\n", |
| 607 | BQ27XXX_REG_AP, tval); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 608 | return tval; |
| 609 | } |
| 610 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 611 | if (di->chip == BQ27000 || di->chip == BQ27010) |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 612 | return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 613 | else |
| 614 | return tval; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Returns true if a battery over temperature condition is detected |
| 619 | */ |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 620 | 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] | 621 | { |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 622 | if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 623 | return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD); |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 624 | if (di->chip == BQ27530 || di->chip == BQ27421) |
| 625 | return flags & BQ27XXX_FLAG_OT; |
| 626 | |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Returns true if a battery under temperature condition is detected |
| 632 | */ |
| 633 | static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags) |
| 634 | { |
| 635 | if (di->chip == BQ27530 || di->chip == BQ27421) |
| 636 | return flags & BQ27XXX_FLAG_UT; |
| 637 | |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Returns true if a low state of charge condition is detected |
| 643 | */ |
| 644 | static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags) |
| 645 | { |
| 646 | if (di->chip == BQ27000 || di->chip == BQ27010) |
| 647 | return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 648 | else |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 649 | return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Read flag register. |
| 654 | * Return < 0 if something fails. |
| 655 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 656 | static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di) |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 657 | { |
Andrzej Hajda | 2edd69a | 2015-09-28 10:51:27 +0200 | [diff] [blame] | 658 | int flags; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 659 | |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 660 | flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false); |
| 661 | if (flags < 0) { |
| 662 | dev_err(di->dev, "error reading flag register:%d\n", flags); |
| 663 | return flags; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 664 | } |
| 665 | |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 666 | /* Unlikely but important to return first */ |
| 667 | if (unlikely(bq27xxx_battery_overtemp(di, flags))) |
| 668 | return POWER_SUPPLY_HEALTH_OVERHEAT; |
| 669 | if (unlikely(bq27xxx_battery_undertemp(di, flags))) |
| 670 | return POWER_SUPPLY_HEALTH_COLD; |
| 671 | if (unlikely(bq27xxx_battery_dead(di, flags))) |
| 672 | return POWER_SUPPLY_HEALTH_DEAD; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 673 | |
Andrew F. Davis | 74aab84 | 2015-09-22 14:35:10 -0500 | [diff] [blame] | 674 | return POWER_SUPPLY_HEALTH_GOOD; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 675 | } |
| 676 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 677 | void bq27xxx_battery_update(struct bq27xxx_device_info *di) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 678 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 679 | struct bq27xxx_reg_cache cache = {0, }; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 680 | bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010; |
| 681 | bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 682 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 683 | cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag); |
Marek Belisko | 3dd843e | 2014-06-30 21:49:56 +0200 | [diff] [blame] | 684 | if ((cache.flags & 0xff) == 0xff) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 685 | cache.flags = -1; /* read error */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 686 | if (cache.flags >= 0) { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 687 | cache.temperature = bq27xxx_battery_read_temperature(di); |
| 688 | if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) { |
NeilBrown | 1f94b25 | 2015-10-07 20:42:38 +0200 | [diff] [blame] | 689 | 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] | 690 | cache.capacity = -ENODATA; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 691 | cache.energy = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 692 | cache.time_to_empty = -ENODATA; |
| 693 | cache.time_to_empty_avg = -ENODATA; |
| 694 | cache.time_to_full = -ENODATA; |
| 695 | cache.charge_full = -ENODATA; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 696 | cache.health = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 697 | } else { |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 698 | if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR) |
| 699 | cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE); |
| 700 | if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR) |
| 701 | cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP); |
| 702 | if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR) |
| 703 | cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF); |
| 704 | cache.charge_full = bq27xxx_battery_read_fcc(di); |
Andrew F. Davis | c570903 | 2015-09-22 14:35:08 -0500 | [diff] [blame] | 705 | cache.capacity = bq27xxx_battery_read_soc(di); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 706 | if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR) |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 707 | cache.energy = bq27xxx_battery_read_energy(di); |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 708 | cache.health = bq27xxx_battery_read_health(di); |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 709 | } |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 710 | if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR) |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 711 | cache.cycle_count = bq27xxx_battery_read_cyct(di); |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 712 | if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR) |
| 713 | cache.power_avg = bq27xxx_battery_read_pwr_avg(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 714 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 715 | /* We only have to read charge design full once */ |
| 716 | if (di->charge_design_full <= 0) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 717 | di->charge_design_full = bq27xxx_battery_read_dcap(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Puthikorn Voravootivat | 90f04a2 | 2014-10-21 18:15:37 -0700 | [diff] [blame] | 720 | if (di->cache.capacity != cache.capacity) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 721 | power_supply_changed(di->bat); |
Puthikorn Voravootivat | 90f04a2 | 2014-10-21 18:15:37 -0700 | [diff] [blame] | 722 | |
| 723 | if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) |
| 724 | di->cache = cache; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 725 | |
| 726 | di->last_update = jiffies; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 727 | } |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 728 | EXPORT_SYMBOL_GPL(bq27xxx_battery_update); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 729 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 730 | static void bq27xxx_battery_poll(struct work_struct *work) |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 731 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 732 | struct bq27xxx_device_info *di = |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 733 | container_of(work, struct bq27xxx_device_info, |
| 734 | work.work); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 735 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 736 | bq27xxx_battery_update(di); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 737 | |
| 738 | if (poll_interval > 0) { |
| 739 | /* The timer does not have to be accurate. */ |
| 740 | set_timer_slack(&di->work.timer, poll_interval * HZ / 4); |
| 741 | schedule_delayed_work(&di->work, poll_interval * HZ); |
| 742 | } |
| 743 | } |
| 744 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 745 | /* |
Pali Rohár | bf7d414 | 2011-02-01 00:23:13 +0100 | [diff] [blame] | 746 | * Return the battery average current in µA |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 747 | * Note that current can be negative signed as well |
| 748 | * Or 0 if something fails. |
| 749 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 750 | static int bq27xxx_battery_current(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 751 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 752 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 753 | int curr; |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 754 | int flags; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 755 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 756 | curr = bq27xxx_read(di, BQ27XXX_REG_AI, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 757 | if (curr < 0) { |
| 758 | dev_err(di->dev, "error reading current\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 759 | return curr; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 760 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 761 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 762 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
| 763 | flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false); |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 764 | if (flags & BQ27000_FLAG_CHGS) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 765 | dev_dbg(di->dev, "negative current!\n"); |
Grazvydas Ignotas | afbc74fd | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 766 | curr = -curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 767 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 768 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 769 | val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS; |
| 770 | } else { |
| 771 | /* Other gauges return signed value */ |
| 772 | val->intval = (int)((s16)curr) * 1000; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 773 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 774 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 775 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 778 | static int bq27xxx_battery_status(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 779 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 780 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 781 | int status; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 782 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 783 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 784 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 785 | status = POWER_SUPPLY_STATUS_FULL; |
| 786 | else if (di->cache.flags & BQ27000_FLAG_CHGS) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 787 | status = POWER_SUPPLY_STATUS_CHARGING; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 788 | else if (power_supply_am_i_supplied(di->bat)) |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 789 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 790 | else |
| 791 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 792 | } else { |
| 793 | if (di->cache.flags & BQ27XXX_FLAG_FC) |
| 794 | status = POWER_SUPPLY_STATUS_FULL; |
| 795 | else if (di->cache.flags & BQ27XXX_FLAG_DSC) |
| 796 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 797 | else |
| 798 | status = POWER_SUPPLY_STATUS_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | val->intval = status; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 802 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 803 | return 0; |
| 804 | } |
| 805 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 806 | static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 807 | union power_supply_propval *val) |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 808 | { |
| 809 | int level; |
| 810 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 811 | if (di->chip == BQ27000 || di->chip == BQ27010) { |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 812 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 813 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 814 | else if (di->cache.flags & BQ27000_FLAG_EDV1) |
| 815 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 816 | else if (di->cache.flags & BQ27000_FLAG_EDVF) |
| 817 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 818 | else |
| 819 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 820 | } else { |
| 821 | if (di->cache.flags & BQ27XXX_FLAG_FC) |
| 822 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 823 | else if (di->cache.flags & BQ27XXX_FLAG_SOC1) |
| 824 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 825 | else if (di->cache.flags & BQ27XXX_FLAG_SOCF) |
| 826 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 827 | else |
| 828 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | val->intval = level; |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 836 | /* |
Thomas Weber | 91fe4d5 | 2012-02-17 17:46:21 +0100 | [diff] [blame] | 837 | * Return the battery Voltage in millivolts |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 838 | * Or < 0 if something fails. |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 839 | */ |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 840 | static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 841 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 842 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 843 | int volt; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 844 | |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 845 | volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 846 | if (volt < 0) { |
| 847 | dev_err(di->dev, "error reading voltage\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 848 | return volt; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 849 | } |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 850 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 851 | val->intval = volt * 1000; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 852 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 856 | static int bq27xxx_simple_value(int value, |
Andrew F. Davis | ce33edf | 2015-07-22 16:51:55 -0500 | [diff] [blame] | 857 | union power_supply_propval *val) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 858 | { |
| 859 | if (value < 0) |
| 860 | return value; |
| 861 | |
| 862 | val->intval = value; |
| 863 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 867 | static int bq27xxx_battery_get_property(struct power_supply *psy, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 868 | enum power_supply_property psp, |
| 869 | union power_supply_propval *val) |
| 870 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 871 | int ret = 0; |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 872 | struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 873 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 874 | mutex_lock(&di->lock); |
| 875 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) { |
| 876 | cancel_delayed_work_sync(&di->work); |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 877 | bq27xxx_battery_poll(&di->work.work); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 878 | } |
| 879 | mutex_unlock(&di->lock); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 880 | |
| 881 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 882 | return -ENODEV; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 883 | |
| 884 | switch (psp) { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 885 | case POWER_SUPPLY_PROP_STATUS: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 886 | ret = bq27xxx_battery_status(di, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 887 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 888 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 889 | ret = bq27xxx_battery_voltage(di, val); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 890 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 891 | case POWER_SUPPLY_PROP_PRESENT: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 892 | val->intval = di->cache.flags < 0 ? 0 : 1; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 893 | break; |
| 894 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 895 | ret = bq27xxx_battery_current(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 896 | break; |
| 897 | case POWER_SUPPLY_PROP_CAPACITY: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 898 | ret = bq27xxx_simple_value(di->cache.capacity, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 899 | break; |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 900 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 901 | ret = bq27xxx_battery_capacity_level(di, val); |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 902 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 903 | case POWER_SUPPLY_PROP_TEMP: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 904 | ret = bq27xxx_simple_value(di->cache.temperature, val); |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 905 | if (ret == 0) |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 906 | val->intval -= 2731; /* convert decidegree k to c */ |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 907 | break; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 908 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 909 | ret = bq27xxx_simple_value(di->cache.time_to_empty, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 910 | break; |
| 911 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 912 | ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 913 | break; |
| 914 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 915 | ret = bq27xxx_simple_value(di->cache.time_to_full, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 916 | break; |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 917 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 918 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 919 | break; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 920 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 921 | ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 922 | break; |
| 923 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 924 | ret = bq27xxx_simple_value(di->cache.charge_full, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 925 | break; |
| 926 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 927 | ret = bq27xxx_simple_value(di->charge_design_full, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 928 | break; |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 929 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 930 | ret = bq27xxx_simple_value(di->cache.cycle_count, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 931 | break; |
| 932 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 933 | ret = bq27xxx_simple_value(di->cache.energy, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 934 | break; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 935 | case POWER_SUPPLY_PROP_POWER_AVG: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 936 | ret = bq27xxx_simple_value(di->cache.power_avg, val); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 937 | break; |
| 938 | case POWER_SUPPLY_PROP_HEALTH: |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 939 | ret = bq27xxx_simple_value(di->cache.health, val); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 940 | break; |
Andrew F. Davis | db04fc5 | 2015-07-22 16:51:53 -0500 | [diff] [blame] | 941 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 942 | val->strval = BQ27XXX_MANUFACTURER; |
| 943 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 944 | default: |
| 945 | return -EINVAL; |
| 946 | } |
| 947 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 948 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 951 | static void bq27xxx_external_power_changed(struct power_supply *psy) |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 952 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 953 | struct bq27xxx_device_info *di = power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 954 | |
| 955 | cancel_delayed_work_sync(&di->work); |
| 956 | schedule_delayed_work(&di->work, 0); |
| 957 | } |
| 958 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 959 | int bq27xxx_battery_setup(struct bq27xxx_device_info *di) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 960 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 961 | struct power_supply_desc *psy_desc; |
| 962 | struct power_supply_config psy_cfg = { .drv_data = di, }; |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 963 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 964 | INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll); |
| 965 | mutex_init(&di->lock); |
| 966 | di->regs = bq27xxx_regs[di->chip]; |
| 967 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 968 | psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL); |
| 969 | if (!psy_desc) |
| 970 | return -ENOMEM; |
| 971 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 972 | psy_desc->name = di->name; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 973 | psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; |
Andrew F. Davis | d74534c | 2015-09-22 14:35:09 -0500 | [diff] [blame] | 974 | psy_desc->properties = bq27xxx_battery_props[di->chip].props; |
| 975 | psy_desc->num_properties = bq27xxx_battery_props[di->chip].size; |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 976 | psy_desc->get_property = bq27xxx_battery_get_property; |
| 977 | psy_desc->external_power_changed = bq27xxx_external_power_changed; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 978 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 979 | di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg); |
| 980 | if (IS_ERR(di->bat)) { |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 981 | dev_err(di->dev, "failed to register battery\n"); |
| 982 | return PTR_ERR(di->bat); |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 986 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 987 | bq27xxx_battery_update(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 988 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 989 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 990 | } |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 991 | EXPORT_SYMBOL_GPL(bq27xxx_battery_setup); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 992 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 993 | void bq27xxx_battery_teardown(struct bq27xxx_device_info *di) |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 994 | { |
Pali Rohár | 8cfaaa8 | 2011-11-01 01:43:11 +0100 | [diff] [blame] | 995 | /* |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 996 | * power_supply_unregister call bq27xxx_battery_get_property which |
| 997 | * call bq27xxx_battery_poll. |
| 998 | * Make sure that bq27xxx_battery_poll will not call |
Pali Rohár | 8cfaaa8 | 2011-11-01 01:43:11 +0100 | [diff] [blame] | 999 | * schedule_delayed_work again after unregister (which cause OOPS). |
| 1000 | */ |
| 1001 | poll_interval = 0; |
| 1002 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1003 | cancel_delayed_work_sync(&di->work); |
| 1004 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1005 | power_supply_unregister(di->bat); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1006 | |
| 1007 | mutex_destroy(&di->lock); |
| 1008 | } |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1009 | EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1010 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1011 | static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg, |
| 1012 | bool single) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1013 | { |
| 1014 | struct device *dev = di->dev; |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1015 | struct bq27xxx_platform_data *pdata = dev->platform_data; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1016 | unsigned int timeout = 3; |
| 1017 | int upper, lower; |
| 1018 | int temp; |
| 1019 | |
| 1020 | if (!single) { |
| 1021 | /* Make sure the value has not changed in between reading the |
| 1022 | * lower and the upper part */ |
| 1023 | upper = pdata->read(dev, reg + 1); |
| 1024 | do { |
| 1025 | temp = upper; |
| 1026 | if (upper < 0) |
| 1027 | return upper; |
| 1028 | |
| 1029 | lower = pdata->read(dev, reg); |
| 1030 | if (lower < 0) |
| 1031 | return lower; |
| 1032 | |
| 1033 | upper = pdata->read(dev, reg + 1); |
| 1034 | } while (temp != upper && --timeout); |
| 1035 | |
| 1036 | if (timeout == 0) |
| 1037 | return -EIO; |
| 1038 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1039 | return (upper << 8) | lower; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1040 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1041 | |
| 1042 | return pdata->read(dev, reg); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1045 | static int bq27xxx_battery_platform_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1046 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1047 | struct bq27xxx_device_info *di; |
| 1048 | struct bq27xxx_platform_data *pdata = pdev->dev.platform_data; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1049 | |
| 1050 | if (!pdata) { |
| 1051 | dev_err(&pdev->dev, "no platform_data supplied\n"); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | |
| 1055 | if (!pdata->read) { |
| 1056 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); |
| 1057 | return -EINVAL; |
| 1058 | } |
| 1059 | |
Andrew F. Davis | 424cfde | 2015-09-22 14:35:07 -0500 | [diff] [blame] | 1060 | if (!pdata->chip) { |
| 1061 | dev_err(&pdev->dev, "no device supplied\n"); |
| 1062 | return -EINVAL; |
| 1063 | } |
| 1064 | |
Himangi Saraogi | 1cb82fd | 2014-05-28 22:58:49 +0530 | [diff] [blame] | 1065 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
Andrew F. Davis | 6d0a181 | 2015-07-22 16:51:56 -0500 | [diff] [blame] | 1066 | if (!di) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1067 | return -ENOMEM; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1068 | |
| 1069 | platform_set_drvdata(pdev, di); |
| 1070 | |
| 1071 | di->dev = &pdev->dev; |
Andrew F. Davis | 424cfde | 2015-09-22 14:35:07 -0500 | [diff] [blame] | 1072 | di->chip = pdata->chip; |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1073 | di->name = pdata->name ?: dev_name(&pdev->dev); |
| 1074 | di->bus.read = bq27xxx_battery_platform_read; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1075 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1076 | return bq27xxx_battery_setup(di); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1079 | static int bq27xxx_battery_platform_remove(struct platform_device *pdev) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1080 | { |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1081 | struct bq27xxx_device_info *di = platform_get_drvdata(pdev); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1082 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1083 | bq27xxx_battery_teardown(di); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1084 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1088 | static const struct platform_device_id bq27xxx_battery_platform_id_table[] = { |
| 1089 | { "bq27000-battery", }, |
| 1090 | { /* sentinel */ } |
| 1091 | }; |
| 1092 | MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table); |
| 1093 | |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 1094 | #ifdef CONFIG_OF |
| 1095 | static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = { |
| 1096 | { .compatible = "ti,bq27000" }, |
| 1097 | {}, |
| 1098 | }; |
| 1099 | MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table); |
| 1100 | #endif |
| 1101 | |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1102 | static struct platform_driver bq27xxx_battery_platform_driver = { |
| 1103 | .probe = bq27xxx_battery_platform_probe, |
| 1104 | .remove = bq27xxx_battery_platform_remove, |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1105 | .driver = { |
| 1106 | .name = "bq27000-battery", |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 1107 | .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] | 1108 | }, |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1109 | .id_table = bq27xxx_battery_platform_id_table, |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1110 | }; |
Andrew F. Davis | 703df6c | 2015-11-23 10:53:51 -0600 | [diff] [blame] | 1111 | module_platform_driver(bq27xxx_battery_platform_driver); |
Marek Belisko | 8ebb7e9 | 2015-04-30 22:05:58 +0200 | [diff] [blame] | 1112 | |
Pali Rohár | 7df3a74 | 2016-02-21 12:28:22 +0100 | [diff] [blame] | 1113 | MODULE_ALIAS("platform:bq27000-battery"); |
| 1114 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1115 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
Andrew F. Davis | 081bab2 | 2015-09-22 14:35:06 -0500 | [diff] [blame] | 1116 | MODULE_DESCRIPTION("BQ27xxx battery monitor driver"); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1117 | MODULE_LICENSE("GPL"); |