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