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> |
| 28 | |
| 29 | enum { |
| 30 | REG_MANUFACTURER_DATA, |
| 31 | REG_TEMPERATURE, |
| 32 | REG_VOLTAGE, |
| 33 | REG_CURRENT, |
| 34 | REG_CAPACITY, |
| 35 | REG_TIME_TO_EMPTY, |
| 36 | REG_TIME_TO_FULL, |
| 37 | REG_STATUS, |
| 38 | REG_CYCLE_COUNT, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 39 | REG_SERIAL_NUMBER, |
| 40 | REG_REMAINING_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 41 | REG_REMAINING_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 42 | REG_FULL_CHARGE_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 43 | REG_FULL_CHARGE_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 44 | REG_DESIGN_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 45 | REG_DESIGN_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 46 | REG_DESIGN_VOLTAGE, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 49 | /* Battery Mode defines */ |
| 50 | #define BATTERY_MODE_OFFSET 0x03 |
| 51 | #define BATTERY_MODE_MASK 0x8000 |
| 52 | enum bq20z75_battery_mode { |
| 53 | BATTERY_MODE_AMPS, |
| 54 | BATTERY_MODE_WATTS |
| 55 | }; |
| 56 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 57 | /* manufacturer access defines */ |
| 58 | #define MANUFACTURER_ACCESS_STATUS 0x0006 |
| 59 | #define MANUFACTURER_ACCESS_SLEEP 0x0011 |
| 60 | |
| 61 | /* battery status value bits */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 62 | #define BATTERY_DISCHARGING 0x40 |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 63 | #define BATTERY_FULL_CHARGED 0x20 |
| 64 | #define BATTERY_FULL_DISCHARGED 0x10 |
| 65 | |
| 66 | #define BQ20Z75_DATA(_psp, _addr, _min_value, _max_value) { \ |
| 67 | .psp = _psp, \ |
| 68 | .addr = _addr, \ |
| 69 | .min_value = _min_value, \ |
| 70 | .max_value = _max_value, \ |
| 71 | } |
| 72 | |
| 73 | static const struct bq20z75_device_data { |
| 74 | enum power_supply_property psp; |
| 75 | u8 addr; |
| 76 | int min_value; |
| 77 | int max_value; |
| 78 | } bq20z75_data[] = { |
| 79 | [REG_MANUFACTURER_DATA] = |
| 80 | BQ20Z75_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), |
| 81 | [REG_TEMPERATURE] = |
| 82 | BQ20Z75_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), |
| 83 | [REG_VOLTAGE] = |
| 84 | BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000), |
| 85 | [REG_CURRENT] = |
| 86 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, |
| 87 | 32767), |
| 88 | [REG_CAPACITY] = |
| 89 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 90 | [REG_REMAINING_CAPACITY] = |
| 91 | BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 92 | [REG_REMAINING_CAPACITY_CHARGE] = |
| 93 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 94 | [REG_FULL_CHARGE_CAPACITY] = |
| 95 | BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 96 | [REG_FULL_CHARGE_CAPACITY_CHARGE] = |
| 97 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 98 | [REG_TIME_TO_EMPTY] = |
| 99 | BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, |
| 100 | 65535), |
| 101 | [REG_TIME_TO_FULL] = |
| 102 | BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, |
| 103 | 65535), |
| 104 | [REG_STATUS] = |
| 105 | BQ20Z75_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), |
| 106 | [REG_CYCLE_COUNT] = |
| 107 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 108 | [REG_DESIGN_CAPACITY] = |
| 109 | BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, |
| 110 | 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 111 | [REG_DESIGN_CAPACITY_CHARGE] = |
| 112 | BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, |
| 113 | 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 114 | [REG_DESIGN_VOLTAGE] = |
| 115 | BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, |
| 116 | 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 117 | [REG_SERIAL_NUMBER] = |
| 118 | BQ20Z75_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), |
| 119 | }; |
| 120 | |
| 121 | static enum power_supply_property bq20z75_properties[] = { |
| 122 | POWER_SUPPLY_PROP_STATUS, |
| 123 | POWER_SUPPLY_PROP_HEALTH, |
| 124 | POWER_SUPPLY_PROP_PRESENT, |
| 125 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 126 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 127 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 128 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 129 | POWER_SUPPLY_PROP_CAPACITY, |
| 130 | POWER_SUPPLY_PROP_TEMP, |
| 131 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 132 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 133 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 134 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 135 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 136 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 137 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 138 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 139 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 140 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | struct bq20z75_info { |
| 144 | struct i2c_client *client; |
| 145 | struct power_supply power_supply; |
| 146 | }; |
| 147 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 148 | static int bq20z75_read_word_data(struct i2c_client *client, u8 address) |
| 149 | { |
| 150 | s32 ret; |
| 151 | |
| 152 | ret = i2c_smbus_read_word_data(client, address); |
| 153 | if (ret < 0) { |
| 154 | dev_err(&client->dev, |
| 155 | "%s: i2c read at address 0x%x failed\n", |
| 156 | __func__, address); |
| 157 | return ret; |
| 158 | } |
| 159 | return le16_to_cpu(ret); |
| 160 | } |
| 161 | |
| 162 | static int bq20z75_write_word_data(struct i2c_client *client, u8 address, |
| 163 | u16 value) |
| 164 | { |
| 165 | s32 ret; |
| 166 | |
| 167 | ret = i2c_smbus_write_word_data(client, address, le16_to_cpu(value)); |
| 168 | if (ret < 0) { |
| 169 | dev_err(&client->dev, |
| 170 | "%s: i2c write to address 0x%x failed\n", |
| 171 | __func__, address); |
| 172 | return ret; |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 177 | static int bq20z75_get_battery_presence_and_health( |
| 178 | struct i2c_client *client, enum power_supply_property psp, |
| 179 | union power_supply_propval *val) |
| 180 | { |
| 181 | s32 ret; |
| 182 | |
| 183 | /* Write to ManufacturerAccess with |
| 184 | * ManufacturerAccess command and then |
| 185 | * read the status */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 186 | ret = bq20z75_write_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 187 | bq20z75_data[REG_MANUFACTURER_DATA].addr, |
| 188 | MANUFACTURER_ACCESS_STATUS); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 189 | if (ret < 0) |
| 190 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 191 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 192 | |
| 193 | ret = bq20z75_read_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 194 | bq20z75_data[REG_MANUFACTURER_DATA].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 195 | if (ret < 0) |
| 196 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 197 | |
| 198 | if (ret < bq20z75_data[REG_MANUFACTURER_DATA].min_value || |
| 199 | ret > bq20z75_data[REG_MANUFACTURER_DATA].max_value) { |
| 200 | val->intval = 0; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /* Mask the upper nibble of 2nd byte and |
| 205 | * lower byte of response then |
| 206 | * shift the result by 8 to get status*/ |
| 207 | ret &= 0x0F00; |
| 208 | ret >>= 8; |
| 209 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 210 | if (ret == 0x0F) |
| 211 | /* battery removed */ |
| 212 | val->intval = 0; |
| 213 | else |
| 214 | val->intval = 1; |
| 215 | } else if (psp == POWER_SUPPLY_PROP_HEALTH) { |
| 216 | if (ret == 0x09) |
| 217 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 218 | else if (ret == 0x0B) |
| 219 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 220 | else if (ret == 0x0C) |
| 221 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 222 | else |
| 223 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int bq20z75_get_battery_property(struct i2c_client *client, |
| 230 | int reg_offset, enum power_supply_property psp, |
| 231 | union power_supply_propval *val) |
| 232 | { |
| 233 | s32 ret; |
| 234 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 235 | ret = bq20z75_read_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 236 | bq20z75_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 237 | if (ret < 0) |
| 238 | return ret; |
| 239 | |
| 240 | /* returned values are 16 bit */ |
| 241 | if (bq20z75_data[reg_offset].min_value < 0) |
| 242 | ret = (s16)ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 243 | |
| 244 | if (ret >= bq20z75_data[reg_offset].min_value && |
| 245 | ret <= bq20z75_data[reg_offset].max_value) { |
| 246 | val->intval = ret; |
| 247 | if (psp == POWER_SUPPLY_PROP_STATUS) { |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 248 | if (ret & BATTERY_FULL_CHARGED) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 249 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 250 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 251 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 252 | else if (ret & BATTERY_DISCHARGING) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 253 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 254 | else |
| 255 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 256 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 257 | } else { |
| 258 | if (psp == POWER_SUPPLY_PROP_STATUS) |
| 259 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 260 | else |
| 261 | val->intval = 0; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 267 | static void bq20z75_unit_adjustment(struct i2c_client *client, |
| 268 | enum power_supply_property psp, union power_supply_propval *val) |
| 269 | { |
| 270 | #define BASE_UNIT_CONVERSION 1000 |
| 271 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) |
| 272 | #define TIME_UNIT_CONVERSION 600 |
| 273 | #define TEMP_KELVIN_TO_CELCIUS 2731 |
| 274 | switch (psp) { |
| 275 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 276 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 277 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| 278 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
| 279 | break; |
| 280 | |
| 281 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 282 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 283 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 284 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 285 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 286 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 287 | val->intval *= BASE_UNIT_CONVERSION; |
| 288 | break; |
| 289 | |
| 290 | case POWER_SUPPLY_PROP_TEMP: |
| 291 | /* bq20z75 provides battery tempreture in 0.1°K |
| 292 | * so convert it to 0.1°C */ |
| 293 | val->intval -= TEMP_KELVIN_TO_CELCIUS; |
| 294 | val->intval *= 10; |
| 295 | break; |
| 296 | |
| 297 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 298 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
| 299 | val->intval *= TIME_UNIT_CONVERSION; |
| 300 | break; |
| 301 | |
| 302 | default: |
| 303 | dev_dbg(&client->dev, |
| 304 | "%s: no need for unit conversion %d\n", __func__, psp); |
| 305 | } |
| 306 | } |
| 307 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 308 | static enum bq20z75_battery_mode |
| 309 | bq20z75_set_battery_mode(struct i2c_client *client, |
| 310 | enum bq20z75_battery_mode mode) |
| 311 | { |
| 312 | int ret, original_val; |
| 313 | |
| 314 | original_val = bq20z75_read_word_data(client, BATTERY_MODE_OFFSET); |
| 315 | if (original_val < 0) |
| 316 | return original_val; |
| 317 | |
| 318 | if ((original_val & BATTERY_MODE_MASK) == mode) |
| 319 | return mode; |
| 320 | |
| 321 | if (mode == BATTERY_MODE_AMPS) |
| 322 | ret = original_val & ~BATTERY_MODE_MASK; |
| 323 | else |
| 324 | ret = original_val | BATTERY_MODE_MASK; |
| 325 | |
| 326 | ret = bq20z75_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
| 327 | if (ret < 0) |
| 328 | return ret; |
| 329 | |
| 330 | return original_val & BATTERY_MODE_MASK; |
| 331 | } |
| 332 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 333 | static int bq20z75_get_battery_capacity(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 334 | int reg_offset, enum power_supply_property psp, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 335 | union power_supply_propval *val) |
| 336 | { |
| 337 | s32 ret; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 338 | enum bq20z75_battery_mode mode = BATTERY_MODE_WATTS; |
| 339 | |
| 340 | if (power_supply_is_amp_property(psp)) |
| 341 | mode = BATTERY_MODE_AMPS; |
| 342 | |
| 343 | mode = bq20z75_set_battery_mode(client, mode); |
| 344 | if (mode < 0) |
| 345 | return mode; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 346 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 347 | ret = bq20z75_read_word_data(client, bq20z75_data[reg_offset].addr); |
| 348 | if (ret < 0) |
| 349 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 350 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 351 | if (psp == POWER_SUPPLY_PROP_CAPACITY) { |
| 352 | /* bq20z75 spec says that this can be >100 % |
| 353 | * even if max value is 100 % */ |
| 354 | val->intval = min(ret, 100); |
| 355 | } else |
| 356 | val->intval = ret; |
| 357 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 358 | ret = bq20z75_set_battery_mode(client, mode); |
| 359 | if (ret < 0) |
| 360 | return ret; |
| 361 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static char bq20z75_serial[5]; |
| 366 | static int bq20z75_get_battery_serial_number(struct i2c_client *client, |
| 367 | union power_supply_propval *val) |
| 368 | { |
| 369 | int ret; |
| 370 | |
| 371 | ret = bq20z75_read_word_data(client, |
| 372 | bq20z75_data[REG_SERIAL_NUMBER].addr); |
| 373 | if (ret < 0) |
| 374 | return ret; |
| 375 | |
| 376 | ret = sprintf(bq20z75_serial, "%04x", ret); |
| 377 | val->strval = bq20z75_serial; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 382 | static int bq20z75_get_property_index(struct i2c_client *client, |
| 383 | enum power_supply_property psp) |
| 384 | { |
| 385 | int count; |
| 386 | for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) |
| 387 | if (psp == bq20z75_data[count].psp) |
| 388 | return count; |
| 389 | |
| 390 | dev_warn(&client->dev, |
| 391 | "%s: Invalid Property - %d\n", __func__, psp); |
| 392 | |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 396 | static int bq20z75_get_property(struct power_supply *psy, |
| 397 | enum power_supply_property psp, |
| 398 | union power_supply_propval *val) |
| 399 | { |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 400 | int ps_index; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 401 | int ret; |
| 402 | struct bq20z75_info *bq20z75_device = container_of(psy, |
| 403 | struct bq20z75_info, power_supply); |
| 404 | struct i2c_client *client = bq20z75_device->client; |
| 405 | |
| 406 | switch (psp) { |
| 407 | case POWER_SUPPLY_PROP_PRESENT: |
| 408 | case POWER_SUPPLY_PROP_HEALTH: |
| 409 | ret = bq20z75_get_battery_presence_and_health(client, psp, val); |
| 410 | if (ret) |
| 411 | return ret; |
| 412 | break; |
| 413 | |
| 414 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 415 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 416 | break; |
| 417 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 418 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 419 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 420 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 421 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 422 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 423 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 424 | case POWER_SUPPLY_PROP_CAPACITY: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 425 | ps_index = bq20z75_get_property_index(client, psp); |
| 426 | if (ps_index < 0) |
| 427 | return ps_index; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 428 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 429 | ret = bq20z75_get_battery_capacity(client, ps_index, psp, val); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 430 | if (ret) |
| 431 | return ret; |
| 432 | |
| 433 | break; |
| 434 | |
| 435 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 436 | ret = bq20z75_get_battery_serial_number(client, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 437 | if (ret) |
| 438 | return ret; |
| 439 | break; |
| 440 | |
| 441 | case POWER_SUPPLY_PROP_STATUS: |
| 442 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 443 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 444 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 445 | case POWER_SUPPLY_PROP_TEMP: |
| 446 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 447 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 448 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 449 | ps_index = bq20z75_get_property_index(client, psp); |
| 450 | if (ps_index < 0) |
| 451 | return ps_index; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 452 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame^] | 453 | ret = bq20z75_get_battery_property(client, ps_index, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 454 | if (ret) |
| 455 | return ret; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 456 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | default: |
| 460 | dev_err(&client->dev, |
| 461 | "%s: INVALID property\n", __func__); |
| 462 | return -EINVAL; |
| 463 | } |
| 464 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 465 | /* Convert units to match requirements for power supply class */ |
| 466 | bq20z75_unit_adjustment(client, psp, val); |
| 467 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 468 | dev_dbg(&client->dev, |
| 469 | "%s: property = %d, value = %d\n", __func__, psp, val->intval); |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static int bq20z75_probe(struct i2c_client *client, |
| 475 | const struct i2c_device_id *id) |
| 476 | { |
| 477 | struct bq20z75_info *bq20z75_device; |
| 478 | int rc; |
| 479 | |
| 480 | bq20z75_device = kzalloc(sizeof(struct bq20z75_info), GFP_KERNEL); |
| 481 | if (!bq20z75_device) |
| 482 | return -ENOMEM; |
| 483 | |
| 484 | bq20z75_device->client = client; |
| 485 | bq20z75_device->power_supply.name = "battery"; |
| 486 | bq20z75_device->power_supply.type = POWER_SUPPLY_TYPE_BATTERY; |
| 487 | bq20z75_device->power_supply.properties = bq20z75_properties; |
| 488 | bq20z75_device->power_supply.num_properties = |
| 489 | ARRAY_SIZE(bq20z75_properties); |
| 490 | bq20z75_device->power_supply.get_property = bq20z75_get_property; |
| 491 | |
| 492 | i2c_set_clientdata(client, bq20z75_device); |
| 493 | |
| 494 | rc = power_supply_register(&client->dev, &bq20z75_device->power_supply); |
| 495 | if (rc) { |
| 496 | dev_err(&client->dev, |
| 497 | "%s: Failed to register power supply\n", __func__); |
| 498 | kfree(bq20z75_device); |
| 499 | return rc; |
| 500 | } |
| 501 | |
| 502 | dev_info(&client->dev, |
| 503 | "%s: battery gas gauge device registered\n", client->name); |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int bq20z75_remove(struct i2c_client *client) |
| 509 | { |
| 510 | struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); |
| 511 | |
| 512 | power_supply_unregister(&bq20z75_device->power_supply); |
| 513 | kfree(bq20z75_device); |
| 514 | bq20z75_device = NULL; |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | #if defined CONFIG_PM |
| 520 | static int bq20z75_suspend(struct i2c_client *client, |
| 521 | pm_message_t state) |
| 522 | { |
| 523 | s32 ret; |
| 524 | |
| 525 | /* write to manufacturer access with sleep command */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 526 | ret = bq20z75_write_word_data(client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 527 | bq20z75_data[REG_MANUFACTURER_DATA].addr, |
| 528 | MANUFACTURER_ACCESS_SLEEP); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 529 | if (ret < 0) |
| 530 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | #else |
| 535 | #define bq20z75_suspend NULL |
| 536 | #endif |
| 537 | /* any smbus transaction will wake up bq20z75 */ |
| 538 | #define bq20z75_resume NULL |
| 539 | |
| 540 | static const struct i2c_device_id bq20z75_id[] = { |
| 541 | { "bq20z75", 0 }, |
| 542 | {} |
| 543 | }; |
| 544 | |
| 545 | static struct i2c_driver bq20z75_battery_driver = { |
| 546 | .probe = bq20z75_probe, |
| 547 | .remove = bq20z75_remove, |
| 548 | .suspend = bq20z75_suspend, |
| 549 | .resume = bq20z75_resume, |
| 550 | .id_table = bq20z75_id, |
| 551 | .driver = { |
| 552 | .name = "bq20z75-battery", |
| 553 | }, |
| 554 | }; |
| 555 | |
| 556 | static int __init bq20z75_battery_init(void) |
| 557 | { |
| 558 | return i2c_add_driver(&bq20z75_battery_driver); |
| 559 | } |
| 560 | module_init(bq20z75_battery_init); |
| 561 | |
| 562 | static void __exit bq20z75_battery_exit(void) |
| 563 | { |
| 564 | i2c_del_driver(&bq20z75_battery_driver); |
| 565 | } |
| 566 | module_exit(bq20z75_battery_exit); |
| 567 | |
| 568 | MODULE_DESCRIPTION("BQ20z75 battery monitor driver"); |
| 569 | MODULE_LICENSE("GPL"); |