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 | { |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 159 | struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); |
| 160 | s32 ret = 0; |
| 161 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 162 | |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 163 | if (bq20z75_device->pdata) |
| 164 | retries = max(bq20z75_device->pdata->i2c_retry_count + 1, 1); |
| 165 | |
| 166 | while (retries > 0) { |
| 167 | ret = i2c_smbus_read_word_data(client, address); |
| 168 | if (ret >= 0) |
| 169 | break; |
| 170 | retries--; |
| 171 | } |
| 172 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 173 | if (ret < 0) { |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 174 | dev_warn(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 175 | "%s: i2c read at address 0x%x failed\n", |
| 176 | __func__, address); |
| 177 | return ret; |
| 178 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 179 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 180 | return le16_to_cpu(ret); |
| 181 | } |
| 182 | |
| 183 | static int bq20z75_write_word_data(struct i2c_client *client, u8 address, |
| 184 | u16 value) |
| 185 | { |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 186 | struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); |
| 187 | s32 ret = 0; |
| 188 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 189 | |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 190 | if (bq20z75_device->pdata) |
| 191 | retries = max(bq20z75_device->pdata->i2c_retry_count + 1, 1); |
| 192 | |
| 193 | while (retries > 0) { |
| 194 | ret = i2c_smbus_write_word_data(client, address, |
| 195 | le16_to_cpu(value)); |
| 196 | if (ret >= 0) |
| 197 | break; |
| 198 | retries--; |
| 199 | } |
| 200 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 201 | if (ret < 0) { |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 202 | dev_warn(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 203 | "%s: i2c write to address 0x%x failed\n", |
| 204 | __func__, address); |
| 205 | return ret; |
| 206 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame^] | 207 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 211 | static int bq20z75_get_battery_presence_and_health( |
| 212 | struct i2c_client *client, enum power_supply_property psp, |
| 213 | union power_supply_propval *val) |
| 214 | { |
| 215 | s32 ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 216 | struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); |
| 217 | |
| 218 | if (psp == POWER_SUPPLY_PROP_PRESENT && |
| 219 | bq20z75_device->gpio_detect) { |
| 220 | ret = gpio_get_value( |
| 221 | bq20z75_device->pdata->battery_detect); |
| 222 | if (ret == bq20z75_device->pdata->battery_detect_present) |
| 223 | val->intval = 1; |
| 224 | else |
| 225 | val->intval = 0; |
| 226 | return ret; |
| 227 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 228 | |
| 229 | /* Write to ManufacturerAccess with |
| 230 | * ManufacturerAccess command and then |
| 231 | * read the status */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 232 | ret = bq20z75_write_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 233 | bq20z75_data[REG_MANUFACTURER_DATA].addr, |
| 234 | MANUFACTURER_ACCESS_STATUS); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 235 | if (ret < 0) |
| 236 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 237 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 238 | |
| 239 | ret = bq20z75_read_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 240 | bq20z75_data[REG_MANUFACTURER_DATA].addr); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 241 | if (ret < 0) { |
| 242 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 243 | val->intval = 0; /* battery removed */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 244 | return ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 245 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 246 | |
| 247 | if (ret < bq20z75_data[REG_MANUFACTURER_DATA].min_value || |
| 248 | ret > bq20z75_data[REG_MANUFACTURER_DATA].max_value) { |
| 249 | val->intval = 0; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /* Mask the upper nibble of 2nd byte and |
| 254 | * lower byte of response then |
| 255 | * shift the result by 8 to get status*/ |
| 256 | ret &= 0x0F00; |
| 257 | ret >>= 8; |
| 258 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 259 | if (ret == 0x0F) |
| 260 | /* battery removed */ |
| 261 | val->intval = 0; |
| 262 | else |
| 263 | val->intval = 1; |
| 264 | } else if (psp == POWER_SUPPLY_PROP_HEALTH) { |
| 265 | if (ret == 0x09) |
| 266 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 267 | else if (ret == 0x0B) |
| 268 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 269 | else if (ret == 0x0C) |
| 270 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 271 | else |
| 272 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int bq20z75_get_battery_property(struct i2c_client *client, |
| 279 | int reg_offset, enum power_supply_property psp, |
| 280 | union power_supply_propval *val) |
| 281 | { |
| 282 | s32 ret; |
| 283 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 284 | ret = bq20z75_read_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 285 | bq20z75_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 286 | if (ret < 0) |
| 287 | return ret; |
| 288 | |
| 289 | /* returned values are 16 bit */ |
| 290 | if (bq20z75_data[reg_offset].min_value < 0) |
| 291 | ret = (s16)ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 292 | |
| 293 | if (ret >= bq20z75_data[reg_offset].min_value && |
| 294 | ret <= bq20z75_data[reg_offset].max_value) { |
| 295 | val->intval = ret; |
| 296 | if (psp == POWER_SUPPLY_PROP_STATUS) { |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 297 | if (ret & BATTERY_FULL_CHARGED) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 298 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 299 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 300 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 301 | else if (ret & BATTERY_DISCHARGING) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 302 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 303 | else |
| 304 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 305 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 306 | } else { |
| 307 | if (psp == POWER_SUPPLY_PROP_STATUS) |
| 308 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 309 | else |
| 310 | val->intval = 0; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 316 | static void bq20z75_unit_adjustment(struct i2c_client *client, |
| 317 | enum power_supply_property psp, union power_supply_propval *val) |
| 318 | { |
| 319 | #define BASE_UNIT_CONVERSION 1000 |
| 320 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) |
| 321 | #define TIME_UNIT_CONVERSION 600 |
| 322 | #define TEMP_KELVIN_TO_CELCIUS 2731 |
| 323 | switch (psp) { |
| 324 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 325 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 326 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| 327 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
| 328 | break; |
| 329 | |
| 330 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 331 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 332 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 333 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 334 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 335 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 336 | val->intval *= BASE_UNIT_CONVERSION; |
| 337 | break; |
| 338 | |
| 339 | case POWER_SUPPLY_PROP_TEMP: |
| 340 | /* bq20z75 provides battery tempreture in 0.1°K |
| 341 | * so convert it to 0.1°C */ |
| 342 | val->intval -= TEMP_KELVIN_TO_CELCIUS; |
| 343 | val->intval *= 10; |
| 344 | break; |
| 345 | |
| 346 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 347 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
| 348 | val->intval *= TIME_UNIT_CONVERSION; |
| 349 | break; |
| 350 | |
| 351 | default: |
| 352 | dev_dbg(&client->dev, |
| 353 | "%s: no need for unit conversion %d\n", __func__, psp); |
| 354 | } |
| 355 | } |
| 356 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 357 | static enum bq20z75_battery_mode |
| 358 | bq20z75_set_battery_mode(struct i2c_client *client, |
| 359 | enum bq20z75_battery_mode mode) |
| 360 | { |
| 361 | int ret, original_val; |
| 362 | |
| 363 | original_val = bq20z75_read_word_data(client, BATTERY_MODE_OFFSET); |
| 364 | if (original_val < 0) |
| 365 | return original_val; |
| 366 | |
| 367 | if ((original_val & BATTERY_MODE_MASK) == mode) |
| 368 | return mode; |
| 369 | |
| 370 | if (mode == BATTERY_MODE_AMPS) |
| 371 | ret = original_val & ~BATTERY_MODE_MASK; |
| 372 | else |
| 373 | ret = original_val | BATTERY_MODE_MASK; |
| 374 | |
| 375 | ret = bq20z75_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
| 376 | if (ret < 0) |
| 377 | return ret; |
| 378 | |
| 379 | return original_val & BATTERY_MODE_MASK; |
| 380 | } |
| 381 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 382 | static int bq20z75_get_battery_capacity(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 383 | int reg_offset, enum power_supply_property psp, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 384 | union power_supply_propval *val) |
| 385 | { |
| 386 | s32 ret; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 387 | enum bq20z75_battery_mode mode = BATTERY_MODE_WATTS; |
| 388 | |
| 389 | if (power_supply_is_amp_property(psp)) |
| 390 | mode = BATTERY_MODE_AMPS; |
| 391 | |
| 392 | mode = bq20z75_set_battery_mode(client, mode); |
| 393 | if (mode < 0) |
| 394 | return mode; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 395 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 396 | ret = bq20z75_read_word_data(client, bq20z75_data[reg_offset].addr); |
| 397 | if (ret < 0) |
| 398 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 399 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 400 | if (psp == POWER_SUPPLY_PROP_CAPACITY) { |
| 401 | /* bq20z75 spec says that this can be >100 % |
| 402 | * even if max value is 100 % */ |
| 403 | val->intval = min(ret, 100); |
| 404 | } else |
| 405 | val->intval = ret; |
| 406 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 407 | ret = bq20z75_set_battery_mode(client, mode); |
| 408 | if (ret < 0) |
| 409 | return ret; |
| 410 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static char bq20z75_serial[5]; |
| 415 | static int bq20z75_get_battery_serial_number(struct i2c_client *client, |
| 416 | union power_supply_propval *val) |
| 417 | { |
| 418 | int ret; |
| 419 | |
| 420 | ret = bq20z75_read_word_data(client, |
| 421 | bq20z75_data[REG_SERIAL_NUMBER].addr); |
| 422 | if (ret < 0) |
| 423 | return ret; |
| 424 | |
| 425 | ret = sprintf(bq20z75_serial, "%04x", ret); |
| 426 | val->strval = bq20z75_serial; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 431 | static int bq20z75_get_property_index(struct i2c_client *client, |
| 432 | enum power_supply_property psp) |
| 433 | { |
| 434 | int count; |
| 435 | for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) |
| 436 | if (psp == bq20z75_data[count].psp) |
| 437 | return count; |
| 438 | |
| 439 | dev_warn(&client->dev, |
| 440 | "%s: Invalid Property - %d\n", __func__, psp); |
| 441 | |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 445 | static int bq20z75_get_property(struct power_supply *psy, |
| 446 | enum power_supply_property psp, |
| 447 | union power_supply_propval *val) |
| 448 | { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 449 | int ret = 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 450 | struct bq20z75_info *bq20z75_device = container_of(psy, |
| 451 | struct bq20z75_info, power_supply); |
| 452 | struct i2c_client *client = bq20z75_device->client; |
| 453 | |
| 454 | switch (psp) { |
| 455 | case POWER_SUPPLY_PROP_PRESENT: |
| 456 | case POWER_SUPPLY_PROP_HEALTH: |
| 457 | ret = bq20z75_get_battery_presence_and_health(client, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 458 | break; |
| 459 | |
| 460 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 461 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 462 | break; |
| 463 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 464 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 465 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 466 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 467 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 468 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 469 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 470 | case POWER_SUPPLY_PROP_CAPACITY: |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 471 | ret = bq20z75_get_property_index(client, psp); |
| 472 | if (ret < 0) |
| 473 | break; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 474 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 475 | ret = bq20z75_get_battery_capacity(client, ret, psp, val); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 476 | break; |
| 477 | |
| 478 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 479 | ret = bq20z75_get_battery_serial_number(client, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 480 | break; |
| 481 | |
| 482 | case POWER_SUPPLY_PROP_STATUS: |
| 483 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 484 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 485 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 486 | case POWER_SUPPLY_PROP_TEMP: |
| 487 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 488 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 489 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 490 | ret = bq20z75_get_property_index(client, psp); |
| 491 | if (ret < 0) |
| 492 | break; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 493 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 494 | ret = bq20z75_get_battery_property(client, ret, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 495 | break; |
| 496 | |
| 497 | default: |
| 498 | dev_err(&client->dev, |
| 499 | "%s: INVALID property\n", __func__); |
| 500 | return -EINVAL; |
| 501 | } |
| 502 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 503 | if (!bq20z75_device->enable_detection) |
| 504 | goto done; |
| 505 | |
| 506 | if (!bq20z75_device->gpio_detect && |
| 507 | bq20z75_device->is_present != (ret >= 0)) { |
| 508 | bq20z75_device->is_present = (ret >= 0); |
| 509 | power_supply_changed(&bq20z75_device->power_supply); |
| 510 | } |
| 511 | |
| 512 | done: |
| 513 | if (!ret) { |
| 514 | /* Convert units to match requirements for power supply class */ |
| 515 | bq20z75_unit_adjustment(client, psp, val); |
| 516 | } |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 517 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 518 | dev_dbg(&client->dev, |
| 519 | "%s: property = %d, value = %d\n", __func__, psp, val->intval); |
| 520 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 521 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 524 | static irqreturn_t bq20z75_irq(int irq, void *devid) |
| 525 | { |
| 526 | struct power_supply *battery = devid; |
| 527 | |
| 528 | power_supply_changed(battery); |
| 529 | |
| 530 | return IRQ_HANDLED; |
| 531 | } |
| 532 | |
| 533 | static int __devinit bq20z75_probe(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 534 | const struct i2c_device_id *id) |
| 535 | { |
| 536 | struct bq20z75_info *bq20z75_device; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 537 | struct bq20z75_platform_data *pdata = client->dev.platform_data; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 538 | int rc; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 539 | int irq; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 540 | |
| 541 | bq20z75_device = kzalloc(sizeof(struct bq20z75_info), GFP_KERNEL); |
| 542 | if (!bq20z75_device) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | bq20z75_device->client = client; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 546 | bq20z75_device->enable_detection = false; |
| 547 | bq20z75_device->gpio_detect = false; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 548 | bq20z75_device->power_supply.name = "battery"; |
| 549 | bq20z75_device->power_supply.type = POWER_SUPPLY_TYPE_BATTERY; |
| 550 | bq20z75_device->power_supply.properties = bq20z75_properties; |
| 551 | bq20z75_device->power_supply.num_properties = |
| 552 | ARRAY_SIZE(bq20z75_properties); |
| 553 | bq20z75_device->power_supply.get_property = bq20z75_get_property; |
| 554 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 555 | if (pdata) { |
| 556 | bq20z75_device->gpio_detect = |
| 557 | gpio_is_valid(pdata->battery_detect); |
| 558 | bq20z75_device->pdata = pdata; |
| 559 | } |
| 560 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 561 | i2c_set_clientdata(client, bq20z75_device); |
| 562 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 563 | if (!bq20z75_device->gpio_detect) |
| 564 | goto skip_gpio; |
| 565 | |
| 566 | rc = gpio_request(pdata->battery_detect, dev_name(&client->dev)); |
| 567 | if (rc) { |
| 568 | dev_warn(&client->dev, "Failed to request gpio: %d\n", rc); |
| 569 | bq20z75_device->gpio_detect = false; |
| 570 | goto skip_gpio; |
| 571 | } |
| 572 | |
| 573 | rc = gpio_direction_input(pdata->battery_detect); |
| 574 | if (rc) { |
| 575 | dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc); |
| 576 | gpio_free(pdata->battery_detect); |
| 577 | bq20z75_device->gpio_detect = false; |
| 578 | goto skip_gpio; |
| 579 | } |
| 580 | |
| 581 | irq = gpio_to_irq(pdata->battery_detect); |
| 582 | if (irq <= 0) { |
| 583 | dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); |
| 584 | gpio_free(pdata->battery_detect); |
| 585 | bq20z75_device->gpio_detect = false; |
| 586 | goto skip_gpio; |
| 587 | } |
| 588 | |
| 589 | rc = request_irq(irq, bq20z75_irq, |
| 590 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 591 | dev_name(&client->dev), &bq20z75_device->power_supply); |
| 592 | if (rc) { |
| 593 | dev_warn(&client->dev, "Failed to request irq: %d\n", rc); |
| 594 | gpio_free(pdata->battery_detect); |
| 595 | bq20z75_device->gpio_detect = false; |
| 596 | goto skip_gpio; |
| 597 | } |
| 598 | |
| 599 | bq20z75_device->irq = irq; |
| 600 | |
| 601 | skip_gpio: |
| 602 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 603 | rc = power_supply_register(&client->dev, &bq20z75_device->power_supply); |
| 604 | if (rc) { |
| 605 | dev_err(&client->dev, |
| 606 | "%s: Failed to register power supply\n", __func__); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 607 | goto exit_psupply; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | dev_info(&client->dev, |
| 611 | "%s: battery gas gauge device registered\n", client->name); |
| 612 | |
| 613 | return 0; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 614 | |
| 615 | exit_psupply: |
| 616 | if (bq20z75_device->irq) |
| 617 | free_irq(bq20z75_device->irq, &bq20z75_device->power_supply); |
| 618 | if (bq20z75_device->gpio_detect) |
| 619 | gpio_free(pdata->battery_detect); |
| 620 | |
| 621 | kfree(bq20z75_device); |
| 622 | |
| 623 | return rc; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 626 | static int __devexit bq20z75_remove(struct i2c_client *client) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 627 | { |
| 628 | struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); |
| 629 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 630 | if (bq20z75_device->irq) |
| 631 | free_irq(bq20z75_device->irq, &bq20z75_device->power_supply); |
| 632 | if (bq20z75_device->gpio_detect) |
| 633 | gpio_free(bq20z75_device->pdata->battery_detect); |
| 634 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 635 | power_supply_unregister(&bq20z75_device->power_supply); |
| 636 | kfree(bq20z75_device); |
| 637 | bq20z75_device = NULL; |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | #if defined CONFIG_PM |
| 643 | static int bq20z75_suspend(struct i2c_client *client, |
| 644 | pm_message_t state) |
| 645 | { |
| 646 | s32 ret; |
| 647 | |
| 648 | /* write to manufacturer access with sleep command */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 649 | ret = bq20z75_write_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 650 | bq20z75_data[REG_MANUFACTURER_DATA].addr, |
| 651 | MANUFACTURER_ACCESS_SLEEP); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 652 | if (ret < 0) |
| 653 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | #else |
| 658 | #define bq20z75_suspend NULL |
| 659 | #endif |
| 660 | /* any smbus transaction will wake up bq20z75 */ |
| 661 | #define bq20z75_resume NULL |
| 662 | |
| 663 | static const struct i2c_device_id bq20z75_id[] = { |
| 664 | { "bq20z75", 0 }, |
| 665 | {} |
| 666 | }; |
| 667 | |
| 668 | static struct i2c_driver bq20z75_battery_driver = { |
| 669 | .probe = bq20z75_probe, |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 670 | .remove = __devexit_p(bq20z75_remove), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 671 | .suspend = bq20z75_suspend, |
| 672 | .resume = bq20z75_resume, |
| 673 | .id_table = bq20z75_id, |
| 674 | .driver = { |
| 675 | .name = "bq20z75-battery", |
| 676 | }, |
| 677 | }; |
| 678 | |
| 679 | static int __init bq20z75_battery_init(void) |
| 680 | { |
| 681 | return i2c_add_driver(&bq20z75_battery_driver); |
| 682 | } |
| 683 | module_init(bq20z75_battery_init); |
| 684 | |
| 685 | static void __exit bq20z75_battery_exit(void) |
| 686 | { |
| 687 | i2c_del_driver(&bq20z75_battery_driver); |
| 688 | } |
| 689 | module_exit(bq20z75_battery_exit); |
| 690 | |
| 691 | MODULE_DESCRIPTION("BQ20z75 battery monitor driver"); |
| 692 | MODULE_LICENSE("GPL"); |