Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1 | /* |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 2 | * Gas Gauge driver for SBS Compliant Batteries |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 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> |
Sachin Kamat | 075ed03 | 2013-03-14 15:49:46 +0530 | [diff] [blame] | 30 | #include <linux/of.h> |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 31 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 32 | #include <linux/power/sbs-battery.h> |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 33 | |
| 34 | enum { |
| 35 | REG_MANUFACTURER_DATA, |
| 36 | REG_TEMPERATURE, |
| 37 | REG_VOLTAGE, |
| 38 | REG_CURRENT, |
| 39 | REG_CAPACITY, |
| 40 | REG_TIME_TO_EMPTY, |
| 41 | REG_TIME_TO_FULL, |
| 42 | REG_STATUS, |
| 43 | REG_CYCLE_COUNT, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 44 | REG_SERIAL_NUMBER, |
| 45 | REG_REMAINING_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 46 | REG_REMAINING_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 47 | REG_FULL_CHARGE_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 48 | REG_FULL_CHARGE_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 49 | REG_DESIGN_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 50 | REG_DESIGN_CAPACITY_CHARGE, |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 51 | REG_DESIGN_VOLTAGE_MIN, |
| 52 | REG_DESIGN_VOLTAGE_MAX, |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 53 | REG_MANUFACTURER, |
| 54 | REG_MODEL_NAME, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 57 | /* Battery Mode defines */ |
| 58 | #define BATTERY_MODE_OFFSET 0x03 |
| 59 | #define BATTERY_MODE_MASK 0x8000 |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 60 | enum sbs_battery_mode { |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 61 | BATTERY_MODE_AMPS, |
| 62 | BATTERY_MODE_WATTS |
| 63 | }; |
| 64 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 65 | /* manufacturer access defines */ |
| 66 | #define MANUFACTURER_ACCESS_STATUS 0x0006 |
| 67 | #define MANUFACTURER_ACCESS_SLEEP 0x0011 |
| 68 | |
| 69 | /* battery status value bits */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 70 | #define BATTERY_DISCHARGING 0x40 |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 71 | #define BATTERY_FULL_CHARGED 0x20 |
| 72 | #define BATTERY_FULL_DISCHARGED 0x10 |
| 73 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 74 | /* min_value and max_value are only valid for numerical data */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 75 | #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \ |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 76 | .psp = _psp, \ |
| 77 | .addr = _addr, \ |
| 78 | .min_value = _min_value, \ |
| 79 | .max_value = _max_value, \ |
| 80 | } |
| 81 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 82 | static const struct chip_data { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 83 | enum power_supply_property psp; |
| 84 | u8 addr; |
| 85 | int min_value; |
| 86 | int max_value; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 87 | } sbs_data[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 88 | [REG_MANUFACTURER_DATA] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 89 | SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 90 | [REG_TEMPERATURE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 91 | SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 92 | [REG_VOLTAGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 93 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 94 | [REG_CURRENT] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 95 | SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 96 | [REG_CAPACITY] = |
Nikolaus Voss | b1f092f | 2012-04-25 08:59:03 +0200 | [diff] [blame] | 97 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 98 | [REG_REMAINING_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 99 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 100 | [REG_REMAINING_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 101 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 102 | [REG_FULL_CHARGE_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 103 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 104 | [REG_FULL_CHARGE_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 105 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 106 | [REG_TIME_TO_EMPTY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 107 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 108 | [REG_TIME_TO_FULL] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 109 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 110 | [REG_STATUS] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 111 | SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 112 | [REG_CYCLE_COUNT] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 113 | SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 114 | [REG_DESIGN_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 115 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 116 | [REG_DESIGN_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 117 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535), |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 118 | [REG_DESIGN_VOLTAGE_MIN] = |
| 119 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535), |
| 120 | [REG_DESIGN_VOLTAGE_MAX] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 121 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 122 | [REG_SERIAL_NUMBER] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 123 | SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 124 | /* Properties of type `const char *' */ |
| 125 | [REG_MANUFACTURER] = |
| 126 | SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535), |
| 127 | [REG_MODEL_NAME] = |
| 128 | SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 131 | static enum power_supply_property sbs_properties[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 132 | POWER_SUPPLY_PROP_STATUS, |
| 133 | POWER_SUPPLY_PROP_HEALTH, |
| 134 | POWER_SUPPLY_PROP_PRESENT, |
| 135 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 136 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 137 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 138 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 139 | POWER_SUPPLY_PROP_CAPACITY, |
| 140 | POWER_SUPPLY_PROP_TEMP, |
| 141 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 142 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 143 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 144 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 145 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 146 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 147 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 148 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 149 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 150 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 151 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 152 | /* Properties of type `const char *' */ |
| 153 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 154 | POWER_SUPPLY_PROP_MODEL_NAME |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 157 | struct sbs_info { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 158 | struct i2c_client *client; |
| 159 | struct power_supply power_supply; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 160 | struct sbs_platform_data *pdata; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 161 | bool is_present; |
| 162 | bool gpio_detect; |
| 163 | bool enable_detection; |
| 164 | int irq; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 165 | int last_state; |
| 166 | int poll_time; |
| 167 | struct delayed_work work; |
| 168 | int ignore_changes; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 171 | static char model_name[I2C_SMBUS_BLOCK_MAX + 1]; |
| 172 | static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1]; |
| 173 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 174 | static int sbs_read_word_data(struct i2c_client *client, u8 address) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 175 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 176 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 177 | s32 ret = 0; |
| 178 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 179 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 180 | if (chip->pdata) |
| 181 | retries = max(chip->pdata->i2c_retry_count + 1, 1); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 182 | |
| 183 | while (retries > 0) { |
| 184 | ret = i2c_smbus_read_word_data(client, address); |
| 185 | if (ret >= 0) |
| 186 | break; |
| 187 | retries--; |
| 188 | } |
| 189 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 190 | if (ret < 0) { |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 191 | dev_dbg(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 192 | "%s: i2c read at address 0x%x failed\n", |
| 193 | __func__, address); |
| 194 | return ret; |
| 195 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 196 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 197 | return le16_to_cpu(ret); |
| 198 | } |
| 199 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 200 | static int sbs_read_string_data(struct i2c_client *client, u8 address, |
| 201 | char *values) |
| 202 | { |
| 203 | struct sbs_info *chip = i2c_get_clientdata(client); |
| 204 | s32 ret = 0, block_length = 0; |
| 205 | int retries_length = 1, retries_block = 1; |
| 206 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; |
| 207 | |
| 208 | if (chip->pdata) { |
| 209 | retries_length = max(chip->pdata->i2c_retry_count + 1, 1); |
| 210 | retries_block = max(chip->pdata->i2c_retry_count + 1, 1); |
| 211 | } |
| 212 | |
| 213 | /* Adapter needs to support these two functions */ |
| 214 | if (!i2c_check_functionality(client->adapter, |
| 215 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 216 | I2C_FUNC_SMBUS_I2C_BLOCK)){ |
| 217 | return -ENODEV; |
| 218 | } |
| 219 | |
| 220 | /* Get the length of block data */ |
| 221 | while (retries_length > 0) { |
| 222 | ret = i2c_smbus_read_byte_data(client, address); |
| 223 | if (ret >= 0) |
| 224 | break; |
| 225 | retries_length--; |
| 226 | } |
| 227 | |
| 228 | if (ret < 0) { |
| 229 | dev_dbg(&client->dev, |
| 230 | "%s: i2c read at address 0x%x failed\n", |
| 231 | __func__, address); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | /* block_length does not include NULL terminator */ |
| 236 | block_length = ret; |
| 237 | if (block_length > I2C_SMBUS_BLOCK_MAX) { |
| 238 | dev_err(&client->dev, |
| 239 | "%s: Returned block_length is longer than 0x%x\n", |
| 240 | __func__, I2C_SMBUS_BLOCK_MAX); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | /* Get the block data */ |
| 245 | while (retries_block > 0) { |
| 246 | ret = i2c_smbus_read_i2c_block_data( |
| 247 | client, address, |
| 248 | block_length + 1, block_buffer); |
| 249 | if (ret >= 0) |
| 250 | break; |
| 251 | retries_block--; |
| 252 | } |
| 253 | |
| 254 | if (ret < 0) { |
| 255 | dev_dbg(&client->dev, |
| 256 | "%s: i2c read at address 0x%x failed\n", |
| 257 | __func__, address); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | /* block_buffer[0] == block_length */ |
| 262 | memcpy(values, block_buffer + 1, block_length); |
| 263 | values[block_length] = '\0'; |
| 264 | |
| 265 | return le16_to_cpu(ret); |
| 266 | } |
| 267 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 268 | static int sbs_write_word_data(struct i2c_client *client, u8 address, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 269 | u16 value) |
| 270 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 271 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 272 | s32 ret = 0; |
| 273 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 274 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 275 | if (chip->pdata) |
| 276 | retries = max(chip->pdata->i2c_retry_count + 1, 1); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 277 | |
| 278 | while (retries > 0) { |
| 279 | ret = i2c_smbus_write_word_data(client, address, |
| 280 | le16_to_cpu(value)); |
| 281 | if (ret >= 0) |
| 282 | break; |
| 283 | retries--; |
| 284 | } |
| 285 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 286 | if (ret < 0) { |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 287 | dev_dbg(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 288 | "%s: i2c write to address 0x%x failed\n", |
| 289 | __func__, address); |
| 290 | return ret; |
| 291 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 292 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 296 | static int sbs_get_battery_presence_and_health( |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 297 | struct i2c_client *client, enum power_supply_property psp, |
| 298 | union power_supply_propval *val) |
| 299 | { |
| 300 | s32 ret; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 301 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 302 | |
| 303 | if (psp == POWER_SUPPLY_PROP_PRESENT && |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 304 | chip->gpio_detect) { |
| 305 | ret = gpio_get_value(chip->pdata->battery_detect); |
| 306 | if (ret == chip->pdata->battery_detect_present) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 307 | val->intval = 1; |
| 308 | else |
| 309 | val->intval = 0; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 310 | chip->is_present = val->intval; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 311 | return ret; |
| 312 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 313 | |
| 314 | /* Write to ManufacturerAccess with |
| 315 | * ManufacturerAccess command and then |
| 316 | * read the status */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 317 | ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
| 318 | MANUFACTURER_ACCESS_STATUS); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 319 | if (ret < 0) { |
| 320 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 321 | val->intval = 0; /* battery removed */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 322 | return ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 323 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 324 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 325 | ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 326 | if (ret < 0) |
| 327 | return ret; |
| 328 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 329 | if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value || |
| 330 | ret > sbs_data[REG_MANUFACTURER_DATA].max_value) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 331 | val->intval = 0; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /* Mask the upper nibble of 2nd byte and |
| 336 | * lower byte of response then |
| 337 | * shift the result by 8 to get status*/ |
| 338 | ret &= 0x0F00; |
| 339 | ret >>= 8; |
| 340 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 341 | if (ret == 0x0F) |
| 342 | /* battery removed */ |
| 343 | val->intval = 0; |
| 344 | else |
| 345 | val->intval = 1; |
| 346 | } else if (psp == POWER_SUPPLY_PROP_HEALTH) { |
| 347 | if (ret == 0x09) |
| 348 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 349 | else if (ret == 0x0B) |
| 350 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 351 | else if (ret == 0x0C) |
| 352 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 353 | else |
| 354 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 360 | static int sbs_get_battery_property(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 361 | int reg_offset, enum power_supply_property psp, |
| 362 | union power_supply_propval *val) |
| 363 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 364 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 365 | s32 ret; |
| 366 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 367 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 368 | if (ret < 0) |
| 369 | return ret; |
| 370 | |
| 371 | /* returned values are 16 bit */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 372 | if (sbs_data[reg_offset].min_value < 0) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 373 | ret = (s16)ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 374 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 375 | if (ret >= sbs_data[reg_offset].min_value && |
| 376 | ret <= sbs_data[reg_offset].max_value) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 377 | val->intval = ret; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 378 | if (psp != POWER_SUPPLY_PROP_STATUS) |
| 379 | return 0; |
| 380 | |
| 381 | if (ret & BATTERY_FULL_CHARGED) |
| 382 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 383 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 384 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 385 | else if (ret & BATTERY_DISCHARGING) |
| 386 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 387 | else |
| 388 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 389 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 390 | if (chip->poll_time == 0) |
| 391 | chip->last_state = val->intval; |
| 392 | else if (chip->last_state != val->intval) { |
| 393 | cancel_delayed_work_sync(&chip->work); |
| 394 | power_supply_changed(&chip->power_supply); |
| 395 | chip->poll_time = 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 396 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 397 | } else { |
| 398 | if (psp == POWER_SUPPLY_PROP_STATUS) |
| 399 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 400 | else |
| 401 | val->intval = 0; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 407 | static int sbs_get_battery_string_property(struct i2c_client *client, |
| 408 | int reg_offset, enum power_supply_property psp, char *val) |
| 409 | { |
| 410 | s32 ret; |
| 411 | |
| 412 | ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val); |
| 413 | |
| 414 | if (ret < 0) |
| 415 | return ret; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 420 | static void sbs_unit_adjustment(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 421 | enum power_supply_property psp, union power_supply_propval *val) |
| 422 | { |
| 423 | #define BASE_UNIT_CONVERSION 1000 |
| 424 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 425 | #define TIME_UNIT_CONVERSION 60 |
| 426 | #define TEMP_KELVIN_TO_CELSIUS 2731 |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 427 | switch (psp) { |
| 428 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 429 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 430 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 431 | /* sbs provides energy in units of 10mWh. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 432 | * Convert to µWh |
| 433 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 434 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
| 435 | break; |
| 436 | |
| 437 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 438 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 439 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 440 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 441 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 442 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 443 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 444 | val->intval *= BASE_UNIT_CONVERSION; |
| 445 | break; |
| 446 | |
| 447 | case POWER_SUPPLY_PROP_TEMP: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 448 | /* sbs provides battery temperature in 0.1K |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 449 | * so convert it to 0.1°C |
| 450 | */ |
| 451 | val->intval -= TEMP_KELVIN_TO_CELSIUS; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 452 | break; |
| 453 | |
| 454 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 455 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 456 | /* sbs provides time to empty and time to full in minutes. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 457 | * Convert to seconds |
| 458 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 459 | val->intval *= TIME_UNIT_CONVERSION; |
| 460 | break; |
| 461 | |
| 462 | default: |
| 463 | dev_dbg(&client->dev, |
| 464 | "%s: no need for unit conversion %d\n", __func__, psp); |
| 465 | } |
| 466 | } |
| 467 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 468 | static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client, |
| 469 | enum sbs_battery_mode mode) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 470 | { |
| 471 | int ret, original_val; |
| 472 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 473 | original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 474 | if (original_val < 0) |
| 475 | return original_val; |
| 476 | |
| 477 | if ((original_val & BATTERY_MODE_MASK) == mode) |
| 478 | return mode; |
| 479 | |
| 480 | if (mode == BATTERY_MODE_AMPS) |
| 481 | ret = original_val & ~BATTERY_MODE_MASK; |
| 482 | else |
| 483 | ret = original_val | BATTERY_MODE_MASK; |
| 484 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 485 | ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 486 | if (ret < 0) |
| 487 | return ret; |
| 488 | |
| 489 | return original_val & BATTERY_MODE_MASK; |
| 490 | } |
| 491 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 492 | static int sbs_get_battery_capacity(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 493 | int reg_offset, enum power_supply_property psp, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 494 | union power_supply_propval *val) |
| 495 | { |
| 496 | s32 ret; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 497 | enum sbs_battery_mode mode = BATTERY_MODE_WATTS; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 498 | |
| 499 | if (power_supply_is_amp_property(psp)) |
| 500 | mode = BATTERY_MODE_AMPS; |
| 501 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 502 | mode = sbs_set_battery_mode(client, mode); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 503 | if (mode < 0) |
| 504 | return mode; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 505 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 506 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 507 | if (ret < 0) |
| 508 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 509 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 510 | if (psp == POWER_SUPPLY_PROP_CAPACITY) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 511 | /* sbs spec says that this can be >100 % |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 512 | * even if max value is 100 % */ |
| 513 | val->intval = min(ret, 100); |
| 514 | } else |
| 515 | val->intval = ret; |
| 516 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 517 | ret = sbs_set_battery_mode(client, mode); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 518 | if (ret < 0) |
| 519 | return ret; |
| 520 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 524 | static char sbs_serial[5]; |
| 525 | static int sbs_get_battery_serial_number(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 526 | union power_supply_propval *val) |
| 527 | { |
| 528 | int ret; |
| 529 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 530 | ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 531 | if (ret < 0) |
| 532 | return ret; |
| 533 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 534 | ret = sprintf(sbs_serial, "%04x", ret); |
| 535 | val->strval = sbs_serial; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 540 | static int sbs_get_property_index(struct i2c_client *client, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 541 | enum power_supply_property psp) |
| 542 | { |
| 543 | int count; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 544 | for (count = 0; count < ARRAY_SIZE(sbs_data); count++) |
| 545 | if (psp == sbs_data[count].psp) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 546 | return count; |
| 547 | |
| 548 | dev_warn(&client->dev, |
| 549 | "%s: Invalid Property - %d\n", __func__, psp); |
| 550 | |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 554 | static int sbs_get_property(struct power_supply *psy, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 555 | enum power_supply_property psp, |
| 556 | union power_supply_propval *val) |
| 557 | { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 558 | int ret = 0; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 559 | struct sbs_info *chip = container_of(psy, |
| 560 | struct sbs_info, power_supply); |
| 561 | struct i2c_client *client = chip->client; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 562 | |
| 563 | switch (psp) { |
| 564 | case POWER_SUPPLY_PROP_PRESENT: |
| 565 | case POWER_SUPPLY_PROP_HEALTH: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 566 | ret = sbs_get_battery_presence_and_health(client, psp, val); |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 567 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 568 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 572 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
Nikolaus Voss | 5da5098 | 2012-05-09 08:30:44 +0200 | [diff] [blame] | 573 | goto done; /* don't trigger power_supply_changed()! */ |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 574 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 575 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 576 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 577 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 578 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 579 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 580 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 581 | case POWER_SUPPLY_PROP_CAPACITY: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 582 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 583 | if (ret < 0) |
| 584 | break; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 585 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 586 | ret = sbs_get_battery_capacity(client, ret, psp, val); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 587 | break; |
| 588 | |
| 589 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 590 | ret = sbs_get_battery_serial_number(client, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 591 | break; |
| 592 | |
| 593 | case POWER_SUPPLY_PROP_STATUS: |
| 594 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 595 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 596 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 597 | case POWER_SUPPLY_PROP_TEMP: |
| 598 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 599 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 600 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 601 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 602 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 603 | if (ret < 0) |
| 604 | break; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 605 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 606 | ret = sbs_get_battery_property(client, ret, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 607 | break; |
| 608 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 609 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 610 | ret = sbs_get_property_index(client, psp); |
| 611 | if (ret < 0) |
| 612 | break; |
| 613 | |
| 614 | ret = sbs_get_battery_string_property(client, ret, psp, |
| 615 | model_name); |
| 616 | val->strval = model_name; |
| 617 | break; |
| 618 | |
| 619 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 620 | ret = sbs_get_property_index(client, psp); |
| 621 | if (ret < 0) |
| 622 | break; |
| 623 | |
| 624 | ret = sbs_get_battery_string_property(client, ret, psp, |
| 625 | manufacturer); |
| 626 | val->strval = manufacturer; |
| 627 | break; |
| 628 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 629 | default: |
| 630 | dev_err(&client->dev, |
| 631 | "%s: INVALID property\n", __func__); |
| 632 | return -EINVAL; |
| 633 | } |
| 634 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 635 | if (!chip->enable_detection) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 636 | goto done; |
| 637 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 638 | if (!chip->gpio_detect && |
| 639 | chip->is_present != (ret >= 0)) { |
| 640 | chip->is_present = (ret >= 0); |
| 641 | power_supply_changed(&chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | done: |
| 645 | if (!ret) { |
| 646 | /* Convert units to match requirements for power supply class */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 647 | sbs_unit_adjustment(client, psp, val); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 648 | } |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 649 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 650 | dev_dbg(&client->dev, |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 651 | "%s: property = %d, value = %x\n", __func__, psp, val->intval); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 652 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 653 | if (ret && chip->is_present) |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 654 | return ret; |
| 655 | |
| 656 | /* battery not present, so return NODATA for properties */ |
| 657 | if (ret) |
| 658 | return -ENODATA; |
| 659 | |
| 660 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 663 | static irqreturn_t sbs_irq(int irq, void *devid) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 664 | { |
| 665 | struct power_supply *battery = devid; |
| 666 | |
| 667 | power_supply_changed(battery); |
| 668 | |
| 669 | return IRQ_HANDLED; |
| 670 | } |
| 671 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 672 | static void sbs_external_power_changed(struct power_supply *psy) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 673 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 674 | struct sbs_info *chip; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 675 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 676 | chip = container_of(psy, struct sbs_info, power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 677 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 678 | if (chip->ignore_changes > 0) { |
| 679 | chip->ignore_changes--; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
| 683 | /* cancel outstanding work */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 684 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 685 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 686 | schedule_delayed_work(&chip->work, HZ); |
| 687 | chip->poll_time = chip->pdata->poll_retry_count; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 690 | static void sbs_delayed_work(struct work_struct *work) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 691 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 692 | struct sbs_info *chip; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 693 | s32 ret; |
| 694 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 695 | chip = container_of(work, struct sbs_info, work.work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 696 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 697 | ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 698 | /* if the read failed, give up on this work */ |
| 699 | if (ret < 0) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 700 | chip->poll_time = 0; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 701 | return; |
| 702 | } |
| 703 | |
| 704 | if (ret & BATTERY_FULL_CHARGED) |
| 705 | ret = POWER_SUPPLY_STATUS_FULL; |
| 706 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 707 | ret = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 708 | else if (ret & BATTERY_DISCHARGING) |
| 709 | ret = POWER_SUPPLY_STATUS_DISCHARGING; |
| 710 | else |
| 711 | ret = POWER_SUPPLY_STATUS_CHARGING; |
| 712 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 713 | if (chip->last_state != ret) { |
| 714 | chip->poll_time = 0; |
| 715 | power_supply_changed(&chip->power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 716 | return; |
| 717 | } |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 718 | if (chip->poll_time > 0) { |
| 719 | schedule_delayed_work(&chip->work, HZ); |
| 720 | chip->poll_time--; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | } |
| 724 | |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 725 | #if defined(CONFIG_OF) |
| 726 | |
| 727 | #include <linux/of_device.h> |
| 728 | #include <linux/of_gpio.h> |
| 729 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 730 | static const struct of_device_id sbs_dt_ids[] = { |
| 731 | { .compatible = "sbs,sbs-battery" }, |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 732 | { .compatible = "ti,bq20z75" }, |
| 733 | { } |
| 734 | }; |
Olof Johansson | 62df393 | 2012-01-06 05:45:34 +0400 | [diff] [blame] | 735 | MODULE_DEVICE_TABLE(of, sbs_dt_ids); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 736 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 737 | static struct sbs_platform_data *sbs_of_populate_pdata( |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 738 | struct i2c_client *client) |
| 739 | { |
| 740 | struct device_node *of_node = client->dev.of_node; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 741 | struct sbs_platform_data *pdata = client->dev.platform_data; |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 742 | enum of_gpio_flags gpio_flags; |
| 743 | int rc; |
| 744 | u32 prop; |
| 745 | |
| 746 | /* verify this driver matches this device */ |
| 747 | if (!of_node) |
| 748 | return NULL; |
| 749 | |
| 750 | /* if platform data is set, honor it */ |
| 751 | if (pdata) |
| 752 | return pdata; |
| 753 | |
| 754 | /* first make sure at least one property is set, otherwise |
| 755 | * it won't change behavior from running without pdata. |
| 756 | */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 757 | if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) && |
| 758 | !of_get_property(of_node, "sbs,poll-retry-count", NULL) && |
| 759 | !of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 760 | goto of_out; |
| 761 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 762 | pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data), |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 763 | GFP_KERNEL); |
| 764 | if (!pdata) |
| 765 | goto of_out; |
| 766 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 767 | rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 768 | if (!rc) |
| 769 | pdata->i2c_retry_count = prop; |
| 770 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 771 | rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 772 | if (!rc) |
| 773 | pdata->poll_retry_count = prop; |
| 774 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 775 | if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) { |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 776 | pdata->battery_detect = -1; |
| 777 | goto of_out; |
| 778 | } |
| 779 | |
| 780 | pdata->battery_detect = of_get_named_gpio_flags(of_node, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 781 | "sbs,battery-detect-gpios", 0, &gpio_flags); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 782 | |
| 783 | if (gpio_flags & OF_GPIO_ACTIVE_LOW) |
| 784 | pdata->battery_detect_present = 0; |
| 785 | else |
| 786 | pdata->battery_detect_present = 1; |
| 787 | |
| 788 | of_out: |
| 789 | return pdata; |
| 790 | } |
| 791 | #else |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 792 | static struct sbs_platform_data *sbs_of_populate_pdata( |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 793 | struct i2c_client *client) |
| 794 | { |
| 795 | return client->dev.platform_data; |
| 796 | } |
| 797 | #endif |
| 798 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 799 | static int sbs_probe(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 800 | const struct i2c_device_id *id) |
| 801 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 802 | struct sbs_info *chip; |
| 803 | struct sbs_platform_data *pdata = client->dev.platform_data; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 804 | int rc; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 805 | int irq; |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 806 | char *name; |
| 807 | |
| 808 | name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev)); |
| 809 | if (!name) { |
| 810 | dev_err(&client->dev, "Failed to allocate device name\n"); |
| 811 | return -ENOMEM; |
| 812 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 813 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 814 | chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL); |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 815 | if (!chip) { |
| 816 | rc = -ENOMEM; |
| 817 | goto exit_free_name; |
| 818 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 819 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 820 | chip->client = client; |
| 821 | chip->enable_detection = false; |
| 822 | chip->gpio_detect = false; |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 823 | chip->power_supply.name = name; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 824 | chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY; |
| 825 | chip->power_supply.properties = sbs_properties; |
| 826 | chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties); |
| 827 | chip->power_supply.get_property = sbs_get_property; |
Rhyland Klein | 89a22b1 | 2013-06-10 17:26:40 -0400 | [diff] [blame] | 828 | chip->power_supply.of_node = client->dev.of_node; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 829 | /* ignore first notification of external change, it is generated |
| 830 | * from the power_supply_register call back |
| 831 | */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 832 | chip->ignore_changes = 1; |
| 833 | chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; |
| 834 | chip->power_supply.external_power_changed = sbs_external_power_changed; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 835 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 836 | pdata = sbs_of_populate_pdata(client); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 837 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 838 | if (pdata) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 839 | chip->gpio_detect = gpio_is_valid(pdata->battery_detect); |
| 840 | chip->pdata = pdata; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 843 | i2c_set_clientdata(client, chip); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 844 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 845 | if (!chip->gpio_detect) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 846 | goto skip_gpio; |
| 847 | |
| 848 | rc = gpio_request(pdata->battery_detect, dev_name(&client->dev)); |
| 849 | if (rc) { |
| 850 | dev_warn(&client->dev, "Failed to request gpio: %d\n", rc); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 851 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 852 | goto skip_gpio; |
| 853 | } |
| 854 | |
| 855 | rc = gpio_direction_input(pdata->battery_detect); |
| 856 | if (rc) { |
| 857 | dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc); |
| 858 | gpio_free(pdata->battery_detect); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 859 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 860 | goto skip_gpio; |
| 861 | } |
| 862 | |
| 863 | irq = gpio_to_irq(pdata->battery_detect); |
| 864 | if (irq <= 0) { |
| 865 | dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); |
| 866 | gpio_free(pdata->battery_detect); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 867 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 868 | goto skip_gpio; |
| 869 | } |
| 870 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 871 | rc = request_irq(irq, sbs_irq, |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 872 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 873 | dev_name(&client->dev), &chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 874 | if (rc) { |
| 875 | dev_warn(&client->dev, "Failed to request irq: %d\n", rc); |
| 876 | gpio_free(pdata->battery_detect); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 877 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 878 | goto skip_gpio; |
| 879 | } |
| 880 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 881 | chip->irq = irq; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 882 | |
| 883 | skip_gpio: |
Olof Johansson | a22b41a | 2012-09-06 11:32:29 -0700 | [diff] [blame] | 884 | /* |
| 885 | * Before we register, we need to make sure we can actually talk |
| 886 | * to the battery. |
| 887 | */ |
| 888 | rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); |
| 889 | if (rc < 0) { |
| 890 | dev_err(&client->dev, "%s: Failed to get device status\n", |
| 891 | __func__); |
| 892 | goto exit_psupply; |
| 893 | } |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 894 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 895 | rc = power_supply_register(&client->dev, &chip->power_supply); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 896 | if (rc) { |
| 897 | dev_err(&client->dev, |
| 898 | "%s: Failed to register power supply\n", __func__); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 899 | goto exit_psupply; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | dev_info(&client->dev, |
| 903 | "%s: battery gas gauge device registered\n", client->name); |
| 904 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 905 | INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 906 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 907 | chip->enable_detection = true; |
Rhyland Klein | ee177d9 | 2011-05-24 12:06:50 -0700 | [diff] [blame] | 908 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 909 | return 0; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 910 | |
| 911 | exit_psupply: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 912 | if (chip->irq) |
| 913 | free_irq(chip->irq, &chip->power_supply); |
| 914 | if (chip->gpio_detect) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 915 | gpio_free(pdata->battery_detect); |
| 916 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 917 | kfree(chip); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 918 | |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 919 | exit_free_name: |
| 920 | kfree(name); |
| 921 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 922 | return rc; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 925 | static int sbs_remove(struct i2c_client *client) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 926 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 927 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 928 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 929 | if (chip->irq) |
| 930 | free_irq(chip->irq, &chip->power_supply); |
| 931 | if (chip->gpio_detect) |
| 932 | gpio_free(chip->pdata->battery_detect); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 933 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 934 | power_supply_unregister(&chip->power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 935 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 936 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 937 | |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 938 | kfree(chip->power_supply.name); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 939 | kfree(chip); |
| 940 | chip = NULL; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 945 | #if defined CONFIG_PM_SLEEP |
| 946 | |
| 947 | static int sbs_suspend(struct device *dev) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 948 | { |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 949 | struct i2c_client *client = to_i2c_client(dev); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 950 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 951 | s32 ret; |
| 952 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 953 | if (chip->poll_time > 0) |
| 954 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 955 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 956 | /* write to manufacturer access with sleep command */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 957 | ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 958 | MANUFACTURER_ACCESS_SLEEP); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 959 | if (chip->is_present && ret < 0) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 960 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 961 | |
| 962 | return 0; |
| 963 | } |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 964 | |
| 965 | static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); |
| 966 | #define SBS_PM_OPS (&sbs_pm_ops) |
| 967 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 968 | #else |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 969 | #define SBS_PM_OPS NULL |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 970 | #endif |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 971 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 972 | static const struct i2c_device_id sbs_id[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 973 | { "bq20z75", 0 }, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 974 | { "sbs-battery", 1 }, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 975 | {} |
| 976 | }; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 977 | MODULE_DEVICE_TABLE(i2c, sbs_id); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 978 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 979 | static struct i2c_driver sbs_battery_driver = { |
| 980 | .probe = sbs_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 981 | .remove = sbs_remove, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 982 | .id_table = sbs_id, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 983 | .driver = { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 984 | .name = "sbs-battery", |
Sachin Kamat | 075ed03 | 2013-03-14 15:49:46 +0530 | [diff] [blame] | 985 | .of_match_table = of_match_ptr(sbs_dt_ids), |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 986 | .pm = SBS_PM_OPS, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 987 | }, |
| 988 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 989 | module_i2c_driver(sbs_battery_driver); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 990 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 991 | MODULE_DESCRIPTION("SBS battery monitor driver"); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 992 | MODULE_LICENSE("GPL"); |