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