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