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> |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 29 | #include <linux/gpio/consumer.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 | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 166 | bool is_present; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 167 | struct gpio_desc *gpio_detect; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 168 | bool enable_detection; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 169 | int last_state; |
| 170 | int poll_time; |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 171 | u32 i2c_retry_count; |
| 172 | u32 poll_retry_count; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 173 | struct delayed_work work; |
| 174 | int ignore_changes; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 177 | static char model_name[I2C_SMBUS_BLOCK_MAX + 1]; |
| 178 | static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1]; |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 179 | static bool force_load; |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 180 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 181 | static int sbs_read_word_data(struct i2c_client *client, u8 address) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 182 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 183 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 184 | s32 ret = 0; |
| 185 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 186 | |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 187 | retries = chip->i2c_retry_count; |
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 | |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 214 | retries_length = chip->i2c_retry_count; |
| 215 | retries_block = chip->i2c_retry_count; |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 216 | |
| 217 | /* Adapter needs to support these two functions */ |
| 218 | if (!i2c_check_functionality(client->adapter, |
| 219 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 220 | I2C_FUNC_SMBUS_I2C_BLOCK)){ |
| 221 | return -ENODEV; |
| 222 | } |
| 223 | |
| 224 | /* Get the length of block data */ |
| 225 | while (retries_length > 0) { |
| 226 | ret = i2c_smbus_read_byte_data(client, address); |
| 227 | if (ret >= 0) |
| 228 | break; |
| 229 | retries_length--; |
| 230 | } |
| 231 | |
| 232 | if (ret < 0) { |
| 233 | dev_dbg(&client->dev, |
| 234 | "%s: i2c read at address 0x%x failed\n", |
| 235 | __func__, address); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | /* block_length does not include NULL terminator */ |
| 240 | block_length = ret; |
| 241 | if (block_length > I2C_SMBUS_BLOCK_MAX) { |
| 242 | dev_err(&client->dev, |
| 243 | "%s: Returned block_length is longer than 0x%x\n", |
| 244 | __func__, I2C_SMBUS_BLOCK_MAX); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | /* Get the block data */ |
| 249 | while (retries_block > 0) { |
| 250 | ret = i2c_smbus_read_i2c_block_data( |
| 251 | client, address, |
| 252 | block_length + 1, block_buffer); |
| 253 | if (ret >= 0) |
| 254 | break; |
| 255 | retries_block--; |
| 256 | } |
| 257 | |
| 258 | if (ret < 0) { |
| 259 | dev_dbg(&client->dev, |
| 260 | "%s: i2c read at address 0x%x failed\n", |
| 261 | __func__, address); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /* block_buffer[0] == block_length */ |
| 266 | memcpy(values, block_buffer + 1, block_length); |
| 267 | values[block_length] = '\0'; |
| 268 | |
| 269 | return le16_to_cpu(ret); |
| 270 | } |
| 271 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 272 | static int sbs_write_word_data(struct i2c_client *client, u8 address, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 273 | u16 value) |
| 274 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 275 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 276 | s32 ret = 0; |
| 277 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 278 | |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 279 | retries = chip->i2c_retry_count; |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 280 | |
| 281 | while (retries > 0) { |
| 282 | ret = i2c_smbus_write_word_data(client, address, |
| 283 | le16_to_cpu(value)); |
| 284 | if (ret >= 0) |
| 285 | break; |
| 286 | retries--; |
| 287 | } |
| 288 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 289 | if (ret < 0) { |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 290 | dev_dbg(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 291 | "%s: i2c write to address 0x%x failed\n", |
| 292 | __func__, address); |
| 293 | return ret; |
| 294 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 295 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 299 | static int sbs_get_battery_presence_and_health( |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 300 | struct i2c_client *client, enum power_supply_property psp, |
| 301 | union power_supply_propval *val) |
| 302 | { |
| 303 | s32 ret; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 304 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 305 | |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 306 | if (psp == POWER_SUPPLY_PROP_PRESENT && chip->gpio_detect) { |
| 307 | ret = gpiod_get_value_cansleep(chip->gpio_detect); |
| 308 | if (ret < 0) |
| 309 | return ret; |
| 310 | val->intval = ret; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 311 | chip->is_present = val->intval; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 312 | return ret; |
| 313 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 314 | |
Guenter Roeck | 17c6d39 | 2016-09-08 19:10:00 -0700 | [diff] [blame] | 315 | /* |
| 316 | * Write to ManufacturerAccess with ManufacturerAccess command |
| 317 | * and then read the status. Do not check for error on the write |
| 318 | * since not all batteries implement write access to this command, |
| 319 | * while others mandate it. |
| 320 | */ |
| 321 | sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
| 322 | MANUFACTURER_ACCESS_STATUS); |
| 323 | |
| 324 | ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); |
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 | 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; |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 380 | if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) { |
| 381 | if (!(ret & BATTERY_INITIALIZED)) |
| 382 | val->intval = |
| 383 | POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; |
| 384 | else if (ret & BATTERY_FULL_CHARGED) |
| 385 | val->intval = |
| 386 | POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 387 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 388 | val->intval = |
| 389 | POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 390 | else |
| 391 | val->intval = |
| 392 | POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 393 | return 0; |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 394 | } else if (psp != POWER_SUPPLY_PROP_STATUS) { |
| 395 | return 0; |
| 396 | } |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 397 | |
| 398 | if (ret & BATTERY_FULL_CHARGED) |
| 399 | val->intval = POWER_SUPPLY_STATUS_FULL; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 400 | else if (ret & BATTERY_DISCHARGING) |
| 401 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 402 | else |
| 403 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 404 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 405 | if (chip->poll_time == 0) |
| 406 | chip->last_state = val->intval; |
| 407 | else if (chip->last_state != val->intval) { |
| 408 | cancel_delayed_work_sync(&chip->work); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 409 | power_supply_changed(chip->power_supply); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 410 | chip->poll_time = 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 411 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 412 | } else { |
| 413 | if (psp == POWER_SUPPLY_PROP_STATUS) |
| 414 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 415 | else |
| 416 | val->intval = 0; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 422 | static int sbs_get_battery_string_property(struct i2c_client *client, |
| 423 | int reg_offset, enum power_supply_property psp, char *val) |
| 424 | { |
| 425 | s32 ret; |
| 426 | |
| 427 | ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val); |
| 428 | |
| 429 | if (ret < 0) |
| 430 | return ret; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 435 | static void sbs_unit_adjustment(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 436 | enum power_supply_property psp, union power_supply_propval *val) |
| 437 | { |
| 438 | #define BASE_UNIT_CONVERSION 1000 |
| 439 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 440 | #define TIME_UNIT_CONVERSION 60 |
| 441 | #define TEMP_KELVIN_TO_CELSIUS 2731 |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 442 | switch (psp) { |
| 443 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 444 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 445 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 446 | /* sbs provides energy in units of 10mWh. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 447 | * Convert to µWh |
| 448 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 449 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
| 450 | break; |
| 451 | |
| 452 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 453 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 454 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 455 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 456 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 457 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 458 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 459 | val->intval *= BASE_UNIT_CONVERSION; |
| 460 | break; |
| 461 | |
| 462 | case POWER_SUPPLY_PROP_TEMP: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 463 | /* sbs provides battery temperature in 0.1K |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 464 | * so convert it to 0.1°C |
| 465 | */ |
| 466 | val->intval -= TEMP_KELVIN_TO_CELSIUS; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 467 | break; |
| 468 | |
| 469 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 470 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 471 | /* sbs provides time to empty and time to full in minutes. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 472 | * Convert to seconds |
| 473 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 474 | val->intval *= TIME_UNIT_CONVERSION; |
| 475 | break; |
| 476 | |
| 477 | default: |
| 478 | dev_dbg(&client->dev, |
| 479 | "%s: no need for unit conversion %d\n", __func__, psp); |
| 480 | } |
| 481 | } |
| 482 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 483 | static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client, |
| 484 | enum sbs_battery_mode mode) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 485 | { |
| 486 | int ret, original_val; |
| 487 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 488 | original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 489 | if (original_val < 0) |
| 490 | return original_val; |
| 491 | |
| 492 | if ((original_val & BATTERY_MODE_MASK) == mode) |
| 493 | return mode; |
| 494 | |
| 495 | if (mode == BATTERY_MODE_AMPS) |
| 496 | ret = original_val & ~BATTERY_MODE_MASK; |
| 497 | else |
| 498 | ret = original_val | BATTERY_MODE_MASK; |
| 499 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 500 | ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 501 | if (ret < 0) |
| 502 | return ret; |
| 503 | |
| 504 | return original_val & BATTERY_MODE_MASK; |
| 505 | } |
| 506 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 507 | static int sbs_get_battery_capacity(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 508 | int reg_offset, enum power_supply_property psp, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 509 | union power_supply_propval *val) |
| 510 | { |
| 511 | s32 ret; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 512 | enum sbs_battery_mode mode = BATTERY_MODE_WATTS; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 513 | |
| 514 | if (power_supply_is_amp_property(psp)) |
| 515 | mode = BATTERY_MODE_AMPS; |
| 516 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 517 | mode = sbs_set_battery_mode(client, mode); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 518 | if (mode < 0) |
| 519 | return mode; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 520 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 521 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 522 | if (ret < 0) |
| 523 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 524 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 525 | if (psp == POWER_SUPPLY_PROP_CAPACITY) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 526 | /* sbs spec says that this can be >100 % |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 527 | * even if max value is 100 % */ |
| 528 | val->intval = min(ret, 100); |
| 529 | } else |
| 530 | val->intval = ret; |
| 531 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 532 | ret = sbs_set_battery_mode(client, mode); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 533 | if (ret < 0) |
| 534 | return ret; |
| 535 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 539 | static char sbs_serial[5]; |
| 540 | static int sbs_get_battery_serial_number(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 541 | union power_supply_propval *val) |
| 542 | { |
| 543 | int ret; |
| 544 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 545 | ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 546 | if (ret < 0) |
| 547 | return ret; |
| 548 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 549 | ret = sprintf(sbs_serial, "%04x", ret); |
| 550 | val->strval = sbs_serial; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 555 | static int sbs_get_property_index(struct i2c_client *client, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 556 | enum power_supply_property psp) |
| 557 | { |
| 558 | int count; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 559 | for (count = 0; count < ARRAY_SIZE(sbs_data); count++) |
| 560 | if (psp == sbs_data[count].psp) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 561 | return count; |
| 562 | |
| 563 | dev_warn(&client->dev, |
| 564 | "%s: Invalid Property - %d\n", __func__, psp); |
| 565 | |
| 566 | return -EINVAL; |
| 567 | } |
| 568 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 569 | static int sbs_get_property(struct power_supply *psy, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 570 | enum power_supply_property psp, |
| 571 | union power_supply_propval *val) |
| 572 | { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 573 | int ret = 0; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 574 | struct sbs_info *chip = power_supply_get_drvdata(psy); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 575 | struct i2c_client *client = chip->client; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 576 | |
| 577 | switch (psp) { |
| 578 | case POWER_SUPPLY_PROP_PRESENT: |
| 579 | case POWER_SUPPLY_PROP_HEALTH: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 580 | ret = sbs_get_battery_presence_and_health(client, psp, val); |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 581 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 582 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 583 | break; |
| 584 | |
| 585 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 586 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
Nikolaus Voss | 5da5098 | 2012-05-09 08:30:44 +0200 | [diff] [blame] | 587 | goto done; /* don't trigger power_supply_changed()! */ |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 588 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 589 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 590 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 591 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 592 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 593 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 594 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 595 | case POWER_SUPPLY_PROP_CAPACITY: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 596 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 597 | if (ret < 0) |
| 598 | break; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 599 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 600 | ret = sbs_get_battery_capacity(client, ret, psp, val); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 601 | break; |
| 602 | |
| 603 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 604 | ret = sbs_get_battery_serial_number(client, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 605 | break; |
| 606 | |
| 607 | case POWER_SUPPLY_PROP_STATUS: |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 608 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 609 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 610 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 611 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 612 | case POWER_SUPPLY_PROP_TEMP: |
| 613 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 614 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 615 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 616 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 617 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 618 | if (ret < 0) |
| 619 | break; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 620 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 621 | ret = sbs_get_battery_property(client, ret, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 622 | break; |
| 623 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 624 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 625 | ret = sbs_get_property_index(client, psp); |
| 626 | if (ret < 0) |
| 627 | break; |
| 628 | |
| 629 | ret = sbs_get_battery_string_property(client, ret, psp, |
| 630 | model_name); |
| 631 | val->strval = model_name; |
| 632 | break; |
| 633 | |
| 634 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 635 | ret = sbs_get_property_index(client, psp); |
| 636 | if (ret < 0) |
| 637 | break; |
| 638 | |
| 639 | ret = sbs_get_battery_string_property(client, ret, psp, |
| 640 | manufacturer); |
| 641 | val->strval = manufacturer; |
| 642 | break; |
| 643 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 644 | default: |
| 645 | dev_err(&client->dev, |
| 646 | "%s: INVALID property\n", __func__); |
| 647 | return -EINVAL; |
| 648 | } |
| 649 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 650 | if (!chip->enable_detection) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 651 | goto done; |
| 652 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 653 | if (!chip->gpio_detect && |
| 654 | chip->is_present != (ret >= 0)) { |
| 655 | chip->is_present = (ret >= 0); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 656 | power_supply_changed(chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | done: |
| 660 | if (!ret) { |
| 661 | /* Convert units to match requirements for power supply class */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 662 | sbs_unit_adjustment(client, psp, val); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 663 | } |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 664 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 665 | dev_dbg(&client->dev, |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 666 | "%s: property = %d, value = %x\n", __func__, psp, val->intval); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 667 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 668 | if (ret && chip->is_present) |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 669 | return ret; |
| 670 | |
| 671 | /* battery not present, so return NODATA for properties */ |
| 672 | if (ret) |
| 673 | return -ENODATA; |
| 674 | |
| 675 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 678 | static irqreturn_t sbs_irq(int irq, void *devid) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 679 | { |
Phil Reid | d2cec82 | 2016-07-25 10:42:58 +0800 | [diff] [blame] | 680 | struct sbs_info *chip = devid; |
| 681 | struct power_supply *battery = chip->power_supply; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 682 | int ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 683 | |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 684 | ret = gpiod_get_value_cansleep(chip->gpio_detect); |
| 685 | if (ret < 0) |
| 686 | return ret; |
| 687 | chip->is_present = ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 688 | power_supply_changed(battery); |
| 689 | |
| 690 | return IRQ_HANDLED; |
| 691 | } |
| 692 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 693 | static void sbs_external_power_changed(struct power_supply *psy) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 694 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 695 | struct sbs_info *chip = power_supply_get_drvdata(psy); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 696 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 697 | if (chip->ignore_changes > 0) { |
| 698 | chip->ignore_changes--; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 699 | return; |
| 700 | } |
| 701 | |
| 702 | /* cancel outstanding work */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 703 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 704 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 705 | schedule_delayed_work(&chip->work, HZ); |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 706 | chip->poll_time = chip->poll_retry_count; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 709 | static void sbs_delayed_work(struct work_struct *work) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 710 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 711 | struct sbs_info *chip; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 712 | s32 ret; |
| 713 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 714 | chip = container_of(work, struct sbs_info, work.work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 715 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 716 | ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 717 | /* if the read failed, give up on this work */ |
| 718 | if (ret < 0) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 719 | chip->poll_time = 0; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | |
| 723 | if (ret & BATTERY_FULL_CHARGED) |
| 724 | ret = POWER_SUPPLY_STATUS_FULL; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 725 | else if (ret & BATTERY_DISCHARGING) |
| 726 | ret = POWER_SUPPLY_STATUS_DISCHARGING; |
| 727 | else |
| 728 | ret = POWER_SUPPLY_STATUS_CHARGING; |
| 729 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 730 | if (chip->last_state != ret) { |
| 731 | chip->poll_time = 0; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 732 | power_supply_changed(chip->power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 733 | return; |
| 734 | } |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 735 | if (chip->poll_time > 0) { |
| 736 | schedule_delayed_work(&chip->work, HZ); |
| 737 | chip->poll_time--; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 738 | return; |
| 739 | } |
| 740 | } |
| 741 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 742 | static const struct power_supply_desc sbs_default_desc = { |
| 743 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 744 | .properties = sbs_properties, |
| 745 | .num_properties = ARRAY_SIZE(sbs_properties), |
| 746 | .get_property = sbs_get_property, |
| 747 | .external_power_changed = sbs_external_power_changed, |
| 748 | }; |
| 749 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 750 | static int sbs_probe(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 751 | const struct i2c_device_id *id) |
| 752 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 753 | struct sbs_info *chip; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 754 | struct power_supply_desc *sbs_desc; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 755 | struct sbs_platform_data *pdata = client->dev.platform_data; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 756 | struct power_supply_config psy_cfg = {}; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 757 | int rc; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 758 | int irq; |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 759 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 760 | sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc, |
| 761 | sizeof(*sbs_desc), GFP_KERNEL); |
| 762 | if (!sbs_desc) |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 763 | return -ENOMEM; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 764 | |
| 765 | sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s", |
| 766 | dev_name(&client->dev)); |
| 767 | if (!sbs_desc->name) |
| 768 | return -ENOMEM; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 769 | |
Phil Reid | 9239a86 | 2016-07-25 10:42:57 +0800 | [diff] [blame] | 770 | chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 771 | if (!chip) |
| 772 | return -ENOMEM; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 773 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 774 | chip->client = client; |
| 775 | chip->enable_detection = false; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 776 | psy_cfg.of_node = client->dev.of_node; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 777 | psy_cfg.drv_data = chip; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 778 | /* ignore first notification of external change, it is generated |
| 779 | * from the power_supply_register call back |
| 780 | */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 781 | chip->ignore_changes = 1; |
| 782 | chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 783 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 784 | /* use pdata if available, fall back to DT properties, |
| 785 | * or hardcoded defaults if not |
| 786 | */ |
| 787 | rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count", |
| 788 | &chip->i2c_retry_count); |
| 789 | if (rc) |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 790 | chip->i2c_retry_count = 0; |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 791 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 792 | rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count", |
| 793 | &chip->poll_retry_count); |
| 794 | if (rc) |
| 795 | chip->poll_retry_count = 0; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 796 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 797 | if (pdata) { |
| 798 | chip->poll_retry_count = pdata->poll_retry_count; |
| 799 | chip->i2c_retry_count = pdata->i2c_retry_count; |
| 800 | } |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 801 | chip->i2c_retry_count = chip->i2c_retry_count + 1; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 802 | |
| 803 | chip->gpio_detect = devm_gpiod_get_optional(&client->dev, |
| 804 | "sbs,battery-detect", GPIOD_IN); |
| 805 | if (IS_ERR(chip->gpio_detect)) { |
| 806 | dev_err(&client->dev, "Failed to get gpio: %ld\n", |
| 807 | PTR_ERR(chip->gpio_detect)); |
| 808 | return PTR_ERR(chip->gpio_detect); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 811 | i2c_set_clientdata(client, chip); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 812 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 813 | if (!chip->gpio_detect) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 814 | goto skip_gpio; |
| 815 | |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 816 | irq = gpiod_to_irq(chip->gpio_detect); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 817 | if (irq <= 0) { |
| 818 | dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 819 | goto skip_gpio; |
| 820 | } |
| 821 | |
Phil Reid | d2cec82 | 2016-07-25 10:42:58 +0800 | [diff] [blame] | 822 | rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq, |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 823 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
Phil Reid | d2cec82 | 2016-07-25 10:42:58 +0800 | [diff] [blame] | 824 | dev_name(&client->dev), chip); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 825 | if (rc) { |
| 826 | dev_warn(&client->dev, "Failed to request irq: %d\n", rc); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 827 | goto skip_gpio; |
| 828 | } |
| 829 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 830 | skip_gpio: |
Olof Johansson | a22b41a | 2012-09-06 11:32:29 -0700 | [diff] [blame] | 831 | /* |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 832 | * 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] | 833 | * to the battery. |
| 834 | */ |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 835 | if (!(force_load || chip->gpio_detect)) { |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 836 | rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); |
| 837 | |
| 838 | if (rc < 0) { |
| 839 | dev_err(&client->dev, "%s: Failed to get device status\n", |
| 840 | __func__); |
| 841 | goto exit_psupply; |
| 842 | } |
Olof Johansson | a22b41a | 2012-09-06 11:32:29 -0700 | [diff] [blame] | 843 | } |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 844 | |
Phil Reid | 492ff9d8 | 2016-07-25 10:42:59 +0800 | [diff] [blame] | 845 | chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc, |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 846 | &psy_cfg); |
| 847 | if (IS_ERR(chip->power_supply)) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 848 | dev_err(&client->dev, |
| 849 | "%s: Failed to register power supply\n", __func__); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 850 | rc = PTR_ERR(chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 851 | goto exit_psupply; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | dev_info(&client->dev, |
| 855 | "%s: battery gas gauge device registered\n", client->name); |
| 856 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 857 | INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 858 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 859 | chip->enable_detection = true; |
Rhyland Klein | ee177d9 | 2011-05-24 12:06:50 -0700 | [diff] [blame] | 860 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 861 | return 0; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 862 | |
| 863 | exit_psupply: |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 864 | return rc; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 867 | static int sbs_remove(struct i2c_client *client) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 868 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 869 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 870 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 871 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 872 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 876 | #if defined CONFIG_PM_SLEEP |
| 877 | |
| 878 | static int sbs_suspend(struct device *dev) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 879 | { |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 880 | struct i2c_client *client = to_i2c_client(dev); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 881 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 882 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 883 | if (chip->poll_time > 0) |
| 884 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 885 | |
Guenter Roeck | 17c6d39 | 2016-09-08 19:10:00 -0700 | [diff] [blame] | 886 | /* |
| 887 | * Write to manufacturer access with sleep command. |
| 888 | * Support is manufacturer dependend, so ignore errors. |
| 889 | */ |
| 890 | sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 891 | MANUFACTURER_ACCESS_SLEEP); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 892 | |
| 893 | return 0; |
| 894 | } |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 895 | |
| 896 | static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); |
| 897 | #define SBS_PM_OPS (&sbs_pm_ops) |
| 898 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 899 | #else |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 900 | #define SBS_PM_OPS NULL |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 901 | #endif |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 902 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 903 | static const struct i2c_device_id sbs_id[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 904 | { "bq20z75", 0 }, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 905 | { "sbs-battery", 1 }, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 906 | {} |
| 907 | }; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 908 | MODULE_DEVICE_TABLE(i2c, sbs_id); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 909 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 910 | static const struct of_device_id sbs_dt_ids[] = { |
| 911 | { .compatible = "sbs,sbs-battery" }, |
| 912 | { .compatible = "ti,bq20z75" }, |
| 913 | { } |
| 914 | }; |
| 915 | MODULE_DEVICE_TABLE(of, sbs_dt_ids); |
| 916 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 917 | static struct i2c_driver sbs_battery_driver = { |
| 918 | .probe = sbs_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 919 | .remove = sbs_remove, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 920 | .id_table = sbs_id, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 921 | .driver = { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 922 | .name = "sbs-battery", |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 923 | .of_match_table = sbs_dt_ids, |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 924 | .pm = SBS_PM_OPS, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 925 | }, |
| 926 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 927 | module_i2c_driver(sbs_battery_driver); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 928 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 929 | MODULE_DESCRIPTION("SBS battery monitor driver"); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 930 | MODULE_LICENSE("GPL"); |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 931 | |
| 932 | module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH); |
| 933 | MODULE_PARM_DESC(force_load, |
| 934 | "Attempt to load the driver even if no battery is connected"); |