Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * BQ27x00 battery driver |
| 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: |
| 20 | * http://focus.ti.com/docs/prod/folders/print/bq27000.html |
| 21 | * http://focus.ti.com/docs/prod/folders/print/bq27500.html |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 22 | * http://www.ti.com/product/bq27425-g1 |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 23 | * http://www.ti.com/product/BQ27742-G1 |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 24 | * http://www.ti.com/product/BQ27510-G3 |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
Himangi Saraogi | 1cb82fd | 2014-05-28 22:58:49 +0530 | [diff] [blame] | 27 | #include <linux/device.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/param.h> |
| 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/workqueue.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/platform_device.h> |
| 34 | #include <linux/power_supply.h> |
| 35 | #include <linux/idr.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 36 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Harvey Harrison | 8aef7e8 | 2008-09-22 14:53:50 -0700 | [diff] [blame] | 38 | #include <asm/unaligned.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 39 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 40 | #include <linux/power/bq27x00_battery.h> |
| 41 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 42 | #define DRIVER_VERSION "1.2.0" |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 43 | |
| 44 | #define BQ27x00_REG_TEMP 0x06 |
| 45 | #define BQ27x00_REG_VOLT 0x08 |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 46 | #define BQ27x00_REG_AI 0x14 |
| 47 | #define BQ27x00_REG_FLAGS 0x0A |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 48 | #define BQ27x00_REG_TTE 0x16 |
| 49 | #define BQ27x00_REG_TTF 0x18 |
| 50 | #define BQ27x00_REG_TTECP 0x26 |
Thomas Weber | 202e011 | 2012-02-16 08:58:15 +0100 | [diff] [blame] | 51 | #define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */ |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 52 | #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */ |
| 53 | #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */ |
Thomas Weber | 202e011 | 2012-02-16 08:58:15 +0100 | [diff] [blame] | 54 | #define BQ27x00_REG_AE 0x22 /* Available energy */ |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 55 | #define BQ27x00_POWER_AVG 0x24 |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 56 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 57 | #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 58 | #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */ |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 59 | #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */ |
| 60 | #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */ |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 61 | #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */ |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 62 | #define BQ27000_FLAG_FC BIT(5) |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 63 | #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */ |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 64 | |
Pali Rohár | bf7d414 | 2011-02-01 00:23:13 +0100 | [diff] [blame] | 65 | #define BQ27500_REG_SOC 0x2C |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 66 | #define BQ27500_REG_DCAP 0x3C /* Design capacity */ |
Anton Vorontsov | b7aaacf | 2012-02-01 21:20:15 +0400 | [diff] [blame] | 67 | #define BQ27500_FLAG_DSC BIT(0) |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 68 | #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */ |
| 69 | #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */ |
Anton Vorontsov | b7aaacf | 2012-02-01 21:20:15 +0400 | [diff] [blame] | 70 | #define BQ27500_FLAG_FC BIT(9) |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 71 | #define BQ27500_FLAG_OTC BIT(15) |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 72 | |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 73 | #define BQ27742_POWER_AVG 0x76 |
| 74 | |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 75 | #define BQ27510_REG_SOC 0x20 |
| 76 | #define BQ27510_REG_DCAP 0x2E /* Design capacity */ |
| 77 | #define BQ27510_REG_CYCT 0x1E /* Cycle count total */ |
| 78 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 79 | /* bq27425 register addresses are same as bq27x00 addresses minus 4 */ |
| 80 | #define BQ27425_REG_OFFSET 0x04 |
Eric Bénard | 9dbf5a2 | 2014-11-12 23:04:08 +0100 | [diff] [blame] | 81 | #define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET) |
| 82 | #define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET) |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 83 | |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 84 | #define BQ27000_RS 20 /* Resistor sense */ |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 85 | #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000) |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 86 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 87 | struct bq27x00_device_info; |
| 88 | struct bq27x00_access_methods { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 89 | int (*read)(struct bq27x00_device_info *di, u8 reg, bool single); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 92 | enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742, BQ27510}; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 93 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 94 | struct bq27x00_reg_cache { |
| 95 | int temperature; |
| 96 | int time_to_empty; |
| 97 | int time_to_empty_avg; |
| 98 | int time_to_full; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 99 | int charge_full; |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 100 | int cycle_count; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 101 | int capacity; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 102 | int energy; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 103 | int flags; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 104 | int power_avg; |
| 105 | int health; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 108 | struct bq27x00_device_info { |
| 109 | struct device *dev; |
| 110 | int id; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 111 | enum bq27x00_chip chip; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 112 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 113 | struct bq27x00_reg_cache cache; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 114 | int charge_design_full; |
| 115 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 116 | unsigned long last_update; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 117 | struct delayed_work work; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 118 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 119 | struct power_supply *bat; |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 120 | |
| 121 | struct bq27x00_access_methods bus; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 122 | |
| 123 | struct mutex lock; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static enum power_supply_property bq27x00_battery_props[] = { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 127 | POWER_SUPPLY_PROP_STATUS, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 128 | POWER_SUPPLY_PROP_PRESENT, |
| 129 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 130 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 131 | POWER_SUPPLY_PROP_CAPACITY, |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 132 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 133 | POWER_SUPPLY_PROP_TEMP, |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 134 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 135 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 136 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 137 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 138 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 139 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 140 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 141 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 142 | POWER_SUPPLY_PROP_ENERGY_NOW, |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 143 | POWER_SUPPLY_PROP_POWER_AVG, |
| 144 | POWER_SUPPLY_PROP_HEALTH, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 147 | static enum power_supply_property bq27425_battery_props[] = { |
| 148 | POWER_SUPPLY_PROP_STATUS, |
| 149 | POWER_SUPPLY_PROP_PRESENT, |
| 150 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 151 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 152 | POWER_SUPPLY_PROP_CAPACITY, |
| 153 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 154 | POWER_SUPPLY_PROP_TEMP, |
| 155 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 156 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 157 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 158 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 159 | }; |
| 160 | |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 161 | static enum power_supply_property bq27742_battery_props[] = { |
| 162 | POWER_SUPPLY_PROP_STATUS, |
| 163 | POWER_SUPPLY_PROP_PRESENT, |
| 164 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 165 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 166 | POWER_SUPPLY_PROP_CAPACITY, |
| 167 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 168 | POWER_SUPPLY_PROP_TEMP, |
| 169 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 170 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 171 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 172 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 173 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 174 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 175 | POWER_SUPPLY_PROP_POWER_AVG, |
| 176 | POWER_SUPPLY_PROP_HEALTH, |
| 177 | }; |
| 178 | |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 179 | static enum power_supply_property bq27510_battery_props[] = { |
| 180 | POWER_SUPPLY_PROP_STATUS, |
| 181 | POWER_SUPPLY_PROP_PRESENT, |
| 182 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 183 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 184 | POWER_SUPPLY_PROP_CAPACITY, |
| 185 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 186 | POWER_SUPPLY_PROP_TEMP, |
| 187 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 188 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 189 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 190 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 191 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 192 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 193 | POWER_SUPPLY_PROP_POWER_AVG, |
| 194 | POWER_SUPPLY_PROP_HEALTH, |
| 195 | }; |
| 196 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 197 | static unsigned int poll_interval = 360; |
| 198 | module_param(poll_interval, uint, 0644); |
| 199 | MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \ |
| 200 | "0 disables polling"); |
| 201 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 202 | /* |
| 203 | * Common code for BQ27x00 devices |
| 204 | */ |
| 205 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 206 | static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 207 | bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 208 | { |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 209 | if (di->chip == BQ27425) |
| 210 | return di->bus.read(di, reg - BQ27425_REG_OFFSET, single); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 211 | return di->bus.read(di, reg, single); |
| 212 | } |
| 213 | |
| 214 | /* |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 215 | * Higher versions of the chip like BQ27425 and BQ27500 |
| 216 | * differ from BQ27000 and BQ27200 in calculation of certain |
| 217 | * parameters. Hence we need to check for the chip type. |
| 218 | */ |
| 219 | static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di) |
| 220 | { |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 221 | if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742 |
| 222 | || di->chip == BQ27510) |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 223 | return true; |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 228 | * Return the battery Relative State-of-Charge |
| 229 | * Or < 0 if something fails. |
| 230 | */ |
| 231 | static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di) |
| 232 | { |
| 233 | int rsoc; |
| 234 | |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 235 | if (di->chip == BQ27500 || di->chip == BQ27742) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 236 | rsoc = bq27x00_read(di, BQ27500_REG_SOC, false); |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 237 | else if (di->chip == BQ27510) |
| 238 | rsoc = bq27x00_read(di, BQ27510_REG_SOC, false); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 239 | else if (di->chip == BQ27425) |
| 240 | rsoc = bq27x00_read(di, BQ27425_REG_SOC, false); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 241 | else |
| 242 | rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true); |
| 243 | |
| 244 | if (rsoc < 0) |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 245 | dev_dbg(di->dev, "error reading relative State-of-Charge\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 246 | |
| 247 | return rsoc; |
| 248 | } |
| 249 | |
| 250 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 251 | * Return a battery charge value in µAh |
| 252 | * Or < 0 if something fails. |
| 253 | */ |
| 254 | static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg) |
| 255 | { |
| 256 | int charge; |
| 257 | |
| 258 | charge = bq27x00_read(di, reg, false); |
| 259 | if (charge < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 260 | dev_dbg(di->dev, "error reading charge register %02x: %d\n", |
| 261 | reg, charge); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 262 | return charge; |
| 263 | } |
| 264 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 265 | if (bq27xxx_is_chip_version_higher(di)) |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 266 | charge *= 1000; |
| 267 | else |
| 268 | charge = charge * 3570 / BQ27000_RS; |
| 269 | |
| 270 | return charge; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Return the battery Nominal available capaciy in µAh |
| 275 | * Or < 0 if something fails. |
| 276 | */ |
| 277 | static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di) |
| 278 | { |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 279 | int flags; |
| 280 | bool is_bq27500 = di->chip == BQ27500; |
Puthikorn Voravootivat | a3c0c3e | 2014-09-30 11:18:56 -0700 | [diff] [blame] | 281 | bool is_bq27742 = di->chip == BQ27742; |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 282 | bool is_higher = bq27xxx_is_chip_version_higher(di); |
Puthikorn Voravootivat | a3c0c3e | 2014-09-30 11:18:56 -0700 | [diff] [blame] | 283 | bool flags_1b = !(is_bq27500 || is_bq27742); |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 284 | |
Puthikorn Voravootivat | a3c0c3e | 2014-09-30 11:18:56 -0700 | [diff] [blame] | 285 | flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b); |
Pali Rohár | e59ec4a | 2012-11-10 15:42:48 +0100 | [diff] [blame] | 286 | if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI)) |
| 287 | return -ENODATA; |
| 288 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 289 | return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Return the battery Last measured discharge in µAh |
| 294 | * Or < 0 if something fails. |
| 295 | */ |
| 296 | static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di) |
| 297 | { |
| 298 | return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD); |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Return the battery Initial last measured discharge in µAh |
| 303 | * Or < 0 if something fails. |
| 304 | */ |
| 305 | static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di) |
| 306 | { |
| 307 | int ilmd; |
| 308 | |
Eric Bénard | 9dbf5a2 | 2014-11-12 23:04:08 +0100 | [diff] [blame] | 309 | if (bq27xxx_is_chip_version_higher(di)) { |
| 310 | if (di->chip == BQ27425) |
| 311 | ilmd = bq27x00_read(di, BQ27425_REG_DCAP, false); |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 312 | else if (di->chip == BQ27510) |
| 313 | ilmd = bq27x00_read(di, BQ27510_REG_DCAP, false); |
Eric Bénard | 9dbf5a2 | 2014-11-12 23:04:08 +0100 | [diff] [blame] | 314 | else |
| 315 | ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false); |
| 316 | } else |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 317 | ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true); |
| 318 | |
| 319 | if (ilmd < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 320 | dev_dbg(di->dev, "error reading initial last measured discharge\n"); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 321 | return ilmd; |
| 322 | } |
| 323 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 324 | if (bq27xxx_is_chip_version_higher(di)) |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 325 | ilmd *= 1000; |
| 326 | else |
| 327 | ilmd = ilmd * 256 * 3570 / BQ27000_RS; |
| 328 | |
| 329 | return ilmd; |
| 330 | } |
| 331 | |
| 332 | /* |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 333 | * Return the battery Available energy in µWh |
| 334 | * Or < 0 if something fails. |
| 335 | */ |
| 336 | static int bq27x00_battery_read_energy(struct bq27x00_device_info *di) |
| 337 | { |
| 338 | int ae; |
| 339 | |
| 340 | ae = bq27x00_read(di, BQ27x00_REG_AE, false); |
| 341 | if (ae < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 342 | dev_dbg(di->dev, "error reading available energy\n"); |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 343 | return ae; |
| 344 | } |
| 345 | |
| 346 | if (di->chip == BQ27500) |
| 347 | ae *= 1000; |
| 348 | else |
| 349 | ae = ae * 29200 / BQ27000_RS; |
| 350 | |
| 351 | return ae; |
| 352 | } |
| 353 | |
| 354 | /* |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 355 | * Return the battery temperature in tenths of degree Kelvin |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 356 | * Or < 0 if something fails. |
| 357 | */ |
| 358 | static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di) |
| 359 | { |
| 360 | int temp; |
| 361 | |
| 362 | temp = bq27x00_read(di, BQ27x00_REG_TEMP, false); |
| 363 | if (temp < 0) { |
| 364 | dev_err(di->dev, "error reading temperature\n"); |
| 365 | return temp; |
| 366 | } |
| 367 | |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 368 | if (!bq27xxx_is_chip_version_higher(di)) |
| 369 | temp = 5 * temp / 2; |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 370 | |
| 371 | return temp; |
| 372 | } |
| 373 | |
| 374 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 375 | * Return the battery Cycle count total |
| 376 | * Or < 0 if something fails. |
| 377 | */ |
| 378 | static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di) |
| 379 | { |
| 380 | int cyct; |
| 381 | |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 382 | if (di->chip == BQ27510) |
| 383 | cyct = bq27x00_read(di, BQ27510_REG_CYCT, false); |
| 384 | else |
| 385 | cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 386 | if (cyct < 0) |
| 387 | dev_err(di->dev, "error reading cycle count total\n"); |
| 388 | |
| 389 | return cyct; |
| 390 | } |
| 391 | |
| 392 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 393 | * Read a time register. |
| 394 | * Return < 0 if something fails. |
| 395 | */ |
| 396 | static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg) |
| 397 | { |
| 398 | int tval; |
| 399 | |
| 400 | tval = bq27x00_read(di, reg, false); |
| 401 | if (tval < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 402 | dev_dbg(di->dev, "error reading time register %02x: %d\n", |
| 403 | reg, tval); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 404 | return tval; |
| 405 | } |
| 406 | |
| 407 | if (tval == 65535) |
| 408 | return -ENODATA; |
| 409 | |
| 410 | return tval * 60; |
| 411 | } |
| 412 | |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 413 | /* |
| 414 | * Read a power avg register. |
| 415 | * Return < 0 if something fails. |
| 416 | */ |
| 417 | static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg) |
| 418 | { |
| 419 | int tval; |
| 420 | |
| 421 | tval = bq27x00_read(di, reg, false); |
| 422 | if (tval < 0) { |
| 423 | dev_err(di->dev, "error reading power avg rgister %02x: %d\n", |
| 424 | reg, tval); |
| 425 | return tval; |
| 426 | } |
| 427 | |
| 428 | if (di->chip == BQ27500) |
| 429 | return tval; |
| 430 | else |
| 431 | return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Read flag register. |
| 436 | * Return < 0 if something fails. |
| 437 | */ |
| 438 | static int bq27x00_battery_read_health(struct bq27x00_device_info *di) |
| 439 | { |
| 440 | int tval; |
| 441 | |
| 442 | tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false); |
| 443 | if (tval < 0) { |
| 444 | dev_err(di->dev, "error reading flag register:%d\n", tval); |
| 445 | return tval; |
| 446 | } |
| 447 | |
| 448 | if ((di->chip == BQ27500)) { |
| 449 | if (tval & BQ27500_FLAG_SOCF) |
| 450 | tval = POWER_SUPPLY_HEALTH_DEAD; |
| 451 | else if (tval & BQ27500_FLAG_OTC) |
| 452 | tval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 453 | else |
| 454 | tval = POWER_SUPPLY_HEALTH_GOOD; |
| 455 | return tval; |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 456 | } else if (di->chip == BQ27510) { |
| 457 | if (tval & BQ27500_FLAG_OTC) |
| 458 | return POWER_SUPPLY_HEALTH_OVERHEAT; |
| 459 | return POWER_SUPPLY_HEALTH_GOOD; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 460 | } else { |
| 461 | if (tval & BQ27000_FLAG_EDV1) |
| 462 | tval = POWER_SUPPLY_HEALTH_DEAD; |
| 463 | else |
| 464 | tval = POWER_SUPPLY_HEALTH_GOOD; |
| 465 | return tval; |
| 466 | } |
| 467 | |
| 468 | return -1; |
| 469 | } |
| 470 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 471 | static void bq27x00_update(struct bq27x00_device_info *di) |
| 472 | { |
| 473 | struct bq27x00_reg_cache cache = {0, }; |
| 474 | bool is_bq27500 = di->chip == BQ27500; |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 475 | bool is_bq27510 = di->chip == BQ27510; |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 476 | bool is_bq27425 = di->chip == BQ27425; |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 477 | bool is_bq27742 = di->chip == BQ27742; |
Puthikorn Voravootivat | a3c0c3e | 2014-09-30 11:18:56 -0700 | [diff] [blame] | 478 | bool flags_1b = !(is_bq27500 || is_bq27742); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 479 | |
Puthikorn Voravootivat | a3c0c3e | 2014-09-30 11:18:56 -0700 | [diff] [blame] | 480 | cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b); |
Marek Belisko | 3dd843e | 2014-06-30 21:49:56 +0200 | [diff] [blame] | 481 | if ((cache.flags & 0xff) == 0xff) |
| 482 | /* read error */ |
| 483 | cache.flags = -1; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 484 | if (cache.flags >= 0) { |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 485 | if (!is_bq27500 && !is_bq27425 && !is_bq27742 && !is_bq27510 |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 486 | && (cache.flags & BQ27000_FLAG_CI)) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 487 | 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] | 488 | cache.capacity = -ENODATA; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 489 | cache.energy = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 490 | cache.time_to_empty = -ENODATA; |
| 491 | cache.time_to_empty_avg = -ENODATA; |
| 492 | cache.time_to_full = -ENODATA; |
| 493 | cache.charge_full = -ENODATA; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 494 | cache.health = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 495 | } else { |
| 496 | cache.capacity = bq27x00_battery_read_rsoc(di); |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 497 | if (is_bq27742 || is_bq27510) |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 498 | cache.time_to_empty = |
| 499 | bq27x00_battery_read_time(di, |
| 500 | BQ27x00_REG_TTE); |
| 501 | else if (!is_bq27425) { |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 502 | cache.energy = bq27x00_battery_read_energy(di); |
| 503 | cache.time_to_empty = |
| 504 | bq27x00_battery_read_time(di, |
| 505 | BQ27x00_REG_TTE); |
| 506 | cache.time_to_empty_avg = |
| 507 | bq27x00_battery_read_time(di, |
| 508 | BQ27x00_REG_TTECP); |
| 509 | cache.time_to_full = |
| 510 | bq27x00_battery_read_time(di, |
| 511 | BQ27x00_REG_TTF); |
| 512 | } |
Puthikorn Voravootivat | a3c0c3e | 2014-09-30 11:18:56 -0700 | [diff] [blame] | 513 | cache.charge_full = bq27x00_battery_read_lmd(di); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 514 | cache.health = bq27x00_battery_read_health(di); |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 515 | } |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 516 | cache.temperature = bq27x00_battery_read_temperature(di); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 517 | if (!is_bq27425) |
| 518 | cache.cycle_count = bq27x00_battery_read_cyct(di); |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 519 | if (is_bq27742) |
| 520 | cache.power_avg = |
| 521 | bq27x00_battery_read_pwr_avg(di, |
| 522 | BQ27742_POWER_AVG); |
| 523 | else |
| 524 | cache.power_avg = |
| 525 | bq27x00_battery_read_pwr_avg(di, |
| 526 | BQ27x00_POWER_AVG); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 527 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 528 | /* We only have to read charge design full once */ |
| 529 | if (di->charge_design_full <= 0) |
| 530 | di->charge_design_full = bq27x00_battery_read_ilmd(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Puthikorn Voravootivat | 90f04a2 | 2014-10-21 18:15:37 -0700 | [diff] [blame] | 533 | if (di->cache.capacity != cache.capacity) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 534 | power_supply_changed(di->bat); |
Puthikorn Voravootivat | 90f04a2 | 2014-10-21 18:15:37 -0700 | [diff] [blame] | 535 | |
| 536 | if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) |
| 537 | di->cache = cache; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 538 | |
| 539 | di->last_update = jiffies; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 542 | static void bq27x00_battery_poll(struct work_struct *work) |
| 543 | { |
| 544 | struct bq27x00_device_info *di = |
| 545 | container_of(work, struct bq27x00_device_info, work.work); |
| 546 | |
| 547 | bq27x00_update(di); |
| 548 | |
| 549 | if (poll_interval > 0) { |
| 550 | /* The timer does not have to be accurate. */ |
| 551 | set_timer_slack(&di->work.timer, poll_interval * HZ / 4); |
| 552 | schedule_delayed_work(&di->work, poll_interval * HZ); |
| 553 | } |
| 554 | } |
| 555 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 556 | /* |
Pali Rohár | bf7d414 | 2011-02-01 00:23:13 +0100 | [diff] [blame] | 557 | * Return the battery average current in µA |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 558 | * Note that current can be negative signed as well |
| 559 | * Or 0 if something fails. |
| 560 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 561 | static int bq27x00_battery_current(struct bq27x00_device_info *di, |
| 562 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 563 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 564 | int curr; |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 565 | int flags; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 566 | |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 567 | curr = bq27x00_read(di, BQ27x00_REG_AI, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 568 | if (curr < 0) { |
| 569 | dev_err(di->dev, "error reading current\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 570 | return curr; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 571 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 572 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 573 | if (bq27xxx_is_chip_version_higher(di)) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 574 | /* bq27500 returns signed value */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 575 | val->intval = (int)((s16)curr) * 1000; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 576 | } else { |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 577 | flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false); |
| 578 | if (flags & BQ27000_FLAG_CHGS) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 579 | dev_dbg(di->dev, "negative current!\n"); |
Grazvydas Ignotas | afbc74fd | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 580 | curr = -curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 581 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 582 | |
| 583 | val->intval = curr * 3570 / BQ27000_RS; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 584 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 585 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 586 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 589 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 590 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 591 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 592 | int status; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 593 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 594 | if (bq27xxx_is_chip_version_higher(di)) { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 595 | if (di->cache.flags & BQ27500_FLAG_FC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 596 | status = POWER_SUPPLY_STATUS_FULL; |
Anton Vorontsov | b7aaacf | 2012-02-01 21:20:15 +0400 | [diff] [blame] | 597 | else if (di->cache.flags & BQ27500_FLAG_DSC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 598 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
Pali Rohár | 270968c | 2011-11-01 01:43:08 +0100 | [diff] [blame] | 599 | else |
Anton Vorontsov | b7aaacf | 2012-02-01 21:20:15 +0400 | [diff] [blame] | 600 | status = POWER_SUPPLY_STATUS_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 601 | } else { |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 602 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 603 | status = POWER_SUPPLY_STATUS_FULL; |
| 604 | else if (di->cache.flags & BQ27000_FLAG_CHGS) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 605 | status = POWER_SUPPLY_STATUS_CHARGING; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 606 | else if (power_supply_am_i_supplied(di->bat)) |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 607 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 608 | else |
| 609 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 610 | } |
| 611 | |
| 612 | val->intval = status; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 613 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 617 | static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di, |
| 618 | union power_supply_propval *val) |
| 619 | { |
| 620 | int level; |
| 621 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 622 | if (bq27xxx_is_chip_version_higher(di)) { |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 623 | if (di->cache.flags & BQ27500_FLAG_FC) |
| 624 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 625 | else if (di->cache.flags & BQ27500_FLAG_SOC1) |
| 626 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 627 | else if (di->cache.flags & BQ27500_FLAG_SOCF) |
| 628 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 629 | else |
| 630 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
| 631 | } else { |
| 632 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 633 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 634 | else if (di->cache.flags & BQ27000_FLAG_EDV1) |
| 635 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 636 | else if (di->cache.flags & BQ27000_FLAG_EDVF) |
| 637 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 638 | else |
| 639 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
| 640 | } |
| 641 | |
| 642 | val->intval = level; |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 647 | /* |
Thomas Weber | 91fe4d5 | 2012-02-17 17:46:21 +0100 | [diff] [blame] | 648 | * Return the battery Voltage in millivolts |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 649 | * Or < 0 if something fails. |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 650 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 651 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di, |
| 652 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 653 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 654 | int volt; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 655 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 656 | volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 657 | if (volt < 0) { |
| 658 | dev_err(di->dev, "error reading voltage\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 659 | return volt; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 660 | } |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 661 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 662 | val->intval = volt * 1000; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 663 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static int bq27x00_simple_value(int value, |
| 668 | union power_supply_propval *val) |
| 669 | { |
| 670 | if (value < 0) |
| 671 | return value; |
| 672 | |
| 673 | val->intval = value; |
| 674 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 675 | return 0; |
| 676 | } |
| 677 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 678 | static int bq27x00_battery_get_property(struct power_supply *psy, |
| 679 | enum power_supply_property psp, |
| 680 | union power_supply_propval *val) |
| 681 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 682 | int ret = 0; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 683 | struct bq27x00_device_info *di = power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 684 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 685 | mutex_lock(&di->lock); |
| 686 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) { |
| 687 | cancel_delayed_work_sync(&di->work); |
| 688 | bq27x00_battery_poll(&di->work.work); |
| 689 | } |
| 690 | mutex_unlock(&di->lock); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 691 | |
| 692 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 693 | return -ENODEV; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 694 | |
| 695 | switch (psp) { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 696 | case POWER_SUPPLY_PROP_STATUS: |
| 697 | ret = bq27x00_battery_status(di, val); |
| 698 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 699 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 700 | ret = bq27x00_battery_voltage(di, val); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 701 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 702 | case POWER_SUPPLY_PROP_PRESENT: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 703 | val->intval = di->cache.flags < 0 ? 0 : 1; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 704 | break; |
| 705 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 706 | ret = bq27x00_battery_current(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 707 | break; |
| 708 | case POWER_SUPPLY_PROP_CAPACITY: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 709 | ret = bq27x00_simple_value(di->cache.capacity, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 710 | break; |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 711 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
| 712 | ret = bq27x00_battery_capacity_level(di, val); |
| 713 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 714 | case POWER_SUPPLY_PROP_TEMP: |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 715 | ret = bq27x00_simple_value(di->cache.temperature, val); |
Pali Rohár | 5dc3443 | 2013-02-02 11:06:09 +0100 | [diff] [blame] | 716 | if (ret == 0) |
| 717 | val->intval -= 2731; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 718 | break; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 719 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 720 | ret = bq27x00_simple_value(di->cache.time_to_empty, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 721 | break; |
| 722 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 723 | ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 724 | break; |
| 725 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 726 | ret = bq27x00_simple_value(di->cache.time_to_full, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 727 | break; |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 728 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 729 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 730 | break; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 731 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 732 | ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val); |
| 733 | break; |
| 734 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 735 | ret = bq27x00_simple_value(di->cache.charge_full, val); |
| 736 | break; |
| 737 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 738 | ret = bq27x00_simple_value(di->charge_design_full, val); |
| 739 | break; |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 740 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 741 | ret = bq27x00_simple_value(di->cache.cycle_count, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 742 | break; |
| 743 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 744 | ret = bq27x00_simple_value(di->cache.energy, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 745 | break; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 746 | case POWER_SUPPLY_PROP_POWER_AVG: |
| 747 | ret = bq27x00_simple_value(di->cache.power_avg, val); |
| 748 | break; |
| 749 | case POWER_SUPPLY_PROP_HEALTH: |
| 750 | ret = bq27x00_simple_value(di->cache.health, val); |
| 751 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 752 | default: |
| 753 | return -EINVAL; |
| 754 | } |
| 755 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 756 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 759 | static void bq27x00_external_power_changed(struct power_supply *psy) |
| 760 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 761 | struct bq27x00_device_info *di = power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 762 | |
| 763 | cancel_delayed_work_sync(&di->work); |
| 764 | schedule_delayed_work(&di->work, 0); |
| 765 | } |
| 766 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 767 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di, |
| 768 | const char *name) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 769 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 770 | int ret; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 771 | struct power_supply_desc *psy_desc; |
| 772 | struct power_supply_config psy_cfg = { .drv_data = di, }; |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 773 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 774 | psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL); |
| 775 | if (!psy_desc) |
| 776 | return -ENOMEM; |
| 777 | |
| 778 | psy_desc->name = name; |
| 779 | psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 780 | if (di->chip == BQ27425) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 781 | psy_desc->properties = bq27425_battery_props; |
| 782 | psy_desc->num_properties = ARRAY_SIZE(bq27425_battery_props); |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 783 | } else if (di->chip == BQ27742) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 784 | psy_desc->properties = bq27742_battery_props; |
| 785 | psy_desc->num_properties = ARRAY_SIZE(bq27742_battery_props); |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 786 | } else if (di->chip == BQ27510) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 787 | psy_desc->properties = bq27510_battery_props; |
| 788 | psy_desc->num_properties = ARRAY_SIZE(bq27510_battery_props); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 789 | } else { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 790 | psy_desc->properties = bq27x00_battery_props; |
| 791 | psy_desc->num_properties = ARRAY_SIZE(bq27x00_battery_props); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 792 | } |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 793 | psy_desc->get_property = bq27x00_battery_get_property; |
| 794 | psy_desc->external_power_changed = bq27x00_external_power_changed; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 795 | |
| 796 | INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll); |
| 797 | mutex_init(&di->lock); |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 798 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 799 | di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg); |
| 800 | if (IS_ERR(di->bat)) { |
| 801 | ret = PTR_ERR(di->bat); |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 802 | dev_err(di->dev, "failed to register battery: %d\n", ret); |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 807 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 808 | bq27x00_update(di); |
| 809 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 810 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 813 | static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di) |
| 814 | { |
Pali Rohár | 8cfaaa8 | 2011-11-01 01:43:11 +0100 | [diff] [blame] | 815 | /* |
| 816 | * power_supply_unregister call bq27x00_battery_get_property which |
| 817 | * call bq27x00_battery_poll. |
| 818 | * Make sure that bq27x00_battery_poll will not call |
| 819 | * schedule_delayed_work again after unregister (which cause OOPS). |
| 820 | */ |
| 821 | poll_interval = 0; |
| 822 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 823 | cancel_delayed_work_sync(&di->work); |
| 824 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 825 | power_supply_unregister(di->bat); |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 826 | |
| 827 | mutex_destroy(&di->lock); |
| 828 | } |
| 829 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 830 | |
| 831 | /* i2c specific code */ |
| 832 | #ifdef CONFIG_BATTERY_BQ27X00_I2C |
| 833 | |
| 834 | /* If the system has several batteries we need a different name for each |
| 835 | * of them... |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 836 | */ |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 837 | static DEFINE_IDR(battery_id); |
| 838 | static DEFINE_MUTEX(battery_mutex); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 839 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 840 | static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 841 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 842 | struct i2c_client *client = to_i2c_client(di->dev); |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 843 | struct i2c_msg msg[2]; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 844 | unsigned char data[2]; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 845 | int ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 846 | |
| 847 | if (!client->adapter) |
| 848 | return -ENODEV; |
| 849 | |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 850 | msg[0].addr = client->addr; |
| 851 | msg[0].flags = 0; |
| 852 | msg[0].buf = ® |
| 853 | msg[0].len = sizeof(reg); |
| 854 | msg[1].addr = client->addr; |
| 855 | msg[1].flags = I2C_M_RD; |
| 856 | msg[1].buf = data; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 857 | if (single) |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 858 | msg[1].len = 1; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 859 | else |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 860 | msg[1].len = 2; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 861 | |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 862 | ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 863 | if (ret < 0) |
| 864 | return ret; |
| 865 | |
| 866 | if (!single) |
| 867 | ret = get_unaligned_le16(data); |
| 868 | else |
| 869 | ret = data[0]; |
| 870 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 871 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 874 | static int bq27x00_battery_probe(struct i2c_client *client, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 875 | const struct i2c_device_id *id) |
| 876 | { |
| 877 | char *name; |
| 878 | struct bq27x00_device_info *di; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 879 | int num; |
| 880 | int retval = 0; |
| 881 | |
| 882 | /* Get new ID for the new battery device */ |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 883 | mutex_lock(&battery_mutex); |
Tejun Heo | 05e2cef | 2013-02-27 17:04:37 -0800 | [diff] [blame] | 884 | num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 885 | mutex_unlock(&battery_mutex); |
Tejun Heo | 05e2cef | 2013-02-27 17:04:37 -0800 | [diff] [blame] | 886 | if (num < 0) |
| 887 | return num; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 888 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 889 | name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 890 | if (!name) { |
| 891 | dev_err(&client->dev, "failed to allocate device name\n"); |
| 892 | retval = -ENOMEM; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 893 | goto batt_failed; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Himangi Saraogi | 1cb82fd | 2014-05-28 22:58:49 +0530 | [diff] [blame] | 896 | di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 897 | if (!di) { |
| 898 | dev_err(&client->dev, "failed to allocate device info data\n"); |
| 899 | retval = -ENOMEM; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 900 | goto batt_failed; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 901 | } |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 902 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 903 | di->id = num; |
| 904 | di->dev = &client->dev; |
| 905 | di->chip = id->driver_data; |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 906 | di->bus.read = &bq27x00_read_i2c; |
| 907 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 908 | retval = bq27x00_powersupply_init(di, name); |
Julia Lawall | f776099 | 2012-08-19 10:44:20 +0200 | [diff] [blame] | 909 | if (retval) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 910 | goto batt_failed; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 911 | |
| 912 | i2c_set_clientdata(client, di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 913 | |
| 914 | return 0; |
| 915 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 916 | batt_failed: |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 917 | mutex_lock(&battery_mutex); |
| 918 | idr_remove(&battery_id, num); |
| 919 | mutex_unlock(&battery_mutex); |
| 920 | |
| 921 | return retval; |
| 922 | } |
| 923 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 924 | static int bq27x00_battery_remove(struct i2c_client *client) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 925 | { |
| 926 | struct bq27x00_device_info *di = i2c_get_clientdata(client); |
| 927 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 928 | bq27x00_powersupply_unregister(di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 929 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 930 | mutex_lock(&battery_mutex); |
| 931 | idr_remove(&battery_id, di->id); |
| 932 | mutex_unlock(&battery_mutex); |
| 933 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 937 | static const struct i2c_device_id bq27x00_id[] = { |
| 938 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ |
| 939 | { "bq27500", BQ27500 }, |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 940 | { "bq27425", BQ27425 }, |
Puthikorn Voravootivat | 628ef02 | 2014-09-09 12:20:35 -0700 | [diff] [blame] | 941 | { "bq27742", BQ27742 }, |
Alexandre Belloni | f46bf82 | 2015-02-28 18:59:28 +0100 | [diff] [blame] | 942 | { "bq27510", BQ27510 }, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 943 | {}, |
| 944 | }; |
Pali Rohár | fd9b958 | 2011-02-01 00:10:41 +0100 | [diff] [blame] | 945 | MODULE_DEVICE_TABLE(i2c, bq27x00_id); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 946 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 947 | static struct i2c_driver bq27x00_battery_driver = { |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 948 | .driver = { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 949 | .name = "bq27x00-battery", |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 950 | }, |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 951 | .probe = bq27x00_battery_probe, |
| 952 | .remove = bq27x00_battery_remove, |
| 953 | .id_table = bq27x00_id, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 954 | }; |
| 955 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 956 | static inline int bq27x00_battery_i2c_init(void) |
| 957 | { |
| 958 | int ret = i2c_add_driver(&bq27x00_battery_driver); |
| 959 | if (ret) |
| 960 | printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); |
| 961 | |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | static inline void bq27x00_battery_i2c_exit(void) |
| 966 | { |
| 967 | i2c_del_driver(&bq27x00_battery_driver); |
| 968 | } |
| 969 | |
| 970 | #else |
| 971 | |
| 972 | static inline int bq27x00_battery_i2c_init(void) { return 0; } |
| 973 | static inline void bq27x00_battery_i2c_exit(void) {}; |
| 974 | |
| 975 | #endif |
| 976 | |
| 977 | /* platform specific code */ |
| 978 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM |
| 979 | |
| 980 | static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 981 | bool single) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 982 | { |
| 983 | struct device *dev = di->dev; |
| 984 | struct bq27000_platform_data *pdata = dev->platform_data; |
| 985 | unsigned int timeout = 3; |
| 986 | int upper, lower; |
| 987 | int temp; |
| 988 | |
| 989 | if (!single) { |
| 990 | /* Make sure the value has not changed in between reading the |
| 991 | * lower and the upper part */ |
| 992 | upper = pdata->read(dev, reg + 1); |
| 993 | do { |
| 994 | temp = upper; |
| 995 | if (upper < 0) |
| 996 | return upper; |
| 997 | |
| 998 | lower = pdata->read(dev, reg); |
| 999 | if (lower < 0) |
| 1000 | return lower; |
| 1001 | |
| 1002 | upper = pdata->read(dev, reg + 1); |
| 1003 | } while (temp != upper && --timeout); |
| 1004 | |
| 1005 | if (timeout == 0) |
| 1006 | return -EIO; |
| 1007 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1008 | return (upper << 8) | lower; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1009 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 1010 | |
| 1011 | return pdata->read(dev, reg); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 1014 | static int bq27000_battery_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1015 | { |
| 1016 | struct bq27x00_device_info *di; |
| 1017 | struct bq27000_platform_data *pdata = pdev->dev.platform_data; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1018 | const char *name; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1019 | |
| 1020 | if (!pdata) { |
| 1021 | dev_err(&pdev->dev, "no platform_data supplied\n"); |
| 1022 | return -EINVAL; |
| 1023 | } |
| 1024 | |
| 1025 | if (!pdata->read) { |
| 1026 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); |
| 1027 | return -EINVAL; |
| 1028 | } |
| 1029 | |
Himangi Saraogi | 1cb82fd | 2014-05-28 22:58:49 +0530 | [diff] [blame] | 1030 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1031 | if (!di) { |
| 1032 | dev_err(&pdev->dev, "failed to allocate device info data\n"); |
| 1033 | return -ENOMEM; |
| 1034 | } |
| 1035 | |
| 1036 | platform_set_drvdata(pdev, di); |
| 1037 | |
| 1038 | di->dev = &pdev->dev; |
| 1039 | di->chip = BQ27000; |
| 1040 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1041 | name = pdata->name ?: dev_name(&pdev->dev); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1042 | di->bus.read = &bq27000_read_platform; |
| 1043 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1044 | return bq27x00_powersupply_init(di, name); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 1047 | static int bq27000_battery_remove(struct platform_device *pdev) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1048 | { |
| 1049 | struct bq27x00_device_info *di = platform_get_drvdata(pdev); |
| 1050 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 1051 | bq27x00_powersupply_unregister(di); |
| 1052 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | static struct platform_driver bq27000_battery_driver = { |
| 1057 | .probe = bq27000_battery_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 1058 | .remove = bq27000_battery_remove, |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1059 | .driver = { |
| 1060 | .name = "bq27000-battery", |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1061 | }, |
| 1062 | }; |
| 1063 | |
| 1064 | static inline int bq27x00_battery_platform_init(void) |
| 1065 | { |
| 1066 | int ret = platform_driver_register(&bq27000_battery_driver); |
| 1067 | if (ret) |
| 1068 | printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); |
| 1069 | |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | static inline void bq27x00_battery_platform_exit(void) |
| 1074 | { |
| 1075 | platform_driver_unregister(&bq27000_battery_driver); |
| 1076 | } |
| 1077 | |
| 1078 | #else |
| 1079 | |
| 1080 | static inline int bq27x00_battery_platform_init(void) { return 0; } |
| 1081 | static inline void bq27x00_battery_platform_exit(void) {}; |
| 1082 | |
| 1083 | #endif |
| 1084 | |
| 1085 | /* |
| 1086 | * Module stuff |
| 1087 | */ |
| 1088 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1089 | static int __init bq27x00_battery_init(void) |
| 1090 | { |
| 1091 | int ret; |
| 1092 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1093 | ret = bq27x00_battery_i2c_init(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1094 | if (ret) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1095 | return ret; |
| 1096 | |
| 1097 | ret = bq27x00_battery_platform_init(); |
| 1098 | if (ret) |
| 1099 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1100 | |
| 1101 | return ret; |
| 1102 | } |
| 1103 | module_init(bq27x00_battery_init); |
| 1104 | |
| 1105 | static void __exit bq27x00_battery_exit(void) |
| 1106 | { |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1107 | bq27x00_battery_platform_exit(); |
| 1108 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1109 | } |
| 1110 | module_exit(bq27x00_battery_exit); |
| 1111 | |
Marek Belisko | 8ebb7e9 | 2015-04-30 22:05:58 +0200 | [diff] [blame] | 1112 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM |
| 1113 | MODULE_ALIAS("platform:bq27000-battery"); |
| 1114 | #endif |
| 1115 | |
| 1116 | #ifdef CONFIG_BATTERY_BQ27X00_I2C |
| 1117 | MODULE_ALIAS("i2c:bq27000-battery"); |
| 1118 | #endif |
| 1119 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1120 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 1121 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); |
| 1122 | MODULE_LICENSE("GPL"); |