blob: e6f61baa9bedea32a5b8b08be242e1850fe3bc09 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rhyland Kleina7640bf2010-09-05 15:31:23 -07002/*
Rhyland Klein3ddca0622011-12-05 17:50:46 -08003 * Gas Gauge driver for SBS Compliant Batteries
Rhyland Kleina7640bf2010-09-05 15:31:23 -07004 *
5 * Copyright (c) 2010, NVIDIA Corporation.
Rhyland Kleina7640bf2010-09-05 15:31:23 -07006 */
7
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -04008#include <linux/bits.h>
Phil Reidadcf04c2017-07-11 17:43:43 +08009#include <linux/delay.h>
Rhyland Kleina7640bf2010-09-05 15:31:23 -070010#include <linux/err.h>
Phil Reid3b5dd3a2016-09-01 15:50:52 +080011#include <linux/gpio/consumer.h>
Phil Reidb70f0a22017-07-11 17:43:42 +080012#include <linux/i2c.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
Sachin Kamat075ed032013-03-14 15:49:46 +053017#include <linux/of.h>
Brian Norris76b16f4c2018-06-12 13:20:41 -070018#include <linux/of_device.h>
Rhyland Klein3ddca0622011-12-05 17:50:46 -080019#include <linux/power/sbs-battery.h>
Phil Reidb70f0a22017-07-11 17:43:42 +080020#include <linux/power_supply.h>
21#include <linux/slab.h>
22#include <linux/stat.h>
Rhyland Kleina7640bf2010-09-05 15:31:23 -070023
24enum {
25 REG_MANUFACTURER_DATA,
26 REG_TEMPERATURE,
27 REG_VOLTAGE,
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +020028 REG_CURRENT_NOW,
29 REG_CURRENT_AVG,
Sebastian Reicheld6f56322020-05-13 20:56:02 +020030 REG_MAX_ERR,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070031 REG_CAPACITY,
32 REG_TIME_TO_EMPTY,
33 REG_TIME_TO_FULL,
34 REG_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -070035 REG_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070036 REG_CYCLE_COUNT,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070037 REG_SERIAL_NUMBER,
38 REG_REMAINING_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080039 REG_REMAINING_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070040 REG_FULL_CHARGE_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080041 REG_FULL_CHARGE_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070042 REG_DESIGN_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080043 REG_DESIGN_CAPACITY_CHARGE,
Simon Que4495b0a2014-08-04 13:47:46 +020044 REG_DESIGN_VOLTAGE_MIN,
45 REG_DESIGN_VOLTAGE_MAX,
Sebastian Reichel3e9544f2020-05-13 20:56:06 +020046 REG_CHEMISTRY,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020047 REG_MANUFACTURER,
48 REG_MODEL_NAME,
Sebastian Reichel787fdbc2020-05-13 20:56:07 +020049 REG_CHARGE_CURRENT,
50 REG_CHARGE_VOLTAGE,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070051};
52
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +020053#define REG_ADDR_SPEC_INFO 0x1A
54#define SPEC_INFO_VERSION_MASK GENMASK(7, 4)
55#define SPEC_INFO_VERSION_SHIFT 4
56
57#define SBS_VERSION_1_0 1
58#define SBS_VERSION_1_1 2
59#define SBS_VERSION_1_1_WITH_PEC 3
60
Rhyland Klein51d07562011-01-25 11:10:06 -080061/* Battery Mode defines */
62#define BATTERY_MODE_OFFSET 0x03
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -040063#define BATTERY_MODE_CAPACITY_MASK BIT(15)
64enum sbs_capacity_mode {
65 CAPACITY_MODE_AMPS = 0,
66 CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
Rhyland Klein51d07562011-01-25 11:10:06 -080067};
68
Rhyland Kleina7640bf2010-09-05 15:31:23 -070069/* manufacturer access defines */
70#define MANUFACTURER_ACCESS_STATUS 0x0006
71#define MANUFACTURER_ACCESS_SLEEP 0x0011
72
73/* battery status value bits */
Joshua Clayton957cb722016-08-11 09:59:12 -070074#define BATTERY_INITIALIZED 0x80
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070075#define BATTERY_DISCHARGING 0x40
Rhyland Kleina7640bf2010-09-05 15:31:23 -070076#define BATTERY_FULL_CHARGED 0x20
77#define BATTERY_FULL_DISCHARGED 0x10
78
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020079/* min_value and max_value are only valid for numerical data */
Rhyland Klein3ddca0622011-12-05 17:50:46 -080080#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
Rhyland Kleina7640bf2010-09-05 15:31:23 -070081 .psp = _psp, \
82 .addr = _addr, \
83 .min_value = _min_value, \
84 .max_value = _max_value, \
85}
86
Rhyland Klein3ddca0622011-12-05 17:50:46 -080087static const struct chip_data {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070088 enum power_supply_property psp;
89 u8 addr;
90 int min_value;
91 int max_value;
Rhyland Klein3ddca0622011-12-05 17:50:46 -080092} sbs_data[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070093 [REG_MANUFACTURER_DATA] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080094 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070095 [REG_TEMPERATURE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080096 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070097 [REG_VOLTAGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080098 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +020099 [REG_CURRENT_NOW] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800100 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200101 [REG_CURRENT_AVG] =
102 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
Sebastian Reicheld6f56322020-05-13 20:56:02 +0200103 [REG_MAX_ERR] =
104 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700105 [REG_CAPACITY] =
Nikolaus Vossb1f092f2012-04-25 08:59:03 +0200106 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700107 [REG_REMAINING_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800108 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800109 [REG_REMAINING_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800110 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700111 [REG_FULL_CHARGE_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800112 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800113 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800114 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700115 [REG_TIME_TO_EMPTY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800116 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700117 [REG_TIME_TO_FULL] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800118 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200119 [REG_CHARGE_CURRENT] =
120 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535),
121 [REG_CHARGE_VOLTAGE] =
122 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700123 [REG_STATUS] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800124 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
Joshua Clayton957cb722016-08-11 09:59:12 -0700125 [REG_CAPACITY_LEVEL] =
126 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700127 [REG_CYCLE_COUNT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800128 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700129 [REG_DESIGN_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800130 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800131 [REG_DESIGN_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800132 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
Simon Que4495b0a2014-08-04 13:47:46 +0200133 [REG_DESIGN_VOLTAGE_MIN] =
134 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
135 [REG_DESIGN_VOLTAGE_MAX] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800136 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700137 [REG_SERIAL_NUMBER] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800138 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200139 /* Properties of type `const char *' */
140 [REG_MANUFACTURER] =
141 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
142 [REG_MODEL_NAME] =
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200143 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535),
144 [REG_CHEMISTRY] =
145 SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700146};
147
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800148static enum power_supply_property sbs_properties[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700149 POWER_SUPPLY_PROP_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -0700150 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700151 POWER_SUPPLY_PROP_HEALTH,
152 POWER_SUPPLY_PROP_PRESENT,
153 POWER_SUPPLY_PROP_TECHNOLOGY,
154 POWER_SUPPLY_PROP_CYCLE_COUNT,
155 POWER_SUPPLY_PROP_VOLTAGE_NOW,
156 POWER_SUPPLY_PROP_CURRENT_NOW,
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200157 POWER_SUPPLY_PROP_CURRENT_AVG,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700158 POWER_SUPPLY_PROP_CAPACITY,
Sebastian Reicheld6f56322020-05-13 20:56:02 +0200159 POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700160 POWER_SUPPLY_PROP_TEMP,
161 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
162 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
163 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Simon Que4495b0a2014-08-04 13:47:46 +0200164 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700165 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
166 POWER_SUPPLY_PROP_ENERGY_NOW,
167 POWER_SUPPLY_PROP_ENERGY_FULL,
168 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
Rhyland Klein51d07562011-01-25 11:10:06 -0800169 POWER_SUPPLY_PROP_CHARGE_NOW,
170 POWER_SUPPLY_PROP_CHARGE_FULL,
171 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200172 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
173 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200174 /* Properties of type `const char *' */
175 POWER_SUPPLY_PROP_MANUFACTURER,
176 POWER_SUPPLY_PROP_MODEL_NAME
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700177};
178
Sebastian Reichel0ff96912020-05-13 20:56:01 +0200179/* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
180#define SBS_FLAGS_TI_BQ20ZX5 BIT(0)
Brian Norris76b16f4c2018-06-12 13:20:41 -0700181
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800182struct sbs_info {
Rhyland Kleinbb879102011-02-28 16:55:28 -0800183 struct i2c_client *client;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100184 struct power_supply *power_supply;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800185 bool is_present;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800186 struct gpio_desc *gpio_detect;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800187 bool enable_detection;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700188 int last_state;
189 int poll_time;
Phil Reid389958b2016-09-20 09:01:12 +0800190 u32 i2c_retry_count;
191 u32 poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700192 struct delayed_work work;
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700193 struct mutex mode_lock;
Brian Norris76b16f4c2018-06-12 13:20:41 -0700194 u32 flags;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700195};
196
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200197static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
198static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200199static char chemistry[I2C_SMBUS_BLOCK_MAX + 1];
Frans Klaverf4ed9502015-06-10 14:16:56 +0200200static bool force_load;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200201
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200202static int sbs_update_presence(struct sbs_info *chip, bool is_present)
203{
204 struct i2c_client *client = chip->client;
205 int retries = chip->i2c_retry_count;
206 s32 ret = 0;
207 u8 version;
208
209 if (chip->is_present == is_present)
210 return 0;
211
212 if (!is_present) {
213 chip->is_present = false;
214 /* Disable PEC when no device is present */
215 client->flags &= ~I2C_CLIENT_PEC;
216 return 0;
217 }
218
219 /* Check if device supports packet error checking and use it */
220 while (retries > 0) {
221 ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO);
222 if (ret >= 0)
223 break;
224
225 /*
226 * Some batteries trigger the detection pin before the
227 * I2C bus is properly connected. This works around the
228 * issue.
229 */
230 msleep(100);
231
232 retries--;
233 }
234
235 if (ret < 0) {
236 dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);
237
238 /* fallback to old behaviour */
239 client->flags &= ~I2C_CLIENT_PEC;
240 chip->is_present = true;
241
242 return ret;
243 }
244
245 version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT;
246
247 if (version == SBS_VERSION_1_1_WITH_PEC)
248 client->flags |= I2C_CLIENT_PEC;
249 else
250 client->flags &= ~I2C_CLIENT_PEC;
251
252 dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ?
253 "enabled" : "disabled");
254
255 chip->is_present = true;
256
257 return 0;
258}
259
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800260static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700261{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800262 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200263 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800264 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800265
266 while (retries > 0) {
267 ret = i2c_smbus_read_word_data(client, address);
268 if (ret >= 0)
269 break;
270 retries--;
271 }
272
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700273 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800274 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700275 "%s: i2c read at address 0x%x failed\n",
276 __func__, address);
277 return ret;
278 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800279
Phil Reida1bbec72017-06-15 21:59:29 +0800280 return ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700281}
282
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200283static int sbs_read_string_data(struct i2c_client *client, u8 address,
284 char *values)
285{
286 struct sbs_info *chip = i2c_get_clientdata(client);
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200287 int retries = chip->i2c_retry_count;
288 s32 ret = 0;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200289
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200290 if (!i2c_check_functionality(client->adapter,
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200291 I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200292 return -ENODEV;
293 }
294
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200295 /* Get the block data */
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200296 while (retries > 0) {
297 ret = i2c_smbus_read_block_data(client, address, values);
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200298 if (ret >= 0)
299 break;
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200300 retries--;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200301 }
302
303 if (ret < 0) {
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200304 dev_dbg(&client->dev, "%s: failed to read block 0x%x: %d\n",
305 __func__, address, ret);
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200306 return ret;
307 }
308
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200309 /* add string termination */
310 values[ret] = '\0';
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200311
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200312 return 0;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200313}
314
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800315static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700316 u16 value)
317{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800318 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200319 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800320 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800321
322 while (retries > 0) {
Phil Reida1bbec72017-06-15 21:59:29 +0800323 ret = i2c_smbus_write_word_data(client, address, value);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800324 if (ret >= 0)
325 break;
326 retries--;
327 }
328
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700329 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800330 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700331 "%s: i2c write to address 0x%x failed\n",
332 __func__, address);
333 return ret;
334 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800335
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700336 return 0;
337}
338
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200339static int sbs_status_correct(struct i2c_client *client, int *intval)
340{
341 int ret;
342
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200343 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200344 if (ret < 0)
345 return ret;
346
347 ret = (s16)ret;
348
349 /* Not drawing current means full (cannot be not charging) */
350 if (ret == 0)
351 *intval = POWER_SUPPLY_STATUS_FULL;
352
353 if (*intval == POWER_SUPPLY_STATUS_FULL) {
354 /* Drawing or providing current when full */
355 if (ret > 0)
356 *intval = POWER_SUPPLY_STATUS_CHARGING;
357 else if (ret < 0)
358 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
359 }
360
361 return 0;
362}
363
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800364static int sbs_get_battery_presence_and_health(
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700365 struct i2c_client *client, enum power_supply_property psp,
366 union power_supply_propval *val)
367{
Brian Norris76b16f4c2018-06-12 13:20:41 -0700368 int ret;
369
Michael Nosthofffe55e772019-08-16 09:58:42 +0200370 /* Dummy command; if it succeeds, battery is present. */
371 ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
372
373 if (ret < 0) { /* battery not present*/
374 if (psp == POWER_SUPPLY_PROP_PRESENT) {
375 val->intval = 0;
376 return 0;
377 }
378 return ret;
379 }
380
381 if (psp == POWER_SUPPLY_PROP_PRESENT)
382 val->intval = 1; /* battery present */
383 else /* POWER_SUPPLY_PROP_HEALTH */
Brian Norris76b16f4c2018-06-12 13:20:41 -0700384 /* SBS spec doesn't have a general health command. */
385 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
Brian Norris76b16f4c2018-06-12 13:20:41 -0700386
387 return 0;
388}
389
390static int sbs_get_ti_battery_presence_and_health(
391 struct i2c_client *client, enum power_supply_property psp,
392 union power_supply_propval *val)
393{
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700394 s32 ret;
395
Guenter Roeck17c6d392016-09-08 19:10:00 -0700396 /*
397 * Write to ManufacturerAccess with ManufacturerAccess command
Brian Norris76b16f4c2018-06-12 13:20:41 -0700398 * and then read the status.
Guenter Roeck17c6d392016-09-08 19:10:00 -0700399 */
Brian Norris76b16f4c2018-06-12 13:20:41 -0700400 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
401 MANUFACTURER_ACCESS_STATUS);
402 if (ret < 0) {
403 if (psp == POWER_SUPPLY_PROP_PRESENT)
404 val->intval = 0; /* battery removed */
405 return ret;
406 }
Guenter Roeck17c6d392016-09-08 19:10:00 -0700407
408 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800409 if (ret < 0) {
410 if (psp == POWER_SUPPLY_PROP_PRESENT)
411 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700412 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800413 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700414
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800415 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
416 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700417 val->intval = 0;
418 return 0;
419 }
420
421 /* Mask the upper nibble of 2nd byte and
422 * lower byte of response then
423 * shift the result by 8 to get status*/
424 ret &= 0x0F00;
425 ret >>= 8;
426 if (psp == POWER_SUPPLY_PROP_PRESENT) {
427 if (ret == 0x0F)
428 /* battery removed */
429 val->intval = 0;
430 else
431 val->intval = 1;
432 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
433 if (ret == 0x09)
434 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
435 else if (ret == 0x0B)
436 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
437 else if (ret == 0x0C)
438 val->intval = POWER_SUPPLY_HEALTH_DEAD;
439 else
440 val->intval = POWER_SUPPLY_HEALTH_GOOD;
441 }
442
443 return 0;
444}
445
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800446static int sbs_get_battery_property(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700447 int reg_offset, enum power_supply_property psp,
448 union power_supply_propval *val)
449{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800450 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700451 s32 ret;
452
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800453 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700454 if (ret < 0)
455 return ret;
456
457 /* returned values are 16 bit */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800458 if (sbs_data[reg_offset].min_value < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700459 ret = (s16)ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700460
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800461 if (ret >= sbs_data[reg_offset].min_value &&
462 ret <= sbs_data[reg_offset].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700463 val->intval = ret;
Joshua Clayton957cb722016-08-11 09:59:12 -0700464 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
465 if (!(ret & BATTERY_INITIALIZED))
466 val->intval =
467 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
468 else if (ret & BATTERY_FULL_CHARGED)
469 val->intval =
470 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
471 else if (ret & BATTERY_FULL_DISCHARGED)
472 val->intval =
473 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
474 else
475 val->intval =
476 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700477 return 0;
Joshua Clayton957cb722016-08-11 09:59:12 -0700478 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
479 return 0;
480 }
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700481
482 if (ret & BATTERY_FULL_CHARGED)
483 val->intval = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700484 else if (ret & BATTERY_DISCHARGING)
485 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
486 else
487 val->intval = POWER_SUPPLY_STATUS_CHARGING;
488
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200489 sbs_status_correct(client, &val->intval);
490
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800491 if (chip->poll_time == 0)
492 chip->last_state = val->intval;
493 else if (chip->last_state != val->intval) {
494 cancel_delayed_work_sync(&chip->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100495 power_supply_changed(chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800496 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700497 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700498 } else {
499 if (psp == POWER_SUPPLY_PROP_STATUS)
500 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700501 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
502 /* sbs spec says that this can be >100 %
503 * even if max value is 100 %
504 */
505 val->intval = min(ret, 100);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700506 else
507 val->intval = 0;
508 }
509
510 return 0;
511}
512
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200513static int sbs_get_battery_string_property(struct i2c_client *client,
514 int reg_offset, enum power_supply_property psp, char *val)
515{
Sebastian Reichelc4b12a22020-05-13 20:56:03 +0200516 return sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200517}
518
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800519static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700520 enum power_supply_property psp, union power_supply_propval *val)
521{
522#define BASE_UNIT_CONVERSION 1000
523#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800524#define TIME_UNIT_CONVERSION 60
525#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700526 switch (psp) {
527 case POWER_SUPPLY_PROP_ENERGY_NOW:
528 case POWER_SUPPLY_PROP_ENERGY_FULL:
529 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800530 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800531 * Convert to µWh
532 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700533 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
534 break;
535
536 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Simon Que4495b0a2014-08-04 13:47:46 +0200537 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700538 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
539 case POWER_SUPPLY_PROP_CURRENT_NOW:
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200540 case POWER_SUPPLY_PROP_CURRENT_AVG:
Rhyland Klein51d07562011-01-25 11:10:06 -0800541 case POWER_SUPPLY_PROP_CHARGE_NOW:
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200542 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
543 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
Rhyland Klein51d07562011-01-25 11:10:06 -0800544 case POWER_SUPPLY_PROP_CHARGE_FULL:
545 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700546 val->intval *= BASE_UNIT_CONVERSION;
547 break;
548
549 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800550 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800551 * so convert it to 0.1°C
552 */
553 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700554 break;
555
556 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
557 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800558 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800559 * Convert to seconds
560 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700561 val->intval *= TIME_UNIT_CONVERSION;
562 break;
563
564 default:
565 dev_dbg(&client->dev,
566 "%s: no need for unit conversion %d\n", __func__, psp);
567 }
568}
569
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400570static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
571 enum sbs_capacity_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800572{
573 int ret, original_val;
574
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800575 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800576 if (original_val < 0)
577 return original_val;
578
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400579 if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800580 return mode;
581
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400582 if (mode == CAPACITY_MODE_AMPS)
583 ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800584 else
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400585 ret = original_val | BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800586
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800587 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800588 if (ret < 0)
589 return ret;
590
Phil Reidadcf04c2017-07-11 17:43:43 +0800591 usleep_range(1000, 2000);
592
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400593 return original_val & BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800594}
595
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800596static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700597 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700598 union power_supply_propval *val)
599{
600 s32 ret;
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400601 enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800602
603 if (power_supply_is_amp_property(psp))
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400604 mode = CAPACITY_MODE_AMPS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800605
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400606 mode = sbs_set_capacity_mode(client, mode);
Dan Carpentereb368de2019-09-25 14:01:28 +0300607 if ((int)mode < 0)
Rhyland Klein51d07562011-01-25 11:10:06 -0800608 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700609
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800610 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700611 if (ret < 0)
612 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700613
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700614 val->intval = ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700615
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400616 ret = sbs_set_capacity_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800617 if (ret < 0)
618 return ret;
619
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700620 return 0;
621}
622
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800623static char sbs_serial[5];
624static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700625 union power_supply_propval *val)
626{
627 int ret;
628
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800629 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700630 if (ret < 0)
631 return ret;
632
Wolfram Sang5e9bee52017-10-29 00:45:59 +0200633 sprintf(sbs_serial, "%04x", ret);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800634 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700635
636 return 0;
637}
638
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800639static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800640 enum power_supply_property psp)
641{
642 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800643 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
644 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800645 return count;
646
647 dev_warn(&client->dev,
648 "%s: Invalid Property - %d\n", __func__, psp);
649
650 return -EINVAL;
651}
652
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200653static int sbs_get_chemistry(struct i2c_client *client,
654 union power_supply_propval *val)
655{
656 enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY;
657 int ret;
658
659 ret = sbs_get_property_index(client, psp);
660 if (ret < 0)
661 return ret;
662
663 ret = sbs_get_battery_string_property(client, ret, psp,
664 chemistry);
665 if (ret < 0)
666 return ret;
667
668 if (!strncasecmp(chemistry, "LION", 4))
669 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
670 else if (!strncasecmp(chemistry, "LiP", 3))
671 val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
672 else if (!strncasecmp(chemistry, "NiCd", 4))
673 val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
674 else if (!strncasecmp(chemistry, "NiMH", 4))
675 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
676 else
677 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
678
679 if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
680 dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry);
681
682 return 0;
683}
684
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800685static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700686 enum power_supply_property psp,
687 union power_supply_propval *val)
688{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800689 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100690 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800691 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700692
Phil Reid1cf85552017-08-24 17:31:10 +0800693 if (chip->gpio_detect) {
694 ret = gpiod_get_value_cansleep(chip->gpio_detect);
695 if (ret < 0)
696 return ret;
697 if (psp == POWER_SUPPLY_PROP_PRESENT) {
698 val->intval = ret;
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200699 sbs_update_presence(chip, ret);
Phil Reid1cf85552017-08-24 17:31:10 +0800700 return 0;
701 }
702 if (ret == 0)
703 return -ENODATA;
704 }
705
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700706 switch (psp) {
707 case POWER_SUPPLY_PROP_PRESENT:
708 case POWER_SUPPLY_PROP_HEALTH:
Sebastian Reichel0ff96912020-05-13 20:56:01 +0200709 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
Brian Norris76b16f4c2018-06-12 13:20:41 -0700710 ret = sbs_get_ti_battery_presence_and_health(client,
711 psp, val);
712 else
713 ret = sbs_get_battery_presence_and_health(client, psp,
714 val);
Michael Nosthofffe55e772019-08-16 09:58:42 +0200715
716 /* this can only be true if no gpio is used */
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800717 if (psp == POWER_SUPPLY_PROP_PRESENT)
718 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700719 break;
720
721 case POWER_SUPPLY_PROP_TECHNOLOGY:
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200722 ret = sbs_get_chemistry(client, val);
723 if (ret < 0)
724 break;
725
Nikolaus Voss5da50982012-05-09 08:30:44 +0200726 goto done; /* don't trigger power_supply_changed()! */
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700727
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700728 case POWER_SUPPLY_PROP_ENERGY_NOW:
729 case POWER_SUPPLY_PROP_ENERGY_FULL:
730 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800731 case POWER_SUPPLY_PROP_CHARGE_NOW:
732 case POWER_SUPPLY_PROP_CHARGE_FULL:
733 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800734 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800735 if (ret < 0)
736 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700737
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700738 /* sbs_get_battery_capacity() will change the battery mode
739 * temporarily to read the requested attribute. Ensure we stay
740 * in the desired mode for the duration of the attribute read.
741 */
742 mutex_lock(&chip->mode_lock);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800743 ret = sbs_get_battery_capacity(client, ret, psp, val);
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700744 mutex_unlock(&chip->mode_lock);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700745 break;
746
747 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800748 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700749 break;
750
751 case POWER_SUPPLY_PROP_STATUS:
Joshua Clayton957cb722016-08-11 09:59:12 -0700752 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700753 case POWER_SUPPLY_PROP_CYCLE_COUNT:
754 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
755 case POWER_SUPPLY_PROP_CURRENT_NOW:
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200756 case POWER_SUPPLY_PROP_CURRENT_AVG:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700757 case POWER_SUPPLY_PROP_TEMP:
758 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
759 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Simon Que4495b0a2014-08-04 13:47:46 +0200760 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700761 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200762 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
763 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700764 case POWER_SUPPLY_PROP_CAPACITY:
Sebastian Reicheld6f56322020-05-13 20:56:02 +0200765 case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800766 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800767 if (ret < 0)
768 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700769
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800770 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700771 break;
772
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200773 case POWER_SUPPLY_PROP_MODEL_NAME:
774 ret = sbs_get_property_index(client, psp);
775 if (ret < 0)
776 break;
777
778 ret = sbs_get_battery_string_property(client, ret, psp,
779 model_name);
780 val->strval = model_name;
781 break;
782
783 case POWER_SUPPLY_PROP_MANUFACTURER:
784 ret = sbs_get_property_index(client, psp);
785 if (ret < 0)
786 break;
787
788 ret = sbs_get_battery_string_property(client, ret, psp,
789 manufacturer);
790 val->strval = manufacturer;
791 break;
792
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700793 default:
794 dev_err(&client->dev,
795 "%s: INVALID property\n", __func__);
796 return -EINVAL;
797 }
798
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800799 if (!chip->enable_detection)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800800 goto done;
801
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800802 if (!chip->gpio_detect &&
803 chip->is_present != (ret >= 0)) {
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200804 sbs_update_presence(chip, (ret >= 0));
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100805 power_supply_changed(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800806 }
807
808done:
809 if (!ret) {
810 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800811 sbs_unit_adjustment(client, psp, val);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800812 }
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700813
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700814 dev_dbg(&client->dev,
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800815 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700816
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800817 if (ret && chip->is_present)
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800818 return ret;
819
820 /* battery not present, so return NODATA for properties */
821 if (ret)
822 return -ENODATA;
823
824 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700825}
826
Phil Reidcda3b012017-05-01 16:49:58 +0800827static void sbs_supply_changed(struct sbs_info *chip)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800828{
Phil Reidd2cec822016-07-25 10:42:58 +0800829 struct power_supply *battery = chip->power_supply;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800830 int ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800831
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800832 ret = gpiod_get_value_cansleep(chip->gpio_detect);
833 if (ret < 0)
Phil Reidcda3b012017-05-01 16:49:58 +0800834 return;
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200835 sbs_update_presence(chip, ret);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800836 power_supply_changed(battery);
Phil Reidcda3b012017-05-01 16:49:58 +0800837}
Rhyland Kleinbb879102011-02-28 16:55:28 -0800838
Phil Reidcda3b012017-05-01 16:49:58 +0800839static irqreturn_t sbs_irq(int irq, void *devid)
840{
841 sbs_supply_changed(devid);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800842 return IRQ_HANDLED;
843}
844
Phil Reidcda3b012017-05-01 16:49:58 +0800845static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
846 unsigned int data)
847{
848 sbs_supply_changed(i2c_get_clientdata(client));
849}
850
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800851static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700852{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100853 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700854
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700855 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800856 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700857
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800858 schedule_delayed_work(&chip->work, HZ);
Phil Reid389958b2016-09-20 09:01:12 +0800859 chip->poll_time = chip->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700860}
861
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800862static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700863{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800864 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700865 s32 ret;
866
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800867 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700868
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800869 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700870 /* if the read failed, give up on this work */
871 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800872 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700873 return;
874 }
875
876 if (ret & BATTERY_FULL_CHARGED)
877 ret = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700878 else if (ret & BATTERY_DISCHARGING)
879 ret = POWER_SUPPLY_STATUS_DISCHARGING;
880 else
881 ret = POWER_SUPPLY_STATUS_CHARGING;
882
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200883 sbs_status_correct(chip->client, &ret);
884
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800885 if (chip->last_state != ret) {
886 chip->poll_time = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100887 power_supply_changed(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700888 return;
889 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800890 if (chip->poll_time > 0) {
891 schedule_delayed_work(&chip->work, HZ);
892 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700893 return;
894 }
895}
896
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100897static const struct power_supply_desc sbs_default_desc = {
898 .type = POWER_SUPPLY_TYPE_BATTERY,
899 .properties = sbs_properties,
900 .num_properties = ARRAY_SIZE(sbs_properties),
901 .get_property = sbs_get_property,
902 .external_power_changed = sbs_external_power_changed,
903};
904
Bill Pembertonc8afa642012-11-19 13:22:23 -0500905static int sbs_probe(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700906 const struct i2c_device_id *id)
907{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800908 struct sbs_info *chip;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100909 struct power_supply_desc *sbs_desc;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800910 struct sbs_platform_data *pdata = client->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100911 struct power_supply_config psy_cfg = {};
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700912 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800913 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -0800914
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100915 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
916 sizeof(*sbs_desc), GFP_KERNEL);
917 if (!sbs_desc)
Rhyland Klein52f56c62011-12-05 17:50:49 -0800918 return -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100919
920 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
921 dev_name(&client->dev));
922 if (!sbs_desc->name)
923 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700924
Phil Reid9239a862016-07-25 10:42:57 +0800925 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100926 if (!chip)
927 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700928
Brian Norris76b16f4c2018-06-12 13:20:41 -0700929 chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800930 chip->client = client;
931 chip->enable_detection = false;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100932 psy_cfg.of_node = client->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100933 psy_cfg.drv_data = chip;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800934 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700935 mutex_init(&chip->mode_lock);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700936
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200937 /* use pdata if available, fall back to DT properties,
938 * or hardcoded defaults if not
939 */
940 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
941 &chip->i2c_retry_count);
942 if (rc)
Phil Reid389958b2016-09-20 09:01:12 +0800943 chip->i2c_retry_count = 0;
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700944
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200945 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
946 &chip->poll_retry_count);
947 if (rc)
948 chip->poll_retry_count = 0;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800949
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200950 if (pdata) {
951 chip->poll_retry_count = pdata->poll_retry_count;
952 chip->i2c_retry_count = pdata->i2c_retry_count;
953 }
Phil Reid389958b2016-09-20 09:01:12 +0800954 chip->i2c_retry_count = chip->i2c_retry_count + 1;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800955
956 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
957 "sbs,battery-detect", GPIOD_IN);
958 if (IS_ERR(chip->gpio_detect)) {
959 dev_err(&client->dev, "Failed to get gpio: %ld\n",
960 PTR_ERR(chip->gpio_detect));
961 return PTR_ERR(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800962 }
963
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800964 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700965
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800966 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800967 goto skip_gpio;
968
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800969 irq = gpiod_to_irq(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800970 if (irq <= 0) {
971 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800972 goto skip_gpio;
973 }
974
Phil Reidd2cec822016-07-25 10:42:58 +0800975 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
Ryosuke Saitobb8fe8e2017-04-21 12:13:17 +0900976 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Phil Reidd2cec822016-07-25 10:42:58 +0800977 dev_name(&client->dev), chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800978 if (rc) {
979 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800980 goto skip_gpio;
981 }
982
Rhyland Kleinbb879102011-02-28 16:55:28 -0800983skip_gpio:
Olof Johanssona22b41a2012-09-06 11:32:29 -0700984 /*
Frans Klaverf4ed9502015-06-10 14:16:56 +0200985 * Before we register, we might need to make sure we can actually talk
Olof Johanssona22b41a2012-09-06 11:32:29 -0700986 * to the battery.
987 */
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800988 if (!(force_load || chip->gpio_detect)) {
Frans Klaverf4ed9502015-06-10 14:16:56 +0200989 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
990
991 if (rc < 0) {
992 dev_err(&client->dev, "%s: Failed to get device status\n",
993 __func__);
994 goto exit_psupply;
995 }
Olof Johanssona22b41a2012-09-06 11:32:29 -0700996 }
Rhyland Kleinbb879102011-02-28 16:55:28 -0800997
Phil Reid492ff9d82016-07-25 10:42:59 +0800998 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100999 &psy_cfg);
1000 if (IS_ERR(chip->power_supply)) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001001 dev_err(&client->dev,
1002 "%s: Failed to register power supply\n", __func__);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001003 rc = PTR_ERR(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001004 goto exit_psupply;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001005 }
1006
1007 dev_info(&client->dev,
1008 "%s: battery gas gauge device registered\n", client->name);
1009
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001010 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001011
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001012 chip->enable_detection = true;
Rhyland Kleinee177d92011-05-24 12:06:50 -07001013
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001014 return 0;
Rhyland Kleinbb879102011-02-28 16:55:28 -08001015
1016exit_psupply:
Rhyland Kleinbb879102011-02-28 16:55:28 -08001017 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001018}
1019
Bill Pemberton415ec692012-11-19 13:26:07 -05001020static int sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001021{
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001022 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001023
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001024 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001025
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001026 return 0;
1027}
1028
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001029#if defined CONFIG_PM_SLEEP
1030
1031static int sbs_suspend(struct device *dev)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001032{
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001033 struct i2c_client *client = to_i2c_client(dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001034 struct sbs_info *chip = i2c_get_clientdata(client);
Brian Norris76b16f4c2018-06-12 13:20:41 -07001035 int ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001036
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001037 if (chip->poll_time > 0)
1038 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001039
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001040 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
Brian Norris76b16f4c2018-06-12 13:20:41 -07001041 /* Write to manufacturer access with sleep command. */
1042 ret = sbs_write_word_data(client,
1043 sbs_data[REG_MANUFACTURER_DATA].addr,
1044 MANUFACTURER_ACCESS_SLEEP);
1045 if (chip->is_present && ret < 0)
1046 return ret;
1047 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001048
1049 return 0;
1050}
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001051
1052static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
1053#define SBS_PM_OPS (&sbs_pm_ops)
1054
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001055#else
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001056#define SBS_PM_OPS NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001057#endif
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001058
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001059static const struct i2c_device_id sbs_id[] = {
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001060 { "bq20z65", 0 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001061 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001062 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001063 {}
1064};
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001065MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001066
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001067static const struct of_device_id sbs_dt_ids[] = {
1068 { .compatible = "sbs,sbs-battery" },
Brian Norris76b16f4c2018-06-12 13:20:41 -07001069 {
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001070 .compatible = "ti,bq20z65",
1071 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1072 },
1073 {
Brian Norris76b16f4c2018-06-12 13:20:41 -07001074 .compatible = "ti,bq20z75",
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001075 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
Brian Norris76b16f4c2018-06-12 13:20:41 -07001076 },
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001077 { }
1078};
1079MODULE_DEVICE_TABLE(of, sbs_dt_ids);
1080
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001081static struct i2c_driver sbs_battery_driver = {
1082 .probe = sbs_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001083 .remove = sbs_remove,
Phil Reidcda3b012017-05-01 16:49:58 +08001084 .alert = sbs_alert,
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001085 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001086 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001087 .name = "sbs-battery",
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001088 .of_match_table = sbs_dt_ids,
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001089 .pm = SBS_PM_OPS,
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001090 },
1091};
Axel Lin5ff92e72012-01-21 14:42:54 +08001092module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001093
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001094MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001095MODULE_LICENSE("GPL");
Frans Klaverf4ed9502015-06-10 14:16:56 +02001096
Jean-Francois Dagenais75d8a842019-11-01 15:06:59 -04001097module_param(force_load, bool, 0444);
Frans Klaverf4ed9502015-06-10 14:16:56 +02001098MODULE_PARM_DESC(force_load,
1099 "Attempt to load the driver even if no battery is connected");