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