blob: 13192cbcce71625db14babc87f05394bbb64e128 [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>
Sebastian Reichel03b758b2020-05-13 20:56:12 +020017#include <linux/property.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,
Sebastian Reichel6f72a072020-05-13 20:56:09 +020026 REG_BATTERY_MODE,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070027 REG_TEMPERATURE,
28 REG_VOLTAGE,
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +020029 REG_CURRENT_NOW,
30 REG_CURRENT_AVG,
Sebastian Reicheld6f56322020-05-13 20:56:02 +020031 REG_MAX_ERR,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070032 REG_CAPACITY,
33 REG_TIME_TO_EMPTY,
34 REG_TIME_TO_FULL,
35 REG_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -070036 REG_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070037 REG_CYCLE_COUNT,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070038 REG_SERIAL_NUMBER,
39 REG_REMAINING_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080040 REG_REMAINING_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070041 REG_FULL_CHARGE_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080042 REG_FULL_CHARGE_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070043 REG_DESIGN_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080044 REG_DESIGN_CAPACITY_CHARGE,
Simon Que4495b0a2014-08-04 13:47:46 +020045 REG_DESIGN_VOLTAGE_MIN,
46 REG_DESIGN_VOLTAGE_MAX,
Sebastian Reichel3e9544f2020-05-13 20:56:06 +020047 REG_CHEMISTRY,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020048 REG_MANUFACTURER,
49 REG_MODEL_NAME,
Sebastian Reichel787fdbc2020-05-13 20:56:07 +020050 REG_CHARGE_CURRENT,
51 REG_CHARGE_VOLTAGE,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070052};
53
Sebastian Reichel7222bd62020-06-06 01:06:25 +020054#define REG_ADDR_SPEC_INFO 0x1A
55#define SPEC_INFO_VERSION_MASK GENMASK(7, 4)
56#define SPEC_INFO_VERSION_SHIFT 4
57
58#define SBS_VERSION_1_0 1
59#define SBS_VERSION_1_1 2
60#define SBS_VERSION_1_1_WITH_PEC 3
61
Sebastian Reichel7721c2f2020-05-13 20:56:08 +020062#define REG_ADDR_MANUFACTURE_DATE 0x1B
63
Rhyland Klein51d07562011-01-25 11:10:06 -080064/* Battery Mode defines */
65#define BATTERY_MODE_OFFSET 0x03
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -040066#define BATTERY_MODE_CAPACITY_MASK BIT(15)
67enum sbs_capacity_mode {
68 CAPACITY_MODE_AMPS = 0,
69 CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
Rhyland Klein51d07562011-01-25 11:10:06 -080070};
Jean-Francois Dagenais182fc882020-05-13 20:56:11 +020071#define BATTERY_MODE_CHARGER_MASK (1<<14)
Rhyland Klein51d07562011-01-25 11:10:06 -080072
Rhyland Kleina7640bf2010-09-05 15:31:23 -070073/* manufacturer access defines */
74#define MANUFACTURER_ACCESS_STATUS 0x0006
75#define MANUFACTURER_ACCESS_SLEEP 0x0011
76
77/* battery status value bits */
Joshua Clayton957cb722016-08-11 09:59:12 -070078#define BATTERY_INITIALIZED 0x80
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070079#define BATTERY_DISCHARGING 0x40
Rhyland Kleina7640bf2010-09-05 15:31:23 -070080#define BATTERY_FULL_CHARGED 0x20
81#define BATTERY_FULL_DISCHARGED 0x10
82
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020083/* min_value and max_value are only valid for numerical data */
Rhyland Klein3ddca0622011-12-05 17:50:46 -080084#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
Rhyland Kleina7640bf2010-09-05 15:31:23 -070085 .psp = _psp, \
86 .addr = _addr, \
87 .min_value = _min_value, \
88 .max_value = _max_value, \
89}
90
Rhyland Klein3ddca0622011-12-05 17:50:46 -080091static const struct chip_data {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070092 enum power_supply_property psp;
93 u8 addr;
94 int min_value;
95 int max_value;
Rhyland Klein3ddca0622011-12-05 17:50:46 -080096} sbs_data[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070097 [REG_MANUFACTURER_DATA] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080098 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
Sebastian Reichel6f72a072020-05-13 20:56:09 +020099 [REG_BATTERY_MODE] =
100 SBS_DATA(-1, 0x03, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700101 [REG_TEMPERATURE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800102 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700103 [REG_VOLTAGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800104 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200105 [REG_CURRENT_NOW] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800106 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200107 [REG_CURRENT_AVG] =
108 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
Sebastian Reicheld6f56322020-05-13 20:56:02 +0200109 [REG_MAX_ERR] =
110 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700111 [REG_CAPACITY] =
Nikolaus Vossb1f092f2012-04-25 08:59:03 +0200112 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700113 [REG_REMAINING_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800114 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800115 [REG_REMAINING_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800116 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700117 [REG_FULL_CHARGE_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800118 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800119 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800120 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700121 [REG_TIME_TO_EMPTY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800122 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700123 [REG_TIME_TO_FULL] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800124 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200125 [REG_CHARGE_CURRENT] =
126 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535),
127 [REG_CHARGE_VOLTAGE] =
128 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700129 [REG_STATUS] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800130 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
Joshua Clayton957cb722016-08-11 09:59:12 -0700131 [REG_CAPACITY_LEVEL] =
132 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700133 [REG_CYCLE_COUNT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800134 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700135 [REG_DESIGN_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800136 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800137 [REG_DESIGN_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800138 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
Simon Que4495b0a2014-08-04 13:47:46 +0200139 [REG_DESIGN_VOLTAGE_MIN] =
140 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
141 [REG_DESIGN_VOLTAGE_MAX] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800142 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700143 [REG_SERIAL_NUMBER] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800144 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200145 /* Properties of type `const char *' */
146 [REG_MANUFACTURER] =
147 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
148 [REG_MODEL_NAME] =
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200149 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535),
150 [REG_CHEMISTRY] =
151 SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700152};
153
Sebastian Reichel68956db2020-05-13 20:56:14 +0200154static const enum power_supply_property sbs_properties[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700155 POWER_SUPPLY_PROP_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -0700156 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700157 POWER_SUPPLY_PROP_HEALTH,
158 POWER_SUPPLY_PROP_PRESENT,
159 POWER_SUPPLY_PROP_TECHNOLOGY,
160 POWER_SUPPLY_PROP_CYCLE_COUNT,
161 POWER_SUPPLY_PROP_VOLTAGE_NOW,
162 POWER_SUPPLY_PROP_CURRENT_NOW,
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200163 POWER_SUPPLY_PROP_CURRENT_AVG,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700164 POWER_SUPPLY_PROP_CAPACITY,
Sebastian Reicheld6f56322020-05-13 20:56:02 +0200165 POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700166 POWER_SUPPLY_PROP_TEMP,
167 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
168 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
169 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Simon Que4495b0a2014-08-04 13:47:46 +0200170 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700171 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
172 POWER_SUPPLY_PROP_ENERGY_NOW,
173 POWER_SUPPLY_PROP_ENERGY_FULL,
174 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
Rhyland Klein51d07562011-01-25 11:10:06 -0800175 POWER_SUPPLY_PROP_CHARGE_NOW,
176 POWER_SUPPLY_PROP_CHARGE_FULL,
177 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200178 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
179 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
Sebastian Reichel7721c2f2020-05-13 20:56:08 +0200180 POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
181 POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
182 POWER_SUPPLY_PROP_MANUFACTURE_DAY,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200183 /* Properties of type `const char *' */
184 POWER_SUPPLY_PROP_MANUFACTURER,
185 POWER_SUPPLY_PROP_MODEL_NAME
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700186};
187
Sebastian Reichel0ff96912020-05-13 20:56:01 +0200188/* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
189#define SBS_FLAGS_TI_BQ20ZX5 BIT(0)
Brian Norris76b16f4c2018-06-12 13:20:41 -0700190
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800191struct sbs_info {
Rhyland Kleinbb879102011-02-28 16:55:28 -0800192 struct i2c_client *client;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100193 struct power_supply *power_supply;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800194 bool is_present;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800195 struct gpio_desc *gpio_detect;
Jean-Francois Dagenais182fc882020-05-13 20:56:11 +0200196 bool charger_broadcasts;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700197 int last_state;
198 int poll_time;
Phil Reid389958b2016-09-20 09:01:12 +0800199 u32 i2c_retry_count;
200 u32 poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700201 struct delayed_work work;
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700202 struct mutex mode_lock;
Brian Norris76b16f4c2018-06-12 13:20:41 -0700203 u32 flags;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700204};
205
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200206static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
207static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200208static char chemistry[I2C_SMBUS_BLOCK_MAX + 1];
Frans Klaverf4ed9502015-06-10 14:16:56 +0200209static bool force_load;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200210
Jean-Francois Dagenais182fc882020-05-13 20:56:11 +0200211static int sbs_read_word_data(struct i2c_client *client, u8 address);
212static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value);
213
214static void sbs_disable_charger_broadcasts(struct sbs_info *chip)
215{
216 int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET);
217 if (val < 0)
218 goto exit;
219
220 val |= BATTERY_MODE_CHARGER_MASK;
221
222 val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val);
223
224exit:
225 if (val < 0)
226 dev_err(&chip->client->dev,
227 "Failed to disable charger broadcasting: %d\n", val);
228 else
229 dev_dbg(&chip->client->dev, "%s\n", __func__);
230}
231
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200232static int sbs_update_presence(struct sbs_info *chip, bool is_present)
233{
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200234 struct i2c_client *client = chip->client;
235 int retries = chip->i2c_retry_count;
236 s32 ret = 0;
237 u8 version;
238
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200239 if (chip->is_present == is_present)
240 return 0;
241
242 if (!is_present) {
243 chip->is_present = false;
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200244 /* Disable PEC when no device is present */
245 client->flags &= ~I2C_CLIENT_PEC;
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200246 return 0;
247 }
248
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200249 /* Check if device supports packet error checking and use it */
250 while (retries > 0) {
251 ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO);
252 if (ret >= 0)
253 break;
254
255 /*
256 * Some batteries trigger the detection pin before the
257 * I2C bus is properly connected. This works around the
258 * issue.
259 */
260 msleep(100);
261
262 retries--;
263 }
264
265 if (ret < 0) {
266 dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);
267
268 /* fallback to old behaviour */
269 client->flags &= ~I2C_CLIENT_PEC;
270 chip->is_present = true;
271
272 return ret;
273 }
274
275 version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT;
276
277 if (version == SBS_VERSION_1_1_WITH_PEC)
278 client->flags |= I2C_CLIENT_PEC;
279 else
280 client->flags &= ~I2C_CLIENT_PEC;
281
282 dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ?
283 "enabled" : "disabled");
284
Jean-Francois Dagenais182fc882020-05-13 20:56:11 +0200285 if (!chip->is_present && is_present && !chip->charger_broadcasts)
286 sbs_disable_charger_broadcasts(chip);
287
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200288 chip->is_present = true;
289
290 return 0;
291}
292
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800293static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700294{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800295 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200296 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800297 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800298
299 while (retries > 0) {
300 ret = i2c_smbus_read_word_data(client, address);
301 if (ret >= 0)
302 break;
303 retries--;
304 }
305
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700306 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800307 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700308 "%s: i2c read at address 0x%x failed\n",
309 __func__, address);
310 return ret;
311 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800312
Phil Reida1bbec72017-06-15 21:59:29 +0800313 return ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700314}
315
Sebastian Reichel05e04302020-06-06 01:06:24 +0200316static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values)
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200317{
318 struct sbs_info *chip = i2c_get_clientdata(client);
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200319 s32 ret = 0, block_length = 0;
320 int retries_length, retries_block;
321 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200322
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200323 retries_length = chip->i2c_retry_count;
324 retries_block = chip->i2c_retry_count;
325
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200326 dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
327 "Fallback method does not support PEC.\n");
Sebastian Reichel05e04302020-06-06 01:06:24 +0200328
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200329 /* Adapter needs to support these two functions */
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200330 if (!i2c_check_functionality(client->adapter,
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200331 I2C_FUNC_SMBUS_BYTE_DATA |
332 I2C_FUNC_SMBUS_I2C_BLOCK)){
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200333 return -ENODEV;
334 }
335
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200336 /* Get the length of block data */
337 while (retries_length > 0) {
338 ret = i2c_smbus_read_byte_data(client, address);
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200339 if (ret >= 0)
340 break;
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200341 retries_length--;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200342 }
343
344 if (ret < 0) {
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200345 dev_dbg(&client->dev,
346 "%s: i2c read at address 0x%x failed\n",
347 __func__, address);
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200348 return ret;
349 }
350
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200351 /* block_length does not include NULL terminator */
352 block_length = ret;
353 if (block_length > I2C_SMBUS_BLOCK_MAX) {
354 dev_err(&client->dev,
355 "%s: Returned block_length is longer than 0x%x\n",
356 __func__, I2C_SMBUS_BLOCK_MAX);
357 return -EINVAL;
358 }
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200359
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200360 /* Get the block data */
361 while (retries_block > 0) {
362 ret = i2c_smbus_read_i2c_block_data(
363 client, address,
364 block_length + 1, block_buffer);
365 if (ret >= 0)
366 break;
367 retries_block--;
368 }
369
370 if (ret < 0) {
371 dev_dbg(&client->dev,
372 "%s: i2c read at address 0x%x failed\n",
373 __func__, address);
374 return ret;
375 }
376
377 /* block_buffer[0] == block_length */
378 memcpy(values, block_buffer + 1, block_length);
379 values[block_length] = '\0';
380
381 return ret;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200382}
383
Sebastian Reichel05e04302020-06-06 01:06:24 +0200384static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values)
385{
386 struct sbs_info *chip = i2c_get_clientdata(client);
387 int retries = chip->i2c_retry_count;
388 int ret = 0;
389
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200390 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
391 bool pec = client->flags & I2C_CLIENT_PEC;
392 client->flags &= ~I2C_CLIENT_PEC;
393 ret = sbs_read_string_data_fallback(client, address, values);
394 if (pec)
395 client->flags |= I2C_CLIENT_PEC;
396 return ret;
397 }
Sebastian Reichel05e04302020-06-06 01:06:24 +0200398
399 while (retries > 0) {
400 ret = i2c_smbus_read_block_data(client, address, values);
401 if (ret >= 0)
402 break;
403 retries--;
404 }
405
406 if (ret < 0) {
407 dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
408 return ret;
409 }
410
411 /* add string termination */
412 values[ret] = '\0';
413 return ret;
414}
415
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800416static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700417 u16 value)
418{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800419 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200420 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800421 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800422
423 while (retries > 0) {
Phil Reida1bbec72017-06-15 21:59:29 +0800424 ret = i2c_smbus_write_word_data(client, address, value);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800425 if (ret >= 0)
426 break;
427 retries--;
428 }
429
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700430 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800431 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700432 "%s: i2c write to address 0x%x failed\n",
433 __func__, address);
434 return ret;
435 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800436
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700437 return 0;
438}
439
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200440static int sbs_status_correct(struct i2c_client *client, int *intval)
441{
442 int ret;
443
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200444 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200445 if (ret < 0)
446 return ret;
447
448 ret = (s16)ret;
449
Sebastian Reichelf0318bc2020-05-13 20:56:10 +0200450 /* Not drawing current -> not charging (i.e. idle) */
451 if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0)
452 *intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200453
454 if (*intval == POWER_SUPPLY_STATUS_FULL) {
455 /* Drawing or providing current when full */
456 if (ret > 0)
457 *intval = POWER_SUPPLY_STATUS_CHARGING;
458 else if (ret < 0)
459 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
460 }
461
462 return 0;
463}
464
Sebastian Reichel6f72a072020-05-13 20:56:09 +0200465static bool sbs_bat_needs_calibration(struct i2c_client *client)
466{
467 int ret;
468
469 ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr);
470 if (ret < 0)
471 return false;
472
473 return !!(ret & BIT(7));
474}
475
Brian Norris76b16f4c2018-06-12 13:20:41 -0700476static int sbs_get_ti_battery_presence_and_health(
477 struct i2c_client *client, enum power_supply_property psp,
478 union power_supply_propval *val)
479{
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700480 s32 ret;
481
Guenter Roeck17c6d392016-09-08 19:10:00 -0700482 /*
483 * Write to ManufacturerAccess with ManufacturerAccess command
Brian Norris76b16f4c2018-06-12 13:20:41 -0700484 * and then read the status.
Guenter Roeck17c6d392016-09-08 19:10:00 -0700485 */
Brian Norris76b16f4c2018-06-12 13:20:41 -0700486 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
487 MANUFACTURER_ACCESS_STATUS);
488 if (ret < 0) {
489 if (psp == POWER_SUPPLY_PROP_PRESENT)
490 val->intval = 0; /* battery removed */
491 return ret;
492 }
Guenter Roeck17c6d392016-09-08 19:10:00 -0700493
494 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800495 if (ret < 0) {
496 if (psp == POWER_SUPPLY_PROP_PRESENT)
497 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700498 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800499 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700500
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800501 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
502 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700503 val->intval = 0;
504 return 0;
505 }
506
507 /* Mask the upper nibble of 2nd byte and
508 * lower byte of response then
509 * shift the result by 8 to get status*/
510 ret &= 0x0F00;
511 ret >>= 8;
512 if (psp == POWER_SUPPLY_PROP_PRESENT) {
513 if (ret == 0x0F)
514 /* battery removed */
515 val->intval = 0;
516 else
517 val->intval = 1;
518 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
519 if (ret == 0x09)
520 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
521 else if (ret == 0x0B)
522 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
523 else if (ret == 0x0C)
524 val->intval = POWER_SUPPLY_HEALTH_DEAD;
Sebastian Reichel6f72a072020-05-13 20:56:09 +0200525 else if (sbs_bat_needs_calibration(client))
526 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700527 else
528 val->intval = POWER_SUPPLY_HEALTH_GOOD;
529 }
530
531 return 0;
532}
533
Ikjoon Jang2c4bf692020-08-13 13:10:07 +0800534static int sbs_get_battery_presence_and_health(
535 struct i2c_client *client, enum power_supply_property psp,
536 union power_supply_propval *val)
537{
538 struct sbs_info *chip = i2c_get_clientdata(client);
539 int ret;
540
541 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
542 return sbs_get_ti_battery_presence_and_health(client, psp, val);
543
544 /* Dummy command; if it succeeds, battery is present. */
545 ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
546
547 if (ret < 0) { /* battery not present*/
548 if (psp == POWER_SUPPLY_PROP_PRESENT) {
549 val->intval = 0;
550 return 0;
551 }
552 return ret;
553 }
554
555 if (psp == POWER_SUPPLY_PROP_PRESENT)
556 val->intval = 1; /* battery present */
557 else { /* POWER_SUPPLY_PROP_HEALTH */
558 if (sbs_bat_needs_calibration(client)) {
559 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
560 } else {
561 /* SBS spec doesn't have a general health command. */
562 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
563 }
564 }
565
566 return 0;
567}
568
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800569static int sbs_get_battery_property(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700570 int reg_offset, enum power_supply_property psp,
571 union power_supply_propval *val)
572{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800573 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700574 s32 ret;
575
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800576 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700577 if (ret < 0)
578 return ret;
579
580 /* returned values are 16 bit */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800581 if (sbs_data[reg_offset].min_value < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700582 ret = (s16)ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700583
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800584 if (ret >= sbs_data[reg_offset].min_value &&
585 ret <= sbs_data[reg_offset].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700586 val->intval = ret;
Joshua Clayton957cb722016-08-11 09:59:12 -0700587 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
588 if (!(ret & BATTERY_INITIALIZED))
589 val->intval =
590 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
591 else if (ret & BATTERY_FULL_CHARGED)
592 val->intval =
593 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
594 else if (ret & BATTERY_FULL_DISCHARGED)
595 val->intval =
596 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
597 else
598 val->intval =
599 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700600 return 0;
Joshua Clayton957cb722016-08-11 09:59:12 -0700601 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
602 return 0;
603 }
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700604
605 if (ret & BATTERY_FULL_CHARGED)
606 val->intval = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700607 else if (ret & BATTERY_DISCHARGING)
608 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
609 else
610 val->intval = POWER_SUPPLY_STATUS_CHARGING;
611
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200612 sbs_status_correct(client, &val->intval);
613
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800614 if (chip->poll_time == 0)
615 chip->last_state = val->intval;
616 else if (chip->last_state != val->intval) {
617 cancel_delayed_work_sync(&chip->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100618 power_supply_changed(chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800619 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700620 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700621 } else {
622 if (psp == POWER_SUPPLY_PROP_STATUS)
623 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700624 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
625 /* sbs spec says that this can be >100 %
626 * even if max value is 100 %
627 */
628 val->intval = min(ret, 100);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700629 else
630 val->intval = 0;
631 }
632
633 return 0;
634}
635
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200636static int sbs_get_battery_string_property(struct i2c_client *client,
637 int reg_offset, enum power_supply_property psp, char *val)
638{
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200639 s32 ret;
640
641 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
642
643 if (ret < 0)
644 return ret;
645
646 return 0;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200647}
648
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800649static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700650 enum power_supply_property psp, union power_supply_propval *val)
651{
652#define BASE_UNIT_CONVERSION 1000
653#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800654#define TIME_UNIT_CONVERSION 60
655#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700656 switch (psp) {
657 case POWER_SUPPLY_PROP_ENERGY_NOW:
658 case POWER_SUPPLY_PROP_ENERGY_FULL:
659 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800660 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800661 * Convert to µWh
662 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700663 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
664 break;
665
666 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Simon Que4495b0a2014-08-04 13:47:46 +0200667 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700668 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
669 case POWER_SUPPLY_PROP_CURRENT_NOW:
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200670 case POWER_SUPPLY_PROP_CURRENT_AVG:
Rhyland Klein51d07562011-01-25 11:10:06 -0800671 case POWER_SUPPLY_PROP_CHARGE_NOW:
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200672 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
673 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
Rhyland Klein51d07562011-01-25 11:10:06 -0800674 case POWER_SUPPLY_PROP_CHARGE_FULL:
675 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700676 val->intval *= BASE_UNIT_CONVERSION;
677 break;
678
679 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800680 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800681 * so convert it to 0.1°C
682 */
683 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700684 break;
685
686 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
687 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800688 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800689 * Convert to seconds
690 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700691 val->intval *= TIME_UNIT_CONVERSION;
692 break;
693
694 default:
695 dev_dbg(&client->dev,
696 "%s: no need for unit conversion %d\n", __func__, psp);
697 }
698}
699
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400700static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
701 enum sbs_capacity_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800702{
703 int ret, original_val;
704
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800705 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800706 if (original_val < 0)
707 return original_val;
708
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400709 if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800710 return mode;
711
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400712 if (mode == CAPACITY_MODE_AMPS)
713 ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800714 else
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400715 ret = original_val | BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800716
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800717 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800718 if (ret < 0)
719 return ret;
720
Phil Reidadcf04c2017-07-11 17:43:43 +0800721 usleep_range(1000, 2000);
722
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400723 return original_val & BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800724}
725
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800726static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700727 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700728 union power_supply_propval *val)
729{
730 s32 ret;
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400731 enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800732
733 if (power_supply_is_amp_property(psp))
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400734 mode = CAPACITY_MODE_AMPS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800735
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400736 mode = sbs_set_capacity_mode(client, mode);
Dan Carpentereb368de2019-09-25 14:01:28 +0300737 if ((int)mode < 0)
Rhyland Klein51d07562011-01-25 11:10:06 -0800738 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700739
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800740 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700741 if (ret < 0)
742 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700743
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700744 val->intval = ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700745
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400746 ret = sbs_set_capacity_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800747 if (ret < 0)
748 return ret;
749
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700750 return 0;
751}
752
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800753static char sbs_serial[5];
754static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700755 union power_supply_propval *val)
756{
757 int ret;
758
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800759 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700760 if (ret < 0)
761 return ret;
762
Wolfram Sang5e9bee52017-10-29 00:45:59 +0200763 sprintf(sbs_serial, "%04x", ret);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800764 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700765
766 return 0;
767}
768
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800769static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800770 enum power_supply_property psp)
771{
772 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800773 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
774 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800775 return count;
776
777 dev_warn(&client->dev,
778 "%s: Invalid Property - %d\n", __func__, psp);
779
780 return -EINVAL;
781}
782
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200783static int sbs_get_chemistry(struct i2c_client *client,
784 union power_supply_propval *val)
785{
786 enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY;
787 int ret;
788
789 ret = sbs_get_property_index(client, psp);
790 if (ret < 0)
791 return ret;
792
793 ret = sbs_get_battery_string_property(client, ret, psp,
794 chemistry);
795 if (ret < 0)
796 return ret;
797
798 if (!strncasecmp(chemistry, "LION", 4))
799 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
800 else if (!strncasecmp(chemistry, "LiP", 3))
801 val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
802 else if (!strncasecmp(chemistry, "NiCd", 4))
803 val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
804 else if (!strncasecmp(chemistry, "NiMH", 4))
805 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
806 else
807 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
808
809 if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
810 dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry);
811
812 return 0;
813}
814
Sebastian Reichel7721c2f2020-05-13 20:56:08 +0200815static int sbs_get_battery_manufacture_date(struct i2c_client *client,
816 enum power_supply_property psp,
817 union power_supply_propval *val)
818{
819 int ret;
820 u16 day, month, year;
821
822 ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE);
823 if (ret < 0)
824 return ret;
825
826 day = ret & GENMASK(4, 0);
827 month = (ret & GENMASK(8, 5)) >> 5;
828 year = ((ret & GENMASK(15, 9)) >> 9) + 1980;
829
830 switch (psp) {
831 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
832 val->intval = year;
833 break;
834 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
835 val->intval = month;
836 break;
837 case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
838 val->intval = day;
839 break;
840 default:
841 return -EINVAL;
842 }
843
844 return 0;
845}
846
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800847static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700848 enum power_supply_property psp,
849 union power_supply_propval *val)
850{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800851 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100852 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800853 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700854
Phil Reid1cf85552017-08-24 17:31:10 +0800855 if (chip->gpio_detect) {
856 ret = gpiod_get_value_cansleep(chip->gpio_detect);
857 if (ret < 0)
858 return ret;
859 if (psp == POWER_SUPPLY_PROP_PRESENT) {
860 val->intval = ret;
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200861 sbs_update_presence(chip, ret);
Phil Reid1cf85552017-08-24 17:31:10 +0800862 return 0;
863 }
864 if (ret == 0)
865 return -ENODATA;
866 }
867
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700868 switch (psp) {
869 case POWER_SUPPLY_PROP_PRESENT:
870 case POWER_SUPPLY_PROP_HEALTH:
Ikjoon Jang2c4bf692020-08-13 13:10:07 +0800871 ret = sbs_get_battery_presence_and_health(client, psp, val);
Michael Nosthofffe55e772019-08-16 09:58:42 +0200872
873 /* this can only be true if no gpio is used */
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800874 if (psp == POWER_SUPPLY_PROP_PRESENT)
875 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700876 break;
877
878 case POWER_SUPPLY_PROP_TECHNOLOGY:
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200879 ret = sbs_get_chemistry(client, val);
880 if (ret < 0)
881 break;
882
Nikolaus Voss5da50982012-05-09 08:30:44 +0200883 goto done; /* don't trigger power_supply_changed()! */
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700884
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700885 case POWER_SUPPLY_PROP_ENERGY_NOW:
886 case POWER_SUPPLY_PROP_ENERGY_FULL:
887 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800888 case POWER_SUPPLY_PROP_CHARGE_NOW:
889 case POWER_SUPPLY_PROP_CHARGE_FULL:
890 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800891 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800892 if (ret < 0)
893 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700894
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700895 /* sbs_get_battery_capacity() will change the battery mode
896 * temporarily to read the requested attribute. Ensure we stay
897 * in the desired mode for the duration of the attribute read.
898 */
899 mutex_lock(&chip->mode_lock);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800900 ret = sbs_get_battery_capacity(client, ret, psp, val);
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700901 mutex_unlock(&chip->mode_lock);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700902 break;
903
904 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800905 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700906 break;
907
908 case POWER_SUPPLY_PROP_STATUS:
Joshua Clayton957cb722016-08-11 09:59:12 -0700909 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700910 case POWER_SUPPLY_PROP_CYCLE_COUNT:
911 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
912 case POWER_SUPPLY_PROP_CURRENT_NOW:
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200913 case POWER_SUPPLY_PROP_CURRENT_AVG:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700914 case POWER_SUPPLY_PROP_TEMP:
915 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
916 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Simon Que4495b0a2014-08-04 13:47:46 +0200917 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700918 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200919 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
920 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700921 case POWER_SUPPLY_PROP_CAPACITY:
Sebastian Reicheld6f56322020-05-13 20:56:02 +0200922 case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800923 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800924 if (ret < 0)
925 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700926
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800927 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700928 break;
929
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200930 case POWER_SUPPLY_PROP_MODEL_NAME:
931 ret = sbs_get_property_index(client, psp);
932 if (ret < 0)
933 break;
934
935 ret = sbs_get_battery_string_property(client, ret, psp,
936 model_name);
937 val->strval = model_name;
938 break;
939
940 case POWER_SUPPLY_PROP_MANUFACTURER:
941 ret = sbs_get_property_index(client, psp);
942 if (ret < 0)
943 break;
944
945 ret = sbs_get_battery_string_property(client, ret, psp,
946 manufacturer);
947 val->strval = manufacturer;
948 break;
949
Sebastian Reichel7721c2f2020-05-13 20:56:08 +0200950 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
951 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
952 case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
953 ret = sbs_get_battery_manufacture_date(client, psp, val);
954 break;
955
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700956 default:
957 dev_err(&client->dev,
958 "%s: INVALID property\n", __func__);
959 return -EINVAL;
960 }
961
Ikjoon Jang395a7252020-08-28 12:36:26 +0800962 if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
963 bool old_present = chip->is_present;
964 union power_supply_propval val;
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800965 int err = sbs_get_battery_presence_and_health(
Ikjoon Jang395a7252020-08-28 12:36:26 +0800966 client, POWER_SUPPLY_PROP_PRESENT, &val);
967
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800968 sbs_update_presence(chip, !err && val.intval);
Ikjoon Jang395a7252020-08-28 12:36:26 +0800969
970 if (old_present != chip->is_present)
971 power_supply_changed(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800972 }
973
974done:
975 if (!ret) {
976 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800977 sbs_unit_adjustment(client, psp, val);
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800978 dev_dbg(&client->dev,
979 "%s: property = %d, value = %x\n", __func__,
980 psp, val->intval);
981 } else if (!chip->is_present) {
982 /* battery not present, so return NODATA for properties */
983 ret = -ENODATA;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800984 }
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800985 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700986}
987
Phil Reidcda3b012017-05-01 16:49:58 +0800988static void sbs_supply_changed(struct sbs_info *chip)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800989{
Phil Reidd2cec822016-07-25 10:42:58 +0800990 struct power_supply *battery = chip->power_supply;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800991 int ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800992
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800993 ret = gpiod_get_value_cansleep(chip->gpio_detect);
994 if (ret < 0)
Phil Reidcda3b012017-05-01 16:49:58 +0800995 return;
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200996 sbs_update_presence(chip, ret);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800997 power_supply_changed(battery);
Phil Reidcda3b012017-05-01 16:49:58 +0800998}
Rhyland Kleinbb879102011-02-28 16:55:28 -0800999
Phil Reidcda3b012017-05-01 16:49:58 +08001000static irqreturn_t sbs_irq(int irq, void *devid)
1001{
1002 sbs_supply_changed(devid);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001003 return IRQ_HANDLED;
1004}
1005
Phil Reidcda3b012017-05-01 16:49:58 +08001006static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
1007 unsigned int data)
1008{
1009 sbs_supply_changed(i2c_get_clientdata(client));
1010}
1011
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001012static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001013{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001014 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001015
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001016 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001017 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001018
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001019 schedule_delayed_work(&chip->work, HZ);
Phil Reid389958b2016-09-20 09:01:12 +08001020 chip->poll_time = chip->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001021}
1022
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001023static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001024{
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001025 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001026 s32 ret;
1027
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001028 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001029
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001030 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001031 /* if the read failed, give up on this work */
1032 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001033 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001034 return;
1035 }
1036
1037 if (ret & BATTERY_FULL_CHARGED)
1038 ret = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001039 else if (ret & BATTERY_DISCHARGING)
1040 ret = POWER_SUPPLY_STATUS_DISCHARGING;
1041 else
1042 ret = POWER_SUPPLY_STATUS_CHARGING;
1043
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +02001044 sbs_status_correct(chip->client, &ret);
1045
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001046 if (chip->last_state != ret) {
1047 chip->poll_time = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001048 power_supply_changed(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001049 return;
1050 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001051 if (chip->poll_time > 0) {
1052 schedule_delayed_work(&chip->work, HZ);
1053 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001054 return;
1055 }
1056}
1057
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001058static const struct power_supply_desc sbs_default_desc = {
1059 .type = POWER_SUPPLY_TYPE_BATTERY,
1060 .properties = sbs_properties,
1061 .num_properties = ARRAY_SIZE(sbs_properties),
1062 .get_property = sbs_get_property,
1063 .external_power_changed = sbs_external_power_changed,
1064};
1065
Sebastian Reichelf9ca07a2020-05-13 20:56:13 +02001066static int sbs_probe(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001067{
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001068 struct sbs_info *chip;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001069 struct power_supply_desc *sbs_desc;
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001070 struct sbs_platform_data *pdata = client->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01001071 struct power_supply_config psy_cfg = {};
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001072 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -08001073 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -08001074
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001075 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
1076 sizeof(*sbs_desc), GFP_KERNEL);
1077 if (!sbs_desc)
Rhyland Klein52f56c62011-12-05 17:50:49 -08001078 return -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001079
1080 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
1081 dev_name(&client->dev));
1082 if (!sbs_desc->name)
1083 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001084
Phil Reid9239a862016-07-25 10:42:57 +08001085 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001086 if (!chip)
1087 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001088
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001089 chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001090 chip->client = client;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01001091 psy_cfg.of_node = client->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001092 psy_cfg.drv_data = chip;
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001093 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -07001094 mutex_init(&chip->mode_lock);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001095
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001096 /* use pdata if available, fall back to DT properties,
1097 * or hardcoded defaults if not
1098 */
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001099 rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
1100 &chip->i2c_retry_count);
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001101 if (rc)
Phil Reid389958b2016-09-20 09:01:12 +08001102 chip->i2c_retry_count = 0;
Rhyland Klein6c75ea12011-09-14 13:19:07 -07001103
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001104 rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
1105 &chip->poll_retry_count);
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001106 if (rc)
1107 chip->poll_retry_count = 0;
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001108
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001109 if (pdata) {
1110 chip->poll_retry_count = pdata->poll_retry_count;
1111 chip->i2c_retry_count = pdata->i2c_retry_count;
1112 }
Phil Reid389958b2016-09-20 09:01:12 +08001113 chip->i2c_retry_count = chip->i2c_retry_count + 1;
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001114
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001115 chip->charger_broadcasts = !device_property_read_bool(&client->dev,
Jean-Francois Dagenais182fc882020-05-13 20:56:11 +02001116 "sbs,disable-charger-broadcasts");
1117
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001118 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
1119 "sbs,battery-detect", GPIOD_IN);
1120 if (IS_ERR(chip->gpio_detect)) {
1121 dev_err(&client->dev, "Failed to get gpio: %ld\n",
1122 PTR_ERR(chip->gpio_detect));
1123 return PTR_ERR(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001124 }
1125
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001126 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001127
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001128 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -08001129 goto skip_gpio;
1130
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001131 irq = gpiod_to_irq(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001132 if (irq <= 0) {
1133 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001134 goto skip_gpio;
1135 }
1136
Phil Reidd2cec822016-07-25 10:42:58 +08001137 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
Ryosuke Saitobb8fe8e2017-04-21 12:13:17 +09001138 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Phil Reidd2cec822016-07-25 10:42:58 +08001139 dev_name(&client->dev), chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001140 if (rc) {
1141 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001142 goto skip_gpio;
1143 }
1144
Rhyland Kleinbb879102011-02-28 16:55:28 -08001145skip_gpio:
Olof Johanssona22b41a2012-09-06 11:32:29 -07001146 /*
Frans Klaverf4ed9502015-06-10 14:16:56 +02001147 * Before we register, we might need to make sure we can actually talk
Olof Johanssona22b41a2012-09-06 11:32:29 -07001148 * to the battery.
1149 */
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001150 if (!(force_load || chip->gpio_detect)) {
Ikjoon Jang395a7252020-08-28 12:36:26 +08001151 union power_supply_propval val;
Frans Klaverf4ed9502015-06-10 14:16:56 +02001152
Ikjoon Jang395a7252020-08-28 12:36:26 +08001153 rc = sbs_get_battery_presence_and_health(
1154 client, POWER_SUPPLY_PROP_PRESENT, &val);
1155 if (rc < 0 || !val.intval) {
1156 dev_err(&client->dev, "Failed to get present status\n");
1157 rc = -ENODEV;
Frans Klaverf4ed9502015-06-10 14:16:56 +02001158 goto exit_psupply;
1159 }
Olof Johanssona22b41a2012-09-06 11:32:29 -07001160 }
Rhyland Kleinbb879102011-02-28 16:55:28 -08001161
Ikjoon Jang52bef412020-08-11 12:41:41 +08001162 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
1163
Phil Reid492ff9d82016-07-25 10:42:59 +08001164 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001165 &psy_cfg);
1166 if (IS_ERR(chip->power_supply)) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001167 dev_err(&client->dev,
1168 "%s: Failed to register power supply\n", __func__);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001169 rc = PTR_ERR(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001170 goto exit_psupply;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001171 }
1172
1173 dev_info(&client->dev,
1174 "%s: battery gas gauge device registered\n", client->name);
1175
1176 return 0;
Rhyland Kleinbb879102011-02-28 16:55:28 -08001177
1178exit_psupply:
Rhyland Kleinbb879102011-02-28 16:55:28 -08001179 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001180}
1181
Bill Pemberton415ec692012-11-19 13:26:07 -05001182static int sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001183{
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001184 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001185
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001186 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001187
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001188 return 0;
1189}
1190
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001191#if defined CONFIG_PM_SLEEP
1192
1193static int sbs_suspend(struct device *dev)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001194{
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001195 struct i2c_client *client = to_i2c_client(dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001196 struct sbs_info *chip = i2c_get_clientdata(client);
Brian Norris76b16f4c2018-06-12 13:20:41 -07001197 int ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001198
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001199 if (chip->poll_time > 0)
1200 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001201
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001202 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
Brian Norris76b16f4c2018-06-12 13:20:41 -07001203 /* Write to manufacturer access with sleep command. */
1204 ret = sbs_write_word_data(client,
1205 sbs_data[REG_MANUFACTURER_DATA].addr,
1206 MANUFACTURER_ACCESS_SLEEP);
1207 if (chip->is_present && ret < 0)
1208 return ret;
1209 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001210
1211 return 0;
1212}
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001213
1214static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
1215#define SBS_PM_OPS (&sbs_pm_ops)
1216
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001217#else
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001218#define SBS_PM_OPS NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001219#endif
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001220
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001221static const struct i2c_device_id sbs_id[] = {
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001222 { "bq20z65", 0 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001223 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001224 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001225 {}
1226};
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001227MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001228
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001229static const struct of_device_id sbs_dt_ids[] = {
1230 { .compatible = "sbs,sbs-battery" },
Brian Norris76b16f4c2018-06-12 13:20:41 -07001231 {
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001232 .compatible = "ti,bq20z65",
1233 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1234 },
1235 {
Brian Norris76b16f4c2018-06-12 13:20:41 -07001236 .compatible = "ti,bq20z75",
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001237 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
Brian Norris76b16f4c2018-06-12 13:20:41 -07001238 },
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001239 { }
1240};
1241MODULE_DEVICE_TABLE(of, sbs_dt_ids);
1242
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001243static struct i2c_driver sbs_battery_driver = {
Sebastian Reichelf9ca07a2020-05-13 20:56:13 +02001244 .probe_new = sbs_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001245 .remove = sbs_remove,
Phil Reidcda3b012017-05-01 16:49:58 +08001246 .alert = sbs_alert,
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001247 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001248 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001249 .name = "sbs-battery",
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001250 .of_match_table = sbs_dt_ids,
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001251 .pm = SBS_PM_OPS,
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001252 },
1253};
Axel Lin5ff92e72012-01-21 14:42:54 +08001254module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001255
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001256MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001257MODULE_LICENSE("GPL");
Frans Klaverf4ed9502015-06-10 14:16:56 +02001258
Jean-Francois Dagenais75d8a842019-11-01 15:06:59 -04001259module_param(force_load, bool, 0444);
Frans Klaverf4ed9502015-06-10 14:16:56 +02001260MODULE_PARM_DESC(force_load,
1261 "Attempt to load the driver even if no battery is connected");