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