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