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