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 | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 302 | * Return the battery temperature in tenths of degree Celsius |
| 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 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 315 | if (bq27xxx_is_chip_version_higher(di)) |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 316 | temp -= 2731; |
| 317 | else |
| 318 | temp = ((temp * 5) - 5463) / 2; |
| 319 | |
| 320 | return temp; |
| 321 | } |
| 322 | |
| 323 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 324 | * Return the battery Cycle count total |
| 325 | * Or < 0 if something fails. |
| 326 | */ |
| 327 | static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di) |
| 328 | { |
| 329 | int cyct; |
| 330 | |
| 331 | cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false); |
| 332 | if (cyct < 0) |
| 333 | dev_err(di->dev, "error reading cycle count total\n"); |
| 334 | |
| 335 | return cyct; |
| 336 | } |
| 337 | |
| 338 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 339 | * Read a time register. |
| 340 | * Return < 0 if something fails. |
| 341 | */ |
| 342 | static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg) |
| 343 | { |
| 344 | int tval; |
| 345 | |
| 346 | tval = bq27x00_read(di, reg, false); |
| 347 | if (tval < 0) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 348 | dev_dbg(di->dev, "error reading time register %02x: %d\n", |
| 349 | reg, tval); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 350 | return tval; |
| 351 | } |
| 352 | |
| 353 | if (tval == 65535) |
| 354 | return -ENODATA; |
| 355 | |
| 356 | return tval * 60; |
| 357 | } |
| 358 | |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 359 | /* |
| 360 | * Read a power avg register. |
| 361 | * Return < 0 if something fails. |
| 362 | */ |
| 363 | static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg) |
| 364 | { |
| 365 | int tval; |
| 366 | |
| 367 | tval = bq27x00_read(di, reg, false); |
| 368 | if (tval < 0) { |
| 369 | dev_err(di->dev, "error reading power avg rgister %02x: %d\n", |
| 370 | reg, tval); |
| 371 | return tval; |
| 372 | } |
| 373 | |
| 374 | if (di->chip == BQ27500) |
| 375 | return tval; |
| 376 | else |
| 377 | return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Read flag register. |
| 382 | * Return < 0 if something fails. |
| 383 | */ |
| 384 | static int bq27x00_battery_read_health(struct bq27x00_device_info *di) |
| 385 | { |
| 386 | int tval; |
| 387 | |
| 388 | tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false); |
| 389 | if (tval < 0) { |
| 390 | dev_err(di->dev, "error reading flag register:%d\n", tval); |
| 391 | return tval; |
| 392 | } |
| 393 | |
| 394 | if ((di->chip == BQ27500)) { |
| 395 | if (tval & BQ27500_FLAG_SOCF) |
| 396 | tval = POWER_SUPPLY_HEALTH_DEAD; |
| 397 | else if (tval & BQ27500_FLAG_OTC) |
| 398 | tval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 399 | else |
| 400 | tval = POWER_SUPPLY_HEALTH_GOOD; |
| 401 | return tval; |
| 402 | } else { |
| 403 | if (tval & BQ27000_FLAG_EDV1) |
| 404 | tval = POWER_SUPPLY_HEALTH_DEAD; |
| 405 | else |
| 406 | tval = POWER_SUPPLY_HEALTH_GOOD; |
| 407 | return tval; |
| 408 | } |
| 409 | |
| 410 | return -1; |
| 411 | } |
| 412 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 413 | static void bq27x00_update(struct bq27x00_device_info *di) |
| 414 | { |
| 415 | struct bq27x00_reg_cache cache = {0, }; |
| 416 | bool is_bq27500 = di->chip == BQ27500; |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 417 | bool is_bq27425 = di->chip == BQ27425; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 418 | |
Grazvydas Ignotas | 4d40365 | 2012-01-14 22:50:49 +0200 | [diff] [blame] | 419 | cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 420 | if (cache.flags >= 0) { |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 421 | if (!is_bq27500 && !is_bq27425 |
| 422 | && (cache.flags & BQ27000_FLAG_CI)) { |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 423 | 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] | 424 | cache.capacity = -ENODATA; |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 425 | cache.energy = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 426 | cache.time_to_empty = -ENODATA; |
| 427 | cache.time_to_empty_avg = -ENODATA; |
| 428 | cache.time_to_full = -ENODATA; |
| 429 | cache.charge_full = -ENODATA; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 430 | cache.health = -ENODATA; |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 431 | } else { |
| 432 | cache.capacity = bq27x00_battery_read_rsoc(di); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 433 | if (!is_bq27425) { |
| 434 | cache.energy = bq27x00_battery_read_energy(di); |
| 435 | cache.time_to_empty = |
| 436 | bq27x00_battery_read_time(di, |
| 437 | BQ27x00_REG_TTE); |
| 438 | cache.time_to_empty_avg = |
| 439 | bq27x00_battery_read_time(di, |
| 440 | BQ27x00_REG_TTECP); |
| 441 | cache.time_to_full = |
| 442 | bq27x00_battery_read_time(di, |
| 443 | BQ27x00_REG_TTF); |
| 444 | } |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 445 | cache.charge_full = bq27x00_battery_read_lmd(di); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 446 | cache.health = bq27x00_battery_read_health(di); |
Pali Rohár | 4b226c2 | 2011-11-01 01:43:05 +0100 | [diff] [blame] | 447 | } |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 448 | cache.temperature = bq27x00_battery_read_temperature(di); |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 449 | if (!is_bq27425) |
| 450 | cache.cycle_count = bq27x00_battery_read_cyct(di); |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 451 | cache.cycle_count = bq27x00_battery_read_cyct(di); |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 452 | cache.power_avg = |
| 453 | bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 454 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 455 | /* We only have to read charge design full once */ |
| 456 | if (di->charge_design_full <= 0) |
| 457 | di->charge_design_full = bq27x00_battery_read_ilmd(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 460 | if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 461 | di->cache = cache; |
| 462 | power_supply_changed(&di->bat); |
| 463 | } |
| 464 | |
| 465 | di->last_update = jiffies; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 468 | static void bq27x00_battery_poll(struct work_struct *work) |
| 469 | { |
| 470 | struct bq27x00_device_info *di = |
| 471 | container_of(work, struct bq27x00_device_info, work.work); |
| 472 | |
| 473 | bq27x00_update(di); |
| 474 | |
| 475 | if (poll_interval > 0) { |
| 476 | /* The timer does not have to be accurate. */ |
| 477 | set_timer_slack(&di->work.timer, poll_interval * HZ / 4); |
| 478 | schedule_delayed_work(&di->work, poll_interval * HZ); |
| 479 | } |
| 480 | } |
| 481 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 482 | /* |
Pali Rohár | bf7d414 | 2011-02-01 00:23:13 +0100 | [diff] [blame] | 483 | * Return the battery average current in µA |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 484 | * Note that current can be negative signed as well |
| 485 | * Or 0 if something fails. |
| 486 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 487 | static int bq27x00_battery_current(struct bq27x00_device_info *di, |
| 488 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 489 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 490 | int curr; |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 491 | int flags; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 492 | |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 493 | curr = bq27x00_read(di, BQ27x00_REG_AI, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 494 | if (curr < 0) { |
| 495 | dev_err(di->dev, "error reading current\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 496 | return curr; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 497 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 498 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 499 | if (bq27xxx_is_chip_version_higher(di)) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 500 | /* bq27500 returns signed value */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 501 | val->intval = (int)((s16)curr) * 1000; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 502 | } else { |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame] | 503 | flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false); |
| 504 | if (flags & BQ27000_FLAG_CHGS) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 505 | dev_dbg(di->dev, "negative current!\n"); |
Grazvydas Ignotas | afbc74f | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 506 | curr = -curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 507 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 508 | |
| 509 | val->intval = curr * 3570 / BQ27000_RS; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 510 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 511 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 512 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 515 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 516 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 517 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 518 | int status; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 519 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 520 | if (bq27xxx_is_chip_version_higher(di)) { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 521 | if (di->cache.flags & BQ27500_FLAG_FC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 522 | status = POWER_SUPPLY_STATUS_FULL; |
Anton Vorontsov | b7aaacf | 2012-02-01 21:20:15 +0400 | [diff] [blame] | 523 | else if (di->cache.flags & BQ27500_FLAG_DSC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 524 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
Pali Rohár | 270968c | 2011-11-01 01:43:08 +0100 | [diff] [blame] | 525 | else |
Anton Vorontsov | b7aaacf | 2012-02-01 21:20:15 +0400 | [diff] [blame] | 526 | status = POWER_SUPPLY_STATUS_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 527 | } else { |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 528 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 529 | status = POWER_SUPPLY_STATUS_FULL; |
| 530 | else if (di->cache.flags & BQ27000_FLAG_CHGS) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 531 | status = POWER_SUPPLY_STATUS_CHARGING; |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 532 | else if (power_supply_am_i_supplied(&di->bat)) |
| 533 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 534 | else |
| 535 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 536 | } |
| 537 | |
| 538 | val->intval = status; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 539 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 543 | static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di, |
| 544 | union power_supply_propval *val) |
| 545 | { |
| 546 | int level; |
| 547 | |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 548 | if (bq27xxx_is_chip_version_higher(di)) { |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 549 | if (di->cache.flags & BQ27500_FLAG_FC) |
| 550 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 551 | else if (di->cache.flags & BQ27500_FLAG_SOC1) |
| 552 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 553 | else if (di->cache.flags & BQ27500_FLAG_SOCF) |
| 554 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 555 | else |
| 556 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
| 557 | } else { |
| 558 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 559 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 560 | else if (di->cache.flags & BQ27000_FLAG_EDV1) |
| 561 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
| 562 | else if (di->cache.flags & BQ27000_FLAG_EDVF) |
| 563 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 564 | else |
| 565 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
| 566 | } |
| 567 | |
| 568 | val->intval = level; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 573 | /* |
Thomas Weber | 91fe4d5 | 2012-02-17 17:46:21 +0100 | [diff] [blame] | 574 | * Return the battery Voltage in millivolts |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 575 | * Or < 0 if something fails. |
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 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di, |
| 578 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 579 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 580 | int volt; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 581 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 582 | volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 583 | if (volt < 0) { |
| 584 | dev_err(di->dev, "error reading voltage\n"); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 585 | return volt; |
Pali Rohár | c6cd4f2 | 2011-11-01 01:43:09 +0100 | [diff] [blame] | 586 | } |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 587 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 588 | val->intval = volt * 1000; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 589 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int bq27x00_simple_value(int value, |
| 594 | union power_supply_propval *val) |
| 595 | { |
| 596 | if (value < 0) |
| 597 | return value; |
| 598 | |
| 599 | val->intval = value; |
| 600 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 604 | #define to_bq27x00_device_info(x) container_of((x), \ |
| 605 | struct bq27x00_device_info, bat); |
| 606 | |
| 607 | static int bq27x00_battery_get_property(struct power_supply *psy, |
| 608 | enum power_supply_property psp, |
| 609 | union power_supply_propval *val) |
| 610 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 611 | int ret = 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 612 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 613 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 614 | mutex_lock(&di->lock); |
| 615 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) { |
| 616 | cancel_delayed_work_sync(&di->work); |
| 617 | bq27x00_battery_poll(&di->work.work); |
| 618 | } |
| 619 | mutex_unlock(&di->lock); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 620 | |
| 621 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 622 | return -ENODEV; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 623 | |
| 624 | switch (psp) { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 625 | case POWER_SUPPLY_PROP_STATUS: |
| 626 | ret = bq27x00_battery_status(di, val); |
| 627 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 628 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 629 | ret = bq27x00_battery_voltage(di, val); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 630 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 631 | case POWER_SUPPLY_PROP_PRESENT: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 632 | val->intval = di->cache.flags < 0 ? 0 : 1; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 633 | break; |
| 634 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 635 | ret = bq27x00_battery_current(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 636 | break; |
| 637 | case POWER_SUPPLY_PROP_CAPACITY: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 638 | ret = bq27x00_simple_value(di->cache.capacity, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 639 | break; |
Pali Rohár | d66bab3 | 2011-11-01 01:43:04 +0100 | [diff] [blame] | 640 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
| 641 | ret = bq27x00_battery_capacity_level(di, val); |
| 642 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 643 | case POWER_SUPPLY_PROP_TEMP: |
Pali Rohár | d149e98 | 2011-11-01 01:43:07 +0100 | [diff] [blame] | 644 | ret = bq27x00_simple_value(di->cache.temperature, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 645 | break; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 646 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 647 | ret = bq27x00_simple_value(di->cache.time_to_empty, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 648 | break; |
| 649 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 650 | ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 651 | break; |
| 652 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 653 | ret = bq27x00_simple_value(di->cache.time_to_full, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 654 | break; |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 655 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 656 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 657 | break; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 658 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 659 | ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val); |
| 660 | break; |
| 661 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 662 | ret = bq27x00_simple_value(di->cache.charge_full, val); |
| 663 | break; |
| 664 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 665 | ret = bq27x00_simple_value(di->charge_design_full, val); |
| 666 | break; |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 667 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 668 | ret = bq27x00_simple_value(di->cache.cycle_count, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 669 | break; |
| 670 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
Pali Rohár | a8f6bd2 | 2011-11-01 01:43:06 +0100 | [diff] [blame] | 671 | ret = bq27x00_simple_value(di->cache.energy, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 672 | break; |
Syed Rafiuddin | 9903e62 | 2012-05-17 12:51:15 +0530 | [diff] [blame] | 673 | case POWER_SUPPLY_PROP_POWER_AVG: |
| 674 | ret = bq27x00_simple_value(di->cache.power_avg, val); |
| 675 | break; |
| 676 | case POWER_SUPPLY_PROP_HEALTH: |
| 677 | ret = bq27x00_simple_value(di->cache.health, val); |
| 678 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 679 | default: |
| 680 | return -EINVAL; |
| 681 | } |
| 682 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 683 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 686 | static void bq27x00_external_power_changed(struct power_supply *psy) |
| 687 | { |
| 688 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
| 689 | |
| 690 | cancel_delayed_work_sync(&di->work); |
| 691 | schedule_delayed_work(&di->work, 0); |
| 692 | } |
| 693 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 694 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 695 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 696 | int ret; |
| 697 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 698 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 699 | di->chip = BQ27425; |
| 700 | if (di->chip == BQ27425) { |
| 701 | di->bat.properties = bq27425_battery_props; |
| 702 | di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props); |
| 703 | } else { |
| 704 | di->bat.properties = bq27x00_battery_props; |
| 705 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); |
| 706 | } |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 707 | di->bat.get_property = bq27x00_battery_get_property; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 708 | di->bat.external_power_changed = bq27x00_external_power_changed; |
| 709 | |
| 710 | INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll); |
| 711 | mutex_init(&di->lock); |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 712 | |
| 713 | ret = power_supply_register(di->dev, &di->bat); |
| 714 | if (ret) { |
| 715 | dev_err(di->dev, "failed to register battery: %d\n", ret); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 720 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 721 | bq27x00_update(di); |
| 722 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 723 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 726 | static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di) |
| 727 | { |
Pali Rohár | 8cfaaa8 | 2011-11-01 01:43:11 +0100 | [diff] [blame] | 728 | /* |
| 729 | * power_supply_unregister call bq27x00_battery_get_property which |
| 730 | * call bq27x00_battery_poll. |
| 731 | * Make sure that bq27x00_battery_poll will not call |
| 732 | * schedule_delayed_work again after unregister (which cause OOPS). |
| 733 | */ |
| 734 | poll_interval = 0; |
| 735 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 736 | cancel_delayed_work_sync(&di->work); |
| 737 | |
| 738 | power_supply_unregister(&di->bat); |
| 739 | |
| 740 | mutex_destroy(&di->lock); |
| 741 | } |
| 742 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 743 | |
| 744 | /* i2c specific code */ |
| 745 | #ifdef CONFIG_BATTERY_BQ27X00_I2C |
| 746 | |
| 747 | /* If the system has several batteries we need a different name for each |
| 748 | * of them... |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 749 | */ |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 750 | static DEFINE_IDR(battery_id); |
| 751 | static DEFINE_MUTEX(battery_mutex); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 752 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 753 | 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] | 754 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 755 | struct i2c_client *client = to_i2c_client(di->dev); |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 756 | struct i2c_msg msg[2]; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 757 | unsigned char data[2]; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 758 | int ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 759 | |
| 760 | if (!client->adapter) |
| 761 | return -ENODEV; |
| 762 | |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 763 | msg[0].addr = client->addr; |
| 764 | msg[0].flags = 0; |
| 765 | msg[0].buf = ® |
| 766 | msg[0].len = sizeof(reg); |
| 767 | msg[1].addr = client->addr; |
| 768 | msg[1].flags = I2C_M_RD; |
| 769 | msg[1].buf = data; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 770 | if (single) |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 771 | msg[1].len = 1; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 772 | else |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 773 | msg[1].len = 2; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 774 | |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 775 | ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 776 | if (ret < 0) |
| 777 | return ret; |
| 778 | |
| 779 | if (!single) |
| 780 | ret = get_unaligned_le16(data); |
| 781 | else |
| 782 | ret = data[0]; |
| 783 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 784 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 787 | static int bq27x00_battery_probe(struct i2c_client *client, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 788 | const struct i2c_device_id *id) |
| 789 | { |
| 790 | char *name; |
| 791 | struct bq27x00_device_info *di; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 792 | int num; |
| 793 | int retval = 0; |
| 794 | |
| 795 | /* Get new ID for the new battery device */ |
| 796 | retval = idr_pre_get(&battery_id, GFP_KERNEL); |
| 797 | if (retval == 0) |
| 798 | return -ENOMEM; |
| 799 | mutex_lock(&battery_mutex); |
| 800 | retval = idr_get_new(&battery_id, client, &num); |
| 801 | mutex_unlock(&battery_mutex); |
| 802 | if (retval < 0) |
| 803 | return retval; |
| 804 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 805 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 806 | if (!name) { |
| 807 | dev_err(&client->dev, "failed to allocate device name\n"); |
| 808 | retval = -ENOMEM; |
| 809 | goto batt_failed_1; |
| 810 | } |
| 811 | |
| 812 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 813 | if (!di) { |
| 814 | dev_err(&client->dev, "failed to allocate device info data\n"); |
| 815 | retval = -ENOMEM; |
| 816 | goto batt_failed_2; |
| 817 | } |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 818 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 819 | di->id = num; |
| 820 | di->dev = &client->dev; |
| 821 | di->chip = id->driver_data; |
| 822 | di->bat.name = name; |
| 823 | di->bus.read = &bq27x00_read_i2c; |
| 824 | |
Julia Lawall | f776099 | 2012-08-19 10:44:20 +0200 | [diff] [blame] | 825 | retval = bq27x00_powersupply_init(di); |
| 826 | if (retval) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 827 | goto batt_failed_3; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 828 | |
| 829 | i2c_set_clientdata(client, di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 830 | |
| 831 | return 0; |
| 832 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 833 | batt_failed_3: |
| 834 | kfree(di); |
| 835 | batt_failed_2: |
| 836 | kfree(name); |
| 837 | batt_failed_1: |
| 838 | mutex_lock(&battery_mutex); |
| 839 | idr_remove(&battery_id, num); |
| 840 | mutex_unlock(&battery_mutex); |
| 841 | |
| 842 | return retval; |
| 843 | } |
| 844 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 845 | static int bq27x00_battery_remove(struct i2c_client *client) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 846 | { |
| 847 | struct bq27x00_device_info *di = i2c_get_clientdata(client); |
| 848 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 849 | bq27x00_powersupply_unregister(di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 850 | |
| 851 | kfree(di->bat.name); |
| 852 | |
| 853 | mutex_lock(&battery_mutex); |
| 854 | idr_remove(&battery_id, di->id); |
| 855 | mutex_unlock(&battery_mutex); |
| 856 | |
| 857 | kfree(di); |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 862 | static const struct i2c_device_id bq27x00_id[] = { |
| 863 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ |
| 864 | { "bq27500", BQ27500 }, |
Saranya Gopal | a66f59b | 2012-07-13 20:20:07 -0700 | [diff] [blame] | 865 | { "bq27425", BQ27425 }, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 866 | {}, |
| 867 | }; |
Pali Rohár | fd9b958 | 2011-02-01 00:10:41 +0100 | [diff] [blame] | 868 | MODULE_DEVICE_TABLE(i2c, bq27x00_id); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 869 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 870 | static struct i2c_driver bq27x00_battery_driver = { |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 871 | .driver = { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 872 | .name = "bq27x00-battery", |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 873 | }, |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 874 | .probe = bq27x00_battery_probe, |
| 875 | .remove = bq27x00_battery_remove, |
| 876 | .id_table = bq27x00_id, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 877 | }; |
| 878 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 879 | static inline int bq27x00_battery_i2c_init(void) |
| 880 | { |
| 881 | int ret = i2c_add_driver(&bq27x00_battery_driver); |
| 882 | if (ret) |
| 883 | printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); |
| 884 | |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | static inline void bq27x00_battery_i2c_exit(void) |
| 889 | { |
| 890 | i2c_del_driver(&bq27x00_battery_driver); |
| 891 | } |
| 892 | |
| 893 | #else |
| 894 | |
| 895 | static inline int bq27x00_battery_i2c_init(void) { return 0; } |
| 896 | static inline void bq27x00_battery_i2c_exit(void) {}; |
| 897 | |
| 898 | #endif |
| 899 | |
| 900 | /* platform specific code */ |
| 901 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM |
| 902 | |
| 903 | 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] | 904 | bool single) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 905 | { |
| 906 | struct device *dev = di->dev; |
| 907 | struct bq27000_platform_data *pdata = dev->platform_data; |
| 908 | unsigned int timeout = 3; |
| 909 | int upper, lower; |
| 910 | int temp; |
| 911 | |
| 912 | if (!single) { |
| 913 | /* Make sure the value has not changed in between reading the |
| 914 | * lower and the upper part */ |
| 915 | upper = pdata->read(dev, reg + 1); |
| 916 | do { |
| 917 | temp = upper; |
| 918 | if (upper < 0) |
| 919 | return upper; |
| 920 | |
| 921 | lower = pdata->read(dev, reg); |
| 922 | if (lower < 0) |
| 923 | return lower; |
| 924 | |
| 925 | upper = pdata->read(dev, reg + 1); |
| 926 | } while (temp != upper && --timeout); |
| 927 | |
| 928 | if (timeout == 0) |
| 929 | return -EIO; |
| 930 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 931 | return (upper << 8) | lower; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 932 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 933 | |
| 934 | return pdata->read(dev, reg); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static int __devinit bq27000_battery_probe(struct platform_device *pdev) |
| 938 | { |
| 939 | struct bq27x00_device_info *di; |
| 940 | struct bq27000_platform_data *pdata = pdev->dev.platform_data; |
| 941 | int ret; |
| 942 | |
| 943 | if (!pdata) { |
| 944 | dev_err(&pdev->dev, "no platform_data supplied\n"); |
| 945 | return -EINVAL; |
| 946 | } |
| 947 | |
| 948 | if (!pdata->read) { |
| 949 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | |
| 953 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 954 | if (!di) { |
| 955 | dev_err(&pdev->dev, "failed to allocate device info data\n"); |
| 956 | return -ENOMEM; |
| 957 | } |
| 958 | |
| 959 | platform_set_drvdata(pdev, di); |
| 960 | |
| 961 | di->dev = &pdev->dev; |
| 962 | di->chip = BQ27000; |
| 963 | |
| 964 | di->bat.name = pdata->name ?: dev_name(&pdev->dev); |
| 965 | di->bus.read = &bq27000_read_platform; |
| 966 | |
| 967 | ret = bq27x00_powersupply_init(di); |
| 968 | if (ret) |
| 969 | goto err_free; |
| 970 | |
| 971 | return 0; |
| 972 | |
| 973 | err_free: |
| 974 | platform_set_drvdata(pdev, NULL); |
| 975 | kfree(di); |
| 976 | |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | static int __devexit bq27000_battery_remove(struct platform_device *pdev) |
| 981 | { |
| 982 | struct bq27x00_device_info *di = platform_get_drvdata(pdev); |
| 983 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 984 | bq27x00_powersupply_unregister(di); |
| 985 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 986 | platform_set_drvdata(pdev, NULL); |
| 987 | kfree(di); |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static struct platform_driver bq27000_battery_driver = { |
| 993 | .probe = bq27000_battery_probe, |
| 994 | .remove = __devexit_p(bq27000_battery_remove), |
| 995 | .driver = { |
| 996 | .name = "bq27000-battery", |
| 997 | .owner = THIS_MODULE, |
| 998 | }, |
| 999 | }; |
| 1000 | |
| 1001 | static inline int bq27x00_battery_platform_init(void) |
| 1002 | { |
| 1003 | int ret = platform_driver_register(&bq27000_battery_driver); |
| 1004 | if (ret) |
| 1005 | printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); |
| 1006 | |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | static inline void bq27x00_battery_platform_exit(void) |
| 1011 | { |
| 1012 | platform_driver_unregister(&bq27000_battery_driver); |
| 1013 | } |
| 1014 | |
| 1015 | #else |
| 1016 | |
| 1017 | static inline int bq27x00_battery_platform_init(void) { return 0; } |
| 1018 | static inline void bq27x00_battery_platform_exit(void) {}; |
| 1019 | |
| 1020 | #endif |
| 1021 | |
| 1022 | /* |
| 1023 | * Module stuff |
| 1024 | */ |
| 1025 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1026 | static int __init bq27x00_battery_init(void) |
| 1027 | { |
| 1028 | int ret; |
| 1029 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1030 | ret = bq27x00_battery_i2c_init(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1031 | if (ret) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1032 | return ret; |
| 1033 | |
| 1034 | ret = bq27x00_battery_platform_init(); |
| 1035 | if (ret) |
| 1036 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1037 | |
| 1038 | return ret; |
| 1039 | } |
| 1040 | module_init(bq27x00_battery_init); |
| 1041 | |
| 1042 | static void __exit bq27x00_battery_exit(void) |
| 1043 | { |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 1044 | bq27x00_battery_platform_exit(); |
| 1045 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1046 | } |
| 1047 | module_exit(bq27x00_battery_exit); |
| 1048 | |
| 1049 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 1050 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); |
| 1051 | MODULE_LICENSE("GPL"); |