blob: b6a538ebb378fcde62a42b9d76ac78555af8450f [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
Sebastian Reichele3f23962020-10-05 00:46:01 +0200282 if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel")
283 && client->flags & I2C_CLIENT_PEC) {
284 dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n");
285 client->flags &= ~I2C_CLIENT_PEC;
286 }
287
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200288 dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ?
289 "enabled" : "disabled");
290
Jean-Francois Dagenais182fc882020-05-13 20:56:11 +0200291 if (!chip->is_present && is_present && !chip->charger_broadcasts)
292 sbs_disable_charger_broadcasts(chip);
293
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200294 chip->is_present = true;
295
296 return 0;
297}
298
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800299static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700300{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800301 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200302 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800303 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800304
305 while (retries > 0) {
306 ret = i2c_smbus_read_word_data(client, address);
307 if (ret >= 0)
308 break;
309 retries--;
310 }
311
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700312 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800313 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700314 "%s: i2c read at address 0x%x failed\n",
315 __func__, address);
316 return ret;
317 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800318
Phil Reida1bbec72017-06-15 21:59:29 +0800319 return ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700320}
321
Sebastian Reichel05e04302020-06-06 01:06:24 +0200322static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values)
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200323{
324 struct sbs_info *chip = i2c_get_clientdata(client);
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200325 s32 ret = 0, block_length = 0;
326 int retries_length, retries_block;
327 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200328
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200329 retries_length = chip->i2c_retry_count;
330 retries_block = chip->i2c_retry_count;
331
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200332 dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
333 "Fallback method does not support PEC.\n");
Sebastian Reichel05e04302020-06-06 01:06:24 +0200334
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200335 /* Adapter needs to support these two functions */
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200336 if (!i2c_check_functionality(client->adapter,
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200337 I2C_FUNC_SMBUS_BYTE_DATA |
338 I2C_FUNC_SMBUS_I2C_BLOCK)){
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200339 return -ENODEV;
340 }
341
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200342 /* Get the length of block data */
343 while (retries_length > 0) {
344 ret = i2c_smbus_read_byte_data(client, address);
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200345 if (ret >= 0)
346 break;
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200347 retries_length--;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200348 }
349
350 if (ret < 0) {
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200351 dev_dbg(&client->dev,
352 "%s: i2c read at address 0x%x failed\n",
353 __func__, address);
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200354 return ret;
355 }
356
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200357 /* block_length does not include NULL terminator */
358 block_length = ret;
359 if (block_length > I2C_SMBUS_BLOCK_MAX) {
360 dev_err(&client->dev,
361 "%s: Returned block_length is longer than 0x%x\n",
362 __func__, I2C_SMBUS_BLOCK_MAX);
363 return -EINVAL;
364 }
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200365
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200366 /* Get the block data */
367 while (retries_block > 0) {
368 ret = i2c_smbus_read_i2c_block_data(
369 client, address,
370 block_length + 1, block_buffer);
371 if (ret >= 0)
372 break;
373 retries_block--;
374 }
375
376 if (ret < 0) {
377 dev_dbg(&client->dev,
378 "%s: i2c read at address 0x%x failed\n",
379 __func__, address);
380 return ret;
381 }
382
383 /* block_buffer[0] == block_length */
384 memcpy(values, block_buffer + 1, block_length);
385 values[block_length] = '\0';
386
387 return ret;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200388}
389
Sebastian Reichel05e04302020-06-06 01:06:24 +0200390static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values)
391{
392 struct sbs_info *chip = i2c_get_clientdata(client);
393 int retries = chip->i2c_retry_count;
394 int ret = 0;
395
Sebastian Reichel7222bd62020-06-06 01:06:25 +0200396 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
397 bool pec = client->flags & I2C_CLIENT_PEC;
398 client->flags &= ~I2C_CLIENT_PEC;
399 ret = sbs_read_string_data_fallback(client, address, values);
400 if (pec)
401 client->flags |= I2C_CLIENT_PEC;
402 return ret;
403 }
Sebastian Reichel05e04302020-06-06 01:06:24 +0200404
405 while (retries > 0) {
406 ret = i2c_smbus_read_block_data(client, address, values);
407 if (ret >= 0)
408 break;
409 retries--;
410 }
411
412 if (ret < 0) {
413 dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
414 return ret;
415 }
416
417 /* add string termination */
418 values[ret] = '\0';
419 return ret;
420}
421
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800422static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700423 u16 value)
424{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800425 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200426 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800427 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800428
429 while (retries > 0) {
Phil Reida1bbec72017-06-15 21:59:29 +0800430 ret = i2c_smbus_write_word_data(client, address, value);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800431 if (ret >= 0)
432 break;
433 retries--;
434 }
435
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700436 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800437 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700438 "%s: i2c write to address 0x%x failed\n",
439 __func__, address);
440 return ret;
441 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800442
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700443 return 0;
444}
445
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200446static int sbs_status_correct(struct i2c_client *client, int *intval)
447{
448 int ret;
449
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200450 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200451 if (ret < 0)
452 return ret;
453
454 ret = (s16)ret;
455
Sebastian Reichelf0318bc2020-05-13 20:56:10 +0200456 /* Not drawing current -> not charging (i.e. idle) */
457 if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0)
458 *intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200459
460 if (*intval == POWER_SUPPLY_STATUS_FULL) {
461 /* Drawing or providing current when full */
462 if (ret > 0)
463 *intval = POWER_SUPPLY_STATUS_CHARGING;
464 else if (ret < 0)
465 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
466 }
467
468 return 0;
469}
470
Sebastian Reichel6f72a072020-05-13 20:56:09 +0200471static bool sbs_bat_needs_calibration(struct i2c_client *client)
472{
473 int ret;
474
475 ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr);
476 if (ret < 0)
477 return false;
478
479 return !!(ret & BIT(7));
480}
481
Brian Norris76b16f4c2018-06-12 13:20:41 -0700482static int sbs_get_ti_battery_presence_and_health(
483 struct i2c_client *client, enum power_supply_property psp,
484 union power_supply_propval *val)
485{
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700486 s32 ret;
487
Guenter Roeck17c6d392016-09-08 19:10:00 -0700488 /*
489 * Write to ManufacturerAccess with ManufacturerAccess command
Brian Norris76b16f4c2018-06-12 13:20:41 -0700490 * and then read the status.
Guenter Roeck17c6d392016-09-08 19:10:00 -0700491 */
Brian Norris76b16f4c2018-06-12 13:20:41 -0700492 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
493 MANUFACTURER_ACCESS_STATUS);
494 if (ret < 0) {
495 if (psp == POWER_SUPPLY_PROP_PRESENT)
496 val->intval = 0; /* battery removed */
497 return ret;
498 }
Guenter Roeck17c6d392016-09-08 19:10:00 -0700499
500 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800501 if (ret < 0) {
502 if (psp == POWER_SUPPLY_PROP_PRESENT)
503 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700504 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800505 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700506
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800507 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
508 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700509 val->intval = 0;
510 return 0;
511 }
512
513 /* Mask the upper nibble of 2nd byte and
514 * lower byte of response then
515 * shift the result by 8 to get status*/
516 ret &= 0x0F00;
517 ret >>= 8;
518 if (psp == POWER_SUPPLY_PROP_PRESENT) {
519 if (ret == 0x0F)
520 /* battery removed */
521 val->intval = 0;
522 else
523 val->intval = 1;
524 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
525 if (ret == 0x09)
526 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
527 else if (ret == 0x0B)
528 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
529 else if (ret == 0x0C)
530 val->intval = POWER_SUPPLY_HEALTH_DEAD;
Sebastian Reichel6f72a072020-05-13 20:56:09 +0200531 else if (sbs_bat_needs_calibration(client))
532 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700533 else
534 val->intval = POWER_SUPPLY_HEALTH_GOOD;
535 }
536
537 return 0;
538}
539
Ikjoon Jang2c4bf692020-08-13 13:10:07 +0800540static int sbs_get_battery_presence_and_health(
541 struct i2c_client *client, enum power_supply_property psp,
542 union power_supply_propval *val)
543{
544 struct sbs_info *chip = i2c_get_clientdata(client);
545 int ret;
546
547 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
548 return sbs_get_ti_battery_presence_and_health(client, psp, val);
549
550 /* Dummy command; if it succeeds, battery is present. */
551 ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
552
553 if (ret < 0) { /* battery not present*/
554 if (psp == POWER_SUPPLY_PROP_PRESENT) {
555 val->intval = 0;
556 return 0;
557 }
558 return ret;
559 }
560
561 if (psp == POWER_SUPPLY_PROP_PRESENT)
562 val->intval = 1; /* battery present */
563 else { /* POWER_SUPPLY_PROP_HEALTH */
564 if (sbs_bat_needs_calibration(client)) {
565 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
566 } else {
567 /* SBS spec doesn't have a general health command. */
568 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
569 }
570 }
571
572 return 0;
573}
574
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800575static int sbs_get_battery_property(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700576 int reg_offset, enum power_supply_property psp,
577 union power_supply_propval *val)
578{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800579 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700580 s32 ret;
581
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800582 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700583 if (ret < 0)
584 return ret;
585
586 /* returned values are 16 bit */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800587 if (sbs_data[reg_offset].min_value < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700588 ret = (s16)ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700589
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800590 if (ret >= sbs_data[reg_offset].min_value &&
591 ret <= sbs_data[reg_offset].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700592 val->intval = ret;
Joshua Clayton957cb722016-08-11 09:59:12 -0700593 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
594 if (!(ret & BATTERY_INITIALIZED))
595 val->intval =
596 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
597 else if (ret & BATTERY_FULL_CHARGED)
598 val->intval =
599 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
600 else if (ret & BATTERY_FULL_DISCHARGED)
601 val->intval =
602 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
603 else
604 val->intval =
605 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700606 return 0;
Joshua Clayton957cb722016-08-11 09:59:12 -0700607 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
608 return 0;
609 }
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700610
611 if (ret & BATTERY_FULL_CHARGED)
612 val->intval = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700613 else if (ret & BATTERY_DISCHARGING)
614 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
615 else
616 val->intval = POWER_SUPPLY_STATUS_CHARGING;
617
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200618 sbs_status_correct(client, &val->intval);
619
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800620 if (chip->poll_time == 0)
621 chip->last_state = val->intval;
622 else if (chip->last_state != val->intval) {
623 cancel_delayed_work_sync(&chip->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100624 power_supply_changed(chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800625 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700626 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700627 } else {
628 if (psp == POWER_SUPPLY_PROP_STATUS)
629 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700630 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
631 /* sbs spec says that this can be >100 %
632 * even if max value is 100 %
633 */
634 val->intval = min(ret, 100);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700635 else
636 val->intval = 0;
637 }
638
639 return 0;
640}
641
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200642static int sbs_get_battery_string_property(struct i2c_client *client,
643 int reg_offset, enum power_supply_property psp, char *val)
644{
Sebastian Reichel972eabb2020-06-02 16:49:08 +0200645 s32 ret;
646
647 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
648
649 if (ret < 0)
650 return ret;
651
652 return 0;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200653}
654
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800655static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700656 enum power_supply_property psp, union power_supply_propval *val)
657{
658#define BASE_UNIT_CONVERSION 1000
659#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800660#define TIME_UNIT_CONVERSION 60
661#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700662 switch (psp) {
663 case POWER_SUPPLY_PROP_ENERGY_NOW:
664 case POWER_SUPPLY_PROP_ENERGY_FULL:
665 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800666 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800667 * Convert to µWh
668 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700669 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
670 break;
671
672 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Simon Que4495b0a2014-08-04 13:47:46 +0200673 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700674 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
675 case POWER_SUPPLY_PROP_CURRENT_NOW:
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200676 case POWER_SUPPLY_PROP_CURRENT_AVG:
Rhyland Klein51d07562011-01-25 11:10:06 -0800677 case POWER_SUPPLY_PROP_CHARGE_NOW:
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200678 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
679 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
Rhyland Klein51d07562011-01-25 11:10:06 -0800680 case POWER_SUPPLY_PROP_CHARGE_FULL:
681 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700682 val->intval *= BASE_UNIT_CONVERSION;
683 break;
684
685 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800686 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800687 * so convert it to 0.1°C
688 */
689 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700690 break;
691
692 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
693 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800694 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800695 * Convert to seconds
696 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700697 val->intval *= TIME_UNIT_CONVERSION;
698 break;
699
700 default:
701 dev_dbg(&client->dev,
702 "%s: no need for unit conversion %d\n", __func__, psp);
703 }
704}
705
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400706static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
707 enum sbs_capacity_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800708{
709 int ret, original_val;
710
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800711 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800712 if (original_val < 0)
713 return original_val;
714
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400715 if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800716 return mode;
717
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400718 if (mode == CAPACITY_MODE_AMPS)
719 ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800720 else
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400721 ret = original_val | BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800722
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800723 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800724 if (ret < 0)
725 return ret;
726
Phil Reidadcf04c2017-07-11 17:43:43 +0800727 usleep_range(1000, 2000);
728
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400729 return original_val & BATTERY_MODE_CAPACITY_MASK;
Rhyland Klein51d07562011-01-25 11:10:06 -0800730}
731
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800732static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700733 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700734 union power_supply_propval *val)
735{
736 s32 ret;
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400737 enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800738
739 if (power_supply_is_amp_property(psp))
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400740 mode = CAPACITY_MODE_AMPS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800741
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400742 mode = sbs_set_capacity_mode(client, mode);
Dan Carpentereb368de2019-09-25 14:01:28 +0300743 if ((int)mode < 0)
Rhyland Klein51d07562011-01-25 11:10:06 -0800744 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700745
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800746 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700747 if (ret < 0)
748 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700749
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700750 val->intval = ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700751
Jean-Francois Dagenaise2ec6ae2019-11-01 15:07:03 -0400752 ret = sbs_set_capacity_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800753 if (ret < 0)
754 return ret;
755
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700756 return 0;
757}
758
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800759static char sbs_serial[5];
760static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700761 union power_supply_propval *val)
762{
763 int ret;
764
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800765 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700766 if (ret < 0)
767 return ret;
768
Wolfram Sang5e9bee52017-10-29 00:45:59 +0200769 sprintf(sbs_serial, "%04x", ret);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800770 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700771
772 return 0;
773}
774
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800775static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800776 enum power_supply_property psp)
777{
778 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800779 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
780 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800781 return count;
782
783 dev_warn(&client->dev,
784 "%s: Invalid Property - %d\n", __func__, psp);
785
786 return -EINVAL;
787}
788
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200789static int sbs_get_chemistry(struct i2c_client *client,
790 union power_supply_propval *val)
791{
792 enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY;
793 int ret;
794
795 ret = sbs_get_property_index(client, psp);
796 if (ret < 0)
797 return ret;
798
799 ret = sbs_get_battery_string_property(client, ret, psp,
800 chemistry);
801 if (ret < 0)
802 return ret;
803
804 if (!strncasecmp(chemistry, "LION", 4))
805 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
806 else if (!strncasecmp(chemistry, "LiP", 3))
807 val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
808 else if (!strncasecmp(chemistry, "NiCd", 4))
809 val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
810 else if (!strncasecmp(chemistry, "NiMH", 4))
811 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
812 else
813 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
814
815 if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
816 dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry);
817
818 return 0;
819}
820
Sebastian Reichel7721c2f2020-05-13 20:56:08 +0200821static int sbs_get_battery_manufacture_date(struct i2c_client *client,
822 enum power_supply_property psp,
823 union power_supply_propval *val)
824{
825 int ret;
826 u16 day, month, year;
827
828 ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE);
829 if (ret < 0)
830 return ret;
831
832 day = ret & GENMASK(4, 0);
833 month = (ret & GENMASK(8, 5)) >> 5;
834 year = ((ret & GENMASK(15, 9)) >> 9) + 1980;
835
836 switch (psp) {
837 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
838 val->intval = year;
839 break;
840 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
841 val->intval = month;
842 break;
843 case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
844 val->intval = day;
845 break;
846 default:
847 return -EINVAL;
848 }
849
850 return 0;
851}
852
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800853static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700854 enum power_supply_property psp,
855 union power_supply_propval *val)
856{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800857 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100858 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800859 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700860
Phil Reid1cf85552017-08-24 17:31:10 +0800861 if (chip->gpio_detect) {
862 ret = gpiod_get_value_cansleep(chip->gpio_detect);
863 if (ret < 0)
864 return ret;
865 if (psp == POWER_SUPPLY_PROP_PRESENT) {
866 val->intval = ret;
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +0200867 sbs_update_presence(chip, ret);
Phil Reid1cf85552017-08-24 17:31:10 +0800868 return 0;
869 }
870 if (ret == 0)
871 return -ENODATA;
872 }
873
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700874 switch (psp) {
875 case POWER_SUPPLY_PROP_PRESENT:
876 case POWER_SUPPLY_PROP_HEALTH:
Ikjoon Jang2c4bf692020-08-13 13:10:07 +0800877 ret = sbs_get_battery_presence_and_health(client, psp, val);
Michael Nosthofffe55e772019-08-16 09:58:42 +0200878
879 /* this can only be true if no gpio is used */
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800880 if (psp == POWER_SUPPLY_PROP_PRESENT)
881 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700882 break;
883
884 case POWER_SUPPLY_PROP_TECHNOLOGY:
Sebastian Reichel3e9544f2020-05-13 20:56:06 +0200885 ret = sbs_get_chemistry(client, val);
886 if (ret < 0)
887 break;
888
Nikolaus Voss5da50982012-05-09 08:30:44 +0200889 goto done; /* don't trigger power_supply_changed()! */
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700890
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700891 case POWER_SUPPLY_PROP_ENERGY_NOW:
892 case POWER_SUPPLY_PROP_ENERGY_FULL:
893 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800894 case POWER_SUPPLY_PROP_CHARGE_NOW:
895 case POWER_SUPPLY_PROP_CHARGE_FULL:
896 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800897 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800898 if (ret < 0)
899 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700900
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700901 /* sbs_get_battery_capacity() will change the battery mode
902 * temporarily to read the requested attribute. Ensure we stay
903 * in the desired mode for the duration of the attribute read.
904 */
905 mutex_lock(&chip->mode_lock);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800906 ret = sbs_get_battery_capacity(client, ret, psp, val);
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700907 mutex_unlock(&chip->mode_lock);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700908 break;
909
910 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800911 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700912 break;
913
914 case POWER_SUPPLY_PROP_STATUS:
Joshua Clayton957cb722016-08-11 09:59:12 -0700915 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700916 case POWER_SUPPLY_PROP_CYCLE_COUNT:
917 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
918 case POWER_SUPPLY_PROP_CURRENT_NOW:
Sebastian Reichel8ce6ee42020-05-13 20:56:05 +0200919 case POWER_SUPPLY_PROP_CURRENT_AVG:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700920 case POWER_SUPPLY_PROP_TEMP:
921 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
922 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Simon Que4495b0a2014-08-04 13:47:46 +0200923 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700924 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Sebastian Reichel787fdbc2020-05-13 20:56:07 +0200925 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
926 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700927 case POWER_SUPPLY_PROP_CAPACITY:
Sebastian Reicheld6f56322020-05-13 20:56:02 +0200928 case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800929 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800930 if (ret < 0)
931 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700932
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800933 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700934 break;
935
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200936 case POWER_SUPPLY_PROP_MODEL_NAME:
937 ret = sbs_get_property_index(client, psp);
938 if (ret < 0)
939 break;
940
941 ret = sbs_get_battery_string_property(client, ret, psp,
942 model_name);
943 val->strval = model_name;
944 break;
945
946 case POWER_SUPPLY_PROP_MANUFACTURER:
947 ret = sbs_get_property_index(client, psp);
948 if (ret < 0)
949 break;
950
951 ret = sbs_get_battery_string_property(client, ret, psp,
952 manufacturer);
953 val->strval = manufacturer;
954 break;
955
Sebastian Reichel7721c2f2020-05-13 20:56:08 +0200956 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
957 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
958 case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
959 ret = sbs_get_battery_manufacture_date(client, psp, val);
960 break;
961
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700962 default:
963 dev_err(&client->dev,
964 "%s: INVALID property\n", __func__);
965 return -EINVAL;
966 }
967
Ikjoon Jang395a7252020-08-28 12:36:26 +0800968 if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
969 bool old_present = chip->is_present;
970 union power_supply_propval val;
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800971 int err = sbs_get_battery_presence_and_health(
Ikjoon Jang395a7252020-08-28 12:36:26 +0800972 client, POWER_SUPPLY_PROP_PRESENT, &val);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800973
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800974 sbs_update_presence(chip, !err && val.intval);
Ikjoon Jang395a7252020-08-28 12:36:26 +0800975
976 if (old_present != chip->is_present)
977 power_supply_changed(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800978 }
979
980done:
981 if (!ret) {
982 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800983 sbs_unit_adjustment(client, psp, val);
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800984 dev_dbg(&client->dev,
985 "%s: property = %d, value = %x\n", __func__,
986 psp, val->intval);
987 } else if (!chip->is_present) {
988 /* battery not present, so return NODATA for properties */
989 ret = -ENODATA;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800990 }
Ikjoon Jang8ae237e2020-09-03 11:04:40 +0800991 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700992}
993
Phil Reidcda3b012017-05-01 16:49:58 +0800994static void sbs_supply_changed(struct sbs_info *chip)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800995{
Phil Reidd2cec822016-07-25 10:42:58 +0800996 struct power_supply *battery = chip->power_supply;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800997 int ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800998
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800999 ret = gpiod_get_value_cansleep(chip->gpio_detect);
1000 if (ret < 0)
Phil Reidcda3b012017-05-01 16:49:58 +08001001 return;
Sebastian Reichel79bcd5a2020-05-13 20:56:04 +02001002 sbs_update_presence(chip, ret);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001003 power_supply_changed(battery);
Phil Reidcda3b012017-05-01 16:49:58 +08001004}
Rhyland Kleinbb879102011-02-28 16:55:28 -08001005
Phil Reidcda3b012017-05-01 16:49:58 +08001006static irqreturn_t sbs_irq(int irq, void *devid)
1007{
1008 sbs_supply_changed(devid);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001009 return IRQ_HANDLED;
1010}
1011
Phil Reidcda3b012017-05-01 16:49:58 +08001012static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
1013 unsigned int data)
1014{
1015 sbs_supply_changed(i2c_get_clientdata(client));
1016}
1017
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001018static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001019{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001020 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001021
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001022 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001023 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001024
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001025 schedule_delayed_work(&chip->work, HZ);
Phil Reid389958b2016-09-20 09:01:12 +08001026 chip->poll_time = chip->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001027}
1028
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001029static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001030{
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001031 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001032 s32 ret;
1033
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001034 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001035
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001036 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001037 /* if the read failed, give up on this work */
1038 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001039 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001040 return;
1041 }
1042
1043 if (ret & BATTERY_FULL_CHARGED)
1044 ret = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001045 else if (ret & BATTERY_DISCHARGING)
1046 ret = POWER_SUPPLY_STATUS_DISCHARGING;
1047 else
1048 ret = POWER_SUPPLY_STATUS_CHARGING;
1049
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +02001050 sbs_status_correct(chip->client, &ret);
1051
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001052 if (chip->last_state != ret) {
1053 chip->poll_time = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001054 power_supply_changed(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001055 return;
1056 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001057 if (chip->poll_time > 0) {
1058 schedule_delayed_work(&chip->work, HZ);
1059 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001060 return;
1061 }
1062}
1063
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001064static const struct power_supply_desc sbs_default_desc = {
1065 .type = POWER_SUPPLY_TYPE_BATTERY,
1066 .properties = sbs_properties,
1067 .num_properties = ARRAY_SIZE(sbs_properties),
1068 .get_property = sbs_get_property,
1069 .external_power_changed = sbs_external_power_changed,
1070};
1071
Sebastian Reichelf9ca07a2020-05-13 20:56:13 +02001072static int sbs_probe(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001073{
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001074 struct sbs_info *chip;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001075 struct power_supply_desc *sbs_desc;
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001076 struct sbs_platform_data *pdata = client->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01001077 struct power_supply_config psy_cfg = {};
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001078 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -08001079 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -08001080
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001081 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
1082 sizeof(*sbs_desc), GFP_KERNEL);
1083 if (!sbs_desc)
Rhyland Klein52f56c62011-12-05 17:50:49 -08001084 return -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001085
1086 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
1087 dev_name(&client->dev));
1088 if (!sbs_desc->name)
1089 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001090
Phil Reid9239a862016-07-25 10:42:57 +08001091 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001092 if (!chip)
1093 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001094
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001095 chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001096 chip->client = client;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01001097 psy_cfg.of_node = client->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001098 psy_cfg.drv_data = chip;
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001099 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -07001100 mutex_init(&chip->mode_lock);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001101
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001102 /* use pdata if available, fall back to DT properties,
1103 * or hardcoded defaults if not
1104 */
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001105 rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
1106 &chip->i2c_retry_count);
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001107 if (rc)
Phil Reid389958b2016-09-20 09:01:12 +08001108 chip->i2c_retry_count = 0;
Rhyland Klein6c75ea12011-09-14 13:19:07 -07001109
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001110 rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
1111 &chip->poll_retry_count);
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001112 if (rc)
1113 chip->poll_retry_count = 0;
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001114
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001115 if (pdata) {
1116 chip->poll_retry_count = pdata->poll_retry_count;
1117 chip->i2c_retry_count = pdata->i2c_retry_count;
1118 }
Phil Reid389958b2016-09-20 09:01:12 +08001119 chip->i2c_retry_count = chip->i2c_retry_count + 1;
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001120
Sebastian Reichel03b758b2020-05-13 20:56:12 +02001121 chip->charger_broadcasts = !device_property_read_bool(&client->dev,
Jean-Francois Dagenais182fc882020-05-13 20:56:11 +02001122 "sbs,disable-charger-broadcasts");
1123
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001124 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
1125 "sbs,battery-detect", GPIOD_IN);
1126 if (IS_ERR(chip->gpio_detect)) {
1127 dev_err(&client->dev, "Failed to get gpio: %ld\n",
1128 PTR_ERR(chip->gpio_detect));
1129 return PTR_ERR(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001130 }
1131
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001132 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001133
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001134 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -08001135 goto skip_gpio;
1136
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001137 irq = gpiod_to_irq(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001138 if (irq <= 0) {
1139 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001140 goto skip_gpio;
1141 }
1142
Phil Reidd2cec822016-07-25 10:42:58 +08001143 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
Ryosuke Saitobb8fe8e2017-04-21 12:13:17 +09001144 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Phil Reidd2cec822016-07-25 10:42:58 +08001145 dev_name(&client->dev), chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001146 if (rc) {
1147 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001148 goto skip_gpio;
1149 }
1150
Rhyland Kleinbb879102011-02-28 16:55:28 -08001151skip_gpio:
Olof Johanssona22b41a2012-09-06 11:32:29 -07001152 /*
Frans Klaverf4ed9502015-06-10 14:16:56 +02001153 * Before we register, we might need to make sure we can actually talk
Olof Johanssona22b41a2012-09-06 11:32:29 -07001154 * to the battery.
1155 */
Phil Reid3b5dd3a2016-09-01 15:50:52 +08001156 if (!(force_load || chip->gpio_detect)) {
Ikjoon Jang395a7252020-08-28 12:36:26 +08001157 union power_supply_propval val;
Frans Klaverf4ed9502015-06-10 14:16:56 +02001158
Ikjoon Jang395a7252020-08-28 12:36:26 +08001159 rc = sbs_get_battery_presence_and_health(
1160 client, POWER_SUPPLY_PROP_PRESENT, &val);
1161 if (rc < 0 || !val.intval) {
1162 dev_err(&client->dev, "Failed to get present status\n");
1163 rc = -ENODEV;
Frans Klaverf4ed9502015-06-10 14:16:56 +02001164 goto exit_psupply;
1165 }
Olof Johanssona22b41a2012-09-06 11:32:29 -07001166 }
Rhyland Kleinbb879102011-02-28 16:55:28 -08001167
Ikjoon Jang52bef412020-08-11 12:41:41 +08001168 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
1169
Phil Reid492ff9d82016-07-25 10:42:59 +08001170 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001171 &psy_cfg);
1172 if (IS_ERR(chip->power_supply)) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001173 dev_err(&client->dev,
1174 "%s: Failed to register power supply\n", __func__);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001175 rc = PTR_ERR(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -08001176 goto exit_psupply;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001177 }
1178
1179 dev_info(&client->dev,
1180 "%s: battery gas gauge device registered\n", client->name);
1181
1182 return 0;
Rhyland Kleinbb879102011-02-28 16:55:28 -08001183
1184exit_psupply:
Rhyland Kleinbb879102011-02-28 16:55:28 -08001185 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001186}
1187
Bill Pemberton415ec692012-11-19 13:26:07 -05001188static int sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001189{
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001190 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001191
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001192 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001193
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001194 return 0;
1195}
1196
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001197#if defined CONFIG_PM_SLEEP
1198
1199static int sbs_suspend(struct device *dev)
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001200{
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001201 struct i2c_client *client = to_i2c_client(dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001202 struct sbs_info *chip = i2c_get_clientdata(client);
Brian Norris76b16f4c2018-06-12 13:20:41 -07001203 int ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001204
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001205 if (chip->poll_time > 0)
1206 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -07001207
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001208 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
Brian Norris76b16f4c2018-06-12 13:20:41 -07001209 /* Write to manufacturer access with sleep command. */
1210 ret = sbs_write_word_data(client,
1211 sbs_data[REG_MANUFACTURER_DATA].addr,
1212 MANUFACTURER_ACCESS_SLEEP);
1213 if (chip->is_present && ret < 0)
1214 return ret;
1215 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001216
1217 return 0;
1218}
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001219
1220static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
1221#define SBS_PM_OPS (&sbs_pm_ops)
1222
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001223#else
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001224#define SBS_PM_OPS NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001225#endif
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001226
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001227static const struct i2c_device_id sbs_id[] = {
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001228 { "bq20z65", 0 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001229 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001230 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001231 {}
1232};
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001233MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001234
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001235static const struct of_device_id sbs_dt_ids[] = {
1236 { .compatible = "sbs,sbs-battery" },
Brian Norris76b16f4c2018-06-12 13:20:41 -07001237 {
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001238 .compatible = "ti,bq20z65",
1239 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1240 },
1241 {
Brian Norris76b16f4c2018-06-12 13:20:41 -07001242 .compatible = "ti,bq20z75",
Sebastian Reichel0ff96912020-05-13 20:56:01 +02001243 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
Brian Norris76b16f4c2018-06-12 13:20:41 -07001244 },
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001245 { }
1246};
1247MODULE_DEVICE_TABLE(of, sbs_dt_ids);
1248
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001249static struct i2c_driver sbs_battery_driver = {
Sebastian Reichelf9ca07a2020-05-13 20:56:13 +02001250 .probe_new = sbs_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001251 .remove = sbs_remove,
Phil Reidcda3b012017-05-01 16:49:58 +08001252 .alert = sbs_alert,
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001253 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001254 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001255 .name = "sbs-battery",
Arnd Bergmann9edeaad2016-09-06 15:16:33 +02001256 .of_match_table = sbs_dt_ids,
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +01001257 .pm = SBS_PM_OPS,
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001258 },
1259};
Axel Lin5ff92e72012-01-21 14:42:54 +08001260module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001261
Rhyland Klein3ddca0622011-12-05 17:50:46 -08001262MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001263MODULE_LICENSE("GPL");
Frans Klaverf4ed9502015-06-10 14:16:56 +02001264
Jean-Francois Dagenais75d8a842019-11-01 15:06:59 -04001265module_param(force_load, bool, 0444);
Frans Klaverf4ed9502015-06-10 14:16:56 +02001266MODULE_PARM_DESC(force_load,
1267 "Attempt to load the driver even if no battery is connected");