Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Gas Gauge driver for TI's BQ20Z75 |
| 3 | * |
| 4 | * Copyright (c) 2010, NVIDIA Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/power_supply.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/slab.h> |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/gpio.h> |
| 30 | |
| 31 | #include <linux/power/bq20z75.h> |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 32 | |
| 33 | enum { |
| 34 | REG_MANUFACTURER_DATA, |
| 35 | REG_TEMPERATURE, |
| 36 | REG_VOLTAGE, |
| 37 | REG_CURRENT, |
| 38 | REG_CAPACITY, |
| 39 | REG_TIME_TO_EMPTY, |
| 40 | REG_TIME_TO_FULL, |
| 41 | REG_STATUS, |
| 42 | REG_CYCLE_COUNT, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 43 | REG_SERIAL_NUMBER, |
| 44 | REG_REMAINING_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 45 | REG_REMAINING_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 46 | REG_FULL_CHARGE_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 47 | REG_FULL_CHARGE_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 48 | REG_DESIGN_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 49 | REG_DESIGN_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 50 | REG_DESIGN_VOLTAGE, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 53 | /* Battery Mode defines */ |
| 54 | #define BATTERY_MODE_OFFSET 0x03 |
| 55 | #define BATTERY_MODE_MASK 0x8000 |
| 56 | enum bq20z75_battery_mode { |
| 57 | BATTERY_MODE_AMPS, |
| 58 | BATTERY_MODE_WATTS |
| 59 | }; |
| 60 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 61 | /* manufacturer access defines */ |
| 62 | #define MANUFACTURER_ACCESS_STATUS 0x0006 |
| 63 | #define MANUFACTURER_ACCESS_SLEEP 0x0011 |
| 64 | |
| 65 | /* battery status value bits */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 66 | #define BATTERY_DISCHARGING 0x40 |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 67 | #define BATTERY_FULL_CHARGED 0x20 |
| 68 | #define BATTERY_FULL_DISCHARGED 0x10 |
| 69 | |
| 70 | #define BQ20Z75_DATA(_psp, _addr, _min_value, _max_value) { \ |
| 71 | .psp = _psp, \ |
| 72 | .addr = _addr, \ |
| 73 | .min_value = _min_value, \ |
| 74 | .max_value = _max_value, \ |
| 75 | } |
| 76 | |
| 77 | static const struct bq20z75_device_data { |
| 78 | enum power_supply_property psp; |
| 79 | u8 addr; |
| 80 | int min_value; |
| 81 | int max_value; |
| 82 | } bq20z75_data[] = { |
| 83 | [REG_MANUFACTURER_DATA] = |
| 84 | BQ20Z75_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), |
| 85 | [REG_TEMPERATURE] = |
| 86 | BQ20Z75_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), |
| 87 | [REG_VOLTAGE] = |
| 88 | BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000), |
| 89 | [REG_CURRENT] = |
| 90 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, |
| 91 | 32767), |
| 92 | [REG_CAPACITY] = |
| 93 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 94 | [REG_REMAINING_CAPACITY] = |
| 95 | BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 96 | [REG_REMAINING_CAPACITY_CHARGE] = |
| 97 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 98 | [REG_FULL_CHARGE_CAPACITY] = |
| 99 | BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 100 | [REG_FULL_CHARGE_CAPACITY_CHARGE] = |
| 101 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 102 | [REG_TIME_TO_EMPTY] = |
| 103 | BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, |
| 104 | 65535), |
| 105 | [REG_TIME_TO_FULL] = |
| 106 | BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, |
| 107 | 65535), |
| 108 | [REG_STATUS] = |
| 109 | BQ20Z75_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), |
| 110 | [REG_CYCLE_COUNT] = |
| 111 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 112 | [REG_DESIGN_CAPACITY] = |
| 113 | BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, |
| 114 | 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 115 | [REG_DESIGN_CAPACITY_CHARGE] = |
| 116 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, |
| 117 | 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 118 | [REG_DESIGN_VOLTAGE] = |
| 119 | BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, |
| 120 | 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 121 | [REG_SERIAL_NUMBER] = |
| 122 | BQ20Z75_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), |
| 123 | }; |
| 124 | |
| 125 | static enum power_supply_property bq20z75_properties[] = { |
| 126 | POWER_SUPPLY_PROP_STATUS, |
| 127 | POWER_SUPPLY_PROP_HEALTH, |
| 128 | POWER_SUPPLY_PROP_PRESENT, |
| 129 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 130 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 131 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 132 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 133 | POWER_SUPPLY_PROP_CAPACITY, |
| 134 | POWER_SUPPLY_PROP_TEMP, |
| 135 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 136 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 137 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 138 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 139 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 140 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 141 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 142 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 143 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 144 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | struct bq20z75_info { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 148 | struct i2c_client *client; |
| 149 | struct power_supply power_supply; |
| 150 | struct bq20z75_platform_data *pdata; |
| 151 | bool is_present; |
| 152 | bool gpio_detect; |
| 153 | bool enable_detection; |
| 154 | int irq; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 157 | static int bq20z75_read_word_data(struct i2c_client *client, u8 address) |
| 158 | { |
| 159 | s32 ret; |
| 160 | |
| 161 | ret = i2c_smbus_read_word_data(client, address); |
| 162 | if (ret < 0) { |
| 163 | dev_err(&client->dev, |
| 164 | "%s: i2c read at address 0x%x failed\n", |
| 165 | __func__, address); |
| 166 | return ret; |
| 167 | } |
| 168 | return le16_to_cpu(ret); |
| 169 | } |
| 170 | |
| 171 | static int bq20z75_write_word_data(struct i2c_client *client, u8 address, |
| 172 | u16 value) |
| 173 | { |
| 174 | s32 ret; |
| 175 | |
| 176 | ret = i2c_smbus_write_word_data(client, address, le16_to_cpu(value)); |
| 177 | if (ret < 0) { |
| 178 | dev_err(&client->dev, |
| 179 | "%s: i2c write to address 0x%x failed\n", |
| 180 | __func__, address); |
| 181 | return ret; |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 186 | static int bq20z75_get_battery_presence_and_health( |
| 187 | struct i2c_client *client, enum power_supply_property psp, |
| 188 | union power_supply_propval *val) |
| 189 | { |
| 190 | s32 ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 191 | struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); |
| 192 | |
| 193 | if (psp == POWER_SUPPLY_PROP_PRESENT && |
| 194 | bq20z75_device->gpio_detect) { |
| 195 | ret = gpio_get_value( |
| 196 | bq20z75_device->pdata->battery_detect); |
| 197 | if (ret == bq20z75_device->pdata->battery_detect_present) |
| 198 | val->intval = 1; |
| 199 | else |
| 200 | val->intval = 0; |
| 201 | return ret; |
| 202 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 203 | |
| 204 | /* Write to ManufacturerAccess with |
| 205 | * ManufacturerAccess command and then |
| 206 | * read the status */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 207 | ret = bq20z75_write_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 208 | bq20z75_data[REG_MANUFACTURER_DATA].addr, |
| 209 | MANUFACTURER_ACCESS_STATUS); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 210 | if (ret < 0) |
| 211 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 212 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 213 | |
| 214 | ret = bq20z75_read_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 215 | bq20z75_data[REG_MANUFACTURER_DATA].addr); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 216 | if (ret < 0) { |
| 217 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 218 | val->intval = 0; /* battery removed */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 219 | return ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 220 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 221 | |
| 222 | if (ret < bq20z75_data[REG_MANUFACTURER_DATA].min_value || |
| 223 | ret > bq20z75_data[REG_MANUFACTURER_DATA].max_value) { |
| 224 | val->intval = 0; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* Mask the upper nibble of 2nd byte and |
| 229 | * lower byte of response then |
| 230 | * shift the result by 8 to get status*/ |
| 231 | ret &= 0x0F00; |
| 232 | ret >>= 8; |
| 233 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 234 | if (ret == 0x0F) |
| 235 | /* battery removed */ |
| 236 | val->intval = 0; |
| 237 | else |
| 238 | val->intval = 1; |
| 239 | } else if (psp == POWER_SUPPLY_PROP_HEALTH) { |
| 240 | if (ret == 0x09) |
| 241 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 242 | else if (ret == 0x0B) |
| 243 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 244 | else if (ret == 0x0C) |
| 245 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 246 | else |
| 247 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int bq20z75_get_battery_property(struct i2c_client *client, |
| 254 | int reg_offset, enum power_supply_property psp, |
| 255 | union power_supply_propval *val) |
| 256 | { |
| 257 | s32 ret; |
| 258 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 259 | ret = bq20z75_read_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 260 | bq20z75_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 261 | if (ret < 0) |
| 262 | return ret; |
| 263 | |
| 264 | /* returned values are 16 bit */ |
| 265 | if (bq20z75_data[reg_offset].min_value < 0) |
| 266 | ret = (s16)ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 267 | |
| 268 | if (ret >= bq20z75_data[reg_offset].min_value && |
| 269 | ret <= bq20z75_data[reg_offset].max_value) { |
| 270 | val->intval = ret; |
| 271 | if (psp == POWER_SUPPLY_PROP_STATUS) { |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 272 | if (ret & BATTERY_FULL_CHARGED) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 273 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 274 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 275 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 276 | else if (ret & BATTERY_DISCHARGING) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 277 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 278 | else |
| 279 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 280 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 281 | } else { |
| 282 | if (psp == POWER_SUPPLY_PROP_STATUS) |
| 283 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 284 | else |
| 285 | val->intval = 0; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 291 | static void bq20z75_unit_adjustment(struct i2c_client *client, |
| 292 | enum power_supply_property psp, union power_supply_propval *val) |
| 293 | { |
| 294 | #define BASE_UNIT_CONVERSION 1000 |
| 295 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) |
| 296 | #define TIME_UNIT_CONVERSION 600 |
| 297 | #define TEMP_KELVIN_TO_CELCIUS 2731 |
| 298 | switch (psp) { |
| 299 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 300 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 301 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| 302 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
| 303 | break; |
| 304 | |
| 305 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 306 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 307 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 308 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 309 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 310 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 311 | val->intval *= BASE_UNIT_CONVERSION; |
| 312 | break; |
| 313 | |
| 314 | case POWER_SUPPLY_PROP_TEMP: |
| 315 | /* bq20z75 provides battery tempreture in 0.1°K |
| 316 | * so convert it to 0.1°C */ |
| 317 | val->intval -= TEMP_KELVIN_TO_CELCIUS; |
| 318 | val->intval *= 10; |
| 319 | break; |
| 320 | |
| 321 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 322 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
| 323 | val->intval *= TIME_UNIT_CONVERSION; |
| 324 | break; |
| 325 | |
| 326 | default: |
| 327 | dev_dbg(&client->dev, |
| 328 | "%s: no need for unit conversion %d\n", __func__, psp); |
| 329 | } |
| 330 | } |
| 331 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 332 | static enum bq20z75_battery_mode |
| 333 | bq20z75_set_battery_mode(struct i2c_client *client, |
| 334 | enum bq20z75_battery_mode mode) |
| 335 | { |
| 336 | int ret, original_val; |
| 337 | |
| 338 | original_val = bq20z75_read_word_data(client, BATTERY_MODE_OFFSET); |
| 339 | if (original_val < 0) |
| 340 | return original_val; |
| 341 | |
| 342 | if ((original_val & BATTERY_MODE_MASK) == mode) |
| 343 | return mode; |
| 344 | |
| 345 | if (mode == BATTERY_MODE_AMPS) |
| 346 | ret = original_val & ~BATTERY_MODE_MASK; |
| 347 | else |
| 348 | ret = original_val | BATTERY_MODE_MASK; |
| 349 | |
| 350 | ret = bq20z75_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
| 351 | if (ret < 0) |
| 352 | return ret; |
| 353 | |
| 354 | return original_val & BATTERY_MODE_MASK; |
| 355 | } |
| 356 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 357 | static int bq20z75_get_battery_capacity(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 358 | int reg_offset, enum power_supply_property psp, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 359 | union power_supply_propval *val) |
| 360 | { |
| 361 | s32 ret; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 362 | enum bq20z75_battery_mode mode = BATTERY_MODE_WATTS; |
| 363 | |
| 364 | if (power_supply_is_amp_property(psp)) |
| 365 | mode = BATTERY_MODE_AMPS; |
| 366 | |
| 367 | mode = bq20z75_set_battery_mode(client, mode); |
| 368 | if (mode < 0) |
| 369 | return mode; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 370 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 371 | ret = bq20z75_read_word_data(client, bq20z75_data[reg_offset].addr); |
| 372 | if (ret < 0) |
| 373 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 374 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 375 | if (psp == POWER_SUPPLY_PROP_CAPACITY) { |
| 376 | /* bq20z75 spec says that this can be >100 % |
| 377 | * even if max value is 100 % */ |
| 378 | val->intval = min(ret, 100); |
| 379 | } else |
| 380 | val->intval = ret; |
| 381 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 382 | ret = bq20z75_set_battery_mode(client, mode); |
| 383 | if (ret < 0) |
| 384 | return ret; |
| 385 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static char bq20z75_serial[5]; |
| 390 | static int bq20z75_get_battery_serial_number(struct i2c_client *client, |
| 391 | union power_supply_propval *val) |
| 392 | { |
| 393 | int ret; |
| 394 | |
| 395 | ret = bq20z75_read_word_data(client, |
| 396 | bq20z75_data[REG_SERIAL_NUMBER].addr); |
| 397 | if (ret < 0) |
| 398 | return ret; |
| 399 | |
| 400 | ret = sprintf(bq20z75_serial, "%04x", ret); |
| 401 | val->strval = bq20z75_serial; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 406 | static int bq20z75_get_property_index(struct i2c_client *client, |
| 407 | enum power_supply_property psp) |
| 408 | { |
| 409 | int count; |
| 410 | for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) |
| 411 | if (psp == bq20z75_data[count].psp) |
| 412 | return count; |
| 413 | |
| 414 | dev_warn(&client->dev, |
| 415 | "%s: Invalid Property - %d\n", __func__, psp); |
| 416 | |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 420 | static int bq20z75_get_property(struct power_supply *psy, |
| 421 | enum power_supply_property psp, |
| 422 | union power_supply_propval *val) |
| 423 | { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 424 | int ret = 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 425 | struct bq20z75_info *bq20z75_device = container_of(psy, |
| 426 | struct bq20z75_info, power_supply); |
| 427 | struct i2c_client *client = bq20z75_device->client; |
| 428 | |
| 429 | switch (psp) { |
| 430 | case POWER_SUPPLY_PROP_PRESENT: |
| 431 | case POWER_SUPPLY_PROP_HEALTH: |
| 432 | ret = bq20z75_get_battery_presence_and_health(client, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 433 | break; |
| 434 | |
| 435 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 436 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 437 | break; |
| 438 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 439 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 440 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 441 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 442 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 443 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 444 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 445 | case POWER_SUPPLY_PROP_CAPACITY: |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 446 | ret = bq20z75_get_property_index(client, psp); |
| 447 | if (ret < 0) |
| 448 | break; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 449 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 450 | ret = bq20z75_get_battery_capacity(client, ret, psp, val); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 454 | ret = bq20z75_get_battery_serial_number(client, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | case POWER_SUPPLY_PROP_STATUS: |
| 458 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 459 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 460 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 461 | case POWER_SUPPLY_PROP_TEMP: |
| 462 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 463 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 464 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 465 | ret = bq20z75_get_property_index(client, psp); |
| 466 | if (ret < 0) |
| 467 | break; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 468 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 469 | ret = bq20z75_get_battery_property(client, ret, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 470 | break; |
| 471 | |
| 472 | default: |
| 473 | dev_err(&client->dev, |
| 474 | "%s: INVALID property\n", __func__); |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 478 | if (!bq20z75_device->enable_detection) |
| 479 | goto done; |
| 480 | |
| 481 | if (!bq20z75_device->gpio_detect && |
| 482 | bq20z75_device->is_present != (ret >= 0)) { |
| 483 | bq20z75_device->is_present = (ret >= 0); |
| 484 | power_supply_changed(&bq20z75_device->power_supply); |
| 485 | } |
| 486 | |
| 487 | done: |
| 488 | if (!ret) { |
| 489 | /* Convert units to match requirements for power supply class */ |
| 490 | bq20z75_unit_adjustment(client, psp, val); |
| 491 | } |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 492 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 493 | dev_dbg(&client->dev, |
| 494 | "%s: property = %d, value = %d\n", __func__, psp, val->intval); |
| 495 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 496 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 499 | static irqreturn_t bq20z75_irq(int irq, void *devid) |
| 500 | { |
| 501 | struct power_supply *battery = devid; |
| 502 | |
| 503 | power_supply_changed(battery); |
| 504 | |
| 505 | return IRQ_HANDLED; |
| 506 | } |
| 507 | |
| 508 | static int __devinit bq20z75_probe(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 509 | const struct i2c_device_id *id) |
| 510 | { |
| 511 | struct bq20z75_info *bq20z75_device; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 512 | struct bq20z75_platform_data *pdata = client->dev.platform_data; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 513 | int rc; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 514 | int irq; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 515 | |
| 516 | bq20z75_device = kzalloc(sizeof(struct bq20z75_info), GFP_KERNEL); |
| 517 | if (!bq20z75_device) |
| 518 | return -ENOMEM; |
| 519 | |
| 520 | bq20z75_device->client = client; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 521 | bq20z75_device->enable_detection = false; |
| 522 | bq20z75_device->gpio_detect = false; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 523 | bq20z75_device->power_supply.name = "battery"; |
| 524 | bq20z75_device->power_supply.type = POWER_SUPPLY_TYPE_BATTERY; |
| 525 | bq20z75_device->power_supply.properties = bq20z75_properties; |
| 526 | bq20z75_device->power_supply.num_properties = |
| 527 | ARRAY_SIZE(bq20z75_properties); |
| 528 | bq20z75_device->power_supply.get_property = bq20z75_get_property; |
| 529 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 530 | if (pdata) { |
| 531 | bq20z75_device->gpio_detect = |
| 532 | gpio_is_valid(pdata->battery_detect); |
| 533 | bq20z75_device->pdata = pdata; |
| 534 | } |
| 535 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 536 | i2c_set_clientdata(client, bq20z75_device); |
| 537 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 538 | if (!bq20z75_device->gpio_detect) |
| 539 | goto skip_gpio; |
| 540 | |
| 541 | rc = gpio_request(pdata->battery_detect, dev_name(&client->dev)); |
| 542 | if (rc) { |
| 543 | dev_warn(&client->dev, "Failed to request gpio: %d\n", rc); |
| 544 | bq20z75_device->gpio_detect = false; |
| 545 | goto skip_gpio; |
| 546 | } |
| 547 | |
| 548 | rc = gpio_direction_input(pdata->battery_detect); |
| 549 | if (rc) { |
| 550 | dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc); |
| 551 | gpio_free(pdata->battery_detect); |
| 552 | bq20z75_device->gpio_detect = false; |
| 553 | goto skip_gpio; |
| 554 | } |
| 555 | |
| 556 | irq = gpio_to_irq(pdata->battery_detect); |
| 557 | if (irq <= 0) { |
| 558 | dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); |
| 559 | gpio_free(pdata->battery_detect); |
| 560 | bq20z75_device->gpio_detect = false; |
| 561 | goto skip_gpio; |
| 562 | } |
| 563 | |
| 564 | rc = request_irq(irq, bq20z75_irq, |
| 565 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 566 | dev_name(&client->dev), &bq20z75_device->power_supply); |
| 567 | if (rc) { |
| 568 | dev_warn(&client->dev, "Failed to request irq: %d\n", rc); |
| 569 | gpio_free(pdata->battery_detect); |
| 570 | bq20z75_device->gpio_detect = false; |
| 571 | goto skip_gpio; |
| 572 | } |
| 573 | |
| 574 | bq20z75_device->irq = irq; |
| 575 | |
| 576 | skip_gpio: |
| 577 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 578 | rc = power_supply_register(&client->dev, &bq20z75_device->power_supply); |
| 579 | if (rc) { |
| 580 | dev_err(&client->dev, |
| 581 | "%s: Failed to register power supply\n", __func__); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 582 | goto exit_psupply; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | dev_info(&client->dev, |
| 586 | "%s: battery gas gauge device registered\n", client->name); |
| 587 | |
| 588 | return 0; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 589 | |
| 590 | exit_psupply: |
| 591 | if (bq20z75_device->irq) |
| 592 | free_irq(bq20z75_device->irq, &bq20z75_device->power_supply); |
| 593 | if (bq20z75_device->gpio_detect) |
| 594 | gpio_free(pdata->battery_detect); |
| 595 | |
| 596 | kfree(bq20z75_device); |
| 597 | |
| 598 | return rc; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 601 | static int __devexit bq20z75_remove(struct i2c_client *client) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 602 | { |
| 603 | struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); |
| 604 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 605 | if (bq20z75_device->irq) |
| 606 | free_irq(bq20z75_device->irq, &bq20z75_device->power_supply); |
| 607 | if (bq20z75_device->gpio_detect) |
| 608 | gpio_free(bq20z75_device->pdata->battery_detect); |
| 609 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 610 | power_supply_unregister(&bq20z75_device->power_supply); |
| 611 | kfree(bq20z75_device); |
| 612 | bq20z75_device = NULL; |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | #if defined CONFIG_PM |
| 618 | static int bq20z75_suspend(struct i2c_client *client, |
| 619 | pm_message_t state) |
| 620 | { |
| 621 | s32 ret; |
| 622 | |
| 623 | /* write to manufacturer access with sleep command */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 624 | ret = bq20z75_write_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 625 | bq20z75_data[REG_MANUFACTURER_DATA].addr, |
| 626 | MANUFACTURER_ACCESS_SLEEP); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 627 | if (ret < 0) |
| 628 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | #else |
| 633 | #define bq20z75_suspend NULL |
| 634 | #endif |
| 635 | /* any smbus transaction will wake up bq20z75 */ |
| 636 | #define bq20z75_resume NULL |
| 637 | |
| 638 | static const struct i2c_device_id bq20z75_id[] = { |
| 639 | { "bq20z75", 0 }, |
| 640 | {} |
| 641 | }; |
| 642 | |
| 643 | static struct i2c_driver bq20z75_battery_driver = { |
| 644 | .probe = bq20z75_probe, |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame^] | 645 | .remove = __devexit_p(bq20z75_remove), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 646 | .suspend = bq20z75_suspend, |
| 647 | .resume = bq20z75_resume, |
| 648 | .id_table = bq20z75_id, |
| 649 | .driver = { |
| 650 | .name = "bq20z75-battery", |
| 651 | }, |
| 652 | }; |
| 653 | |
| 654 | static int __init bq20z75_battery_init(void) |
| 655 | { |
| 656 | return i2c_add_driver(&bq20z75_battery_driver); |
| 657 | } |
| 658 | module_init(bq20z75_battery_init); |
| 659 | |
| 660 | static void __exit bq20z75_battery_exit(void) |
| 661 | { |
| 662 | i2c_del_driver(&bq20z75_battery_driver); |
| 663 | } |
| 664 | module_exit(bq20z75_battery_exit); |
| 665 | |
| 666 | MODULE_DESCRIPTION("BQ20z75 battery monitor driver"); |
| 667 | MODULE_LICENSE("GPL"); |