blob: 048d205d707490e7b79dac8c2786b3ab3b419286 [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
Phil Reidadcf04c2017-07-11 17:43:43 +08008#include <linux/delay.h>
Rhyland Kleina7640bf2010-09-05 15:31:23 -07009#include <linux/err.h>
Phil Reid3b5dd3a2016-09-01 15:50:52 +080010#include <linux/gpio/consumer.h>
Phil Reidb70f0a22017-07-11 17:43:42 +080011#include <linux/i2c.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
Sachin Kamat075ed032013-03-14 15:49:46 +053016#include <linux/of.h>
Brian Norris76b16f4c2018-06-12 13:20:41 -070017#include <linux/of_device.h>
Rhyland Klein3ddca0622011-12-05 17:50:46 -080018#include <linux/power/sbs-battery.h>
Phil Reidb70f0a22017-07-11 17:43:42 +080019#include <linux/power_supply.h>
20#include <linux/slab.h>
21#include <linux/stat.h>
Rhyland Kleina7640bf2010-09-05 15:31:23 -070022
23enum {
24 REG_MANUFACTURER_DATA,
25 REG_TEMPERATURE,
26 REG_VOLTAGE,
27 REG_CURRENT,
28 REG_CAPACITY,
29 REG_TIME_TO_EMPTY,
30 REG_TIME_TO_FULL,
31 REG_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -070032 REG_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070033 REG_CYCLE_COUNT,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070034 REG_SERIAL_NUMBER,
35 REG_REMAINING_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080036 REG_REMAINING_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070037 REG_FULL_CHARGE_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080038 REG_FULL_CHARGE_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070039 REG_DESIGN_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080040 REG_DESIGN_CAPACITY_CHARGE,
Simon Que4495b0a2014-08-04 13:47:46 +020041 REG_DESIGN_VOLTAGE_MIN,
42 REG_DESIGN_VOLTAGE_MAX,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020043 REG_MANUFACTURER,
44 REG_MODEL_NAME,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070045};
46
Rhyland Klein51d07562011-01-25 11:10:06 -080047/* Battery Mode defines */
48#define BATTERY_MODE_OFFSET 0x03
49#define BATTERY_MODE_MASK 0x8000
Rhyland Klein3ddca0622011-12-05 17:50:46 -080050enum sbs_battery_mode {
Michael Heinemann01881552017-07-07 16:23:54 +020051 BATTERY_MODE_AMPS = 0,
52 BATTERY_MODE_WATTS = 0x8000
Rhyland Klein51d07562011-01-25 11:10:06 -080053};
54
Rhyland Kleina7640bf2010-09-05 15:31:23 -070055/* manufacturer access defines */
56#define MANUFACTURER_ACCESS_STATUS 0x0006
57#define MANUFACTURER_ACCESS_SLEEP 0x0011
58
59/* battery status value bits */
Joshua Clayton957cb722016-08-11 09:59:12 -070060#define BATTERY_INITIALIZED 0x80
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070061#define BATTERY_DISCHARGING 0x40
Rhyland Kleina7640bf2010-09-05 15:31:23 -070062#define BATTERY_FULL_CHARGED 0x20
63#define BATTERY_FULL_DISCHARGED 0x10
64
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020065/* min_value and max_value are only valid for numerical data */
Rhyland Klein3ddca0622011-12-05 17:50:46 -080066#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
Rhyland Kleina7640bf2010-09-05 15:31:23 -070067 .psp = _psp, \
68 .addr = _addr, \
69 .min_value = _min_value, \
70 .max_value = _max_value, \
71}
72
Rhyland Klein3ddca0622011-12-05 17:50:46 -080073static const struct chip_data {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070074 enum power_supply_property psp;
75 u8 addr;
76 int min_value;
77 int max_value;
Rhyland Klein3ddca0622011-12-05 17:50:46 -080078} sbs_data[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070079 [REG_MANUFACTURER_DATA] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080080 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070081 [REG_TEMPERATURE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080082 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070083 [REG_VOLTAGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080084 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070085 [REG_CURRENT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080086 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070087 [REG_CAPACITY] =
Nikolaus Vossb1f092f2012-04-25 08:59:03 +020088 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070089 [REG_REMAINING_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080090 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -080091 [REG_REMAINING_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080092 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070093 [REG_FULL_CHARGE_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080094 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -080095 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080096 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070097 [REG_TIME_TO_EMPTY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080098 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070099 [REG_TIME_TO_FULL] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800100 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700101 [REG_STATUS] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800102 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
Joshua Clayton957cb722016-08-11 09:59:12 -0700103 [REG_CAPACITY_LEVEL] =
104 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700105 [REG_CYCLE_COUNT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800106 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700107 [REG_DESIGN_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800108 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800109 [REG_DESIGN_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800110 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
Simon Que4495b0a2014-08-04 13:47:46 +0200111 [REG_DESIGN_VOLTAGE_MIN] =
112 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
113 [REG_DESIGN_VOLTAGE_MAX] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800114 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700115 [REG_SERIAL_NUMBER] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800116 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200117 /* Properties of type `const char *' */
118 [REG_MANUFACTURER] =
119 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
120 [REG_MODEL_NAME] =
121 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700122};
123
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800124static enum power_supply_property sbs_properties[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700125 POWER_SUPPLY_PROP_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -0700126 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700127 POWER_SUPPLY_PROP_HEALTH,
128 POWER_SUPPLY_PROP_PRESENT,
129 POWER_SUPPLY_PROP_TECHNOLOGY,
130 POWER_SUPPLY_PROP_CYCLE_COUNT,
131 POWER_SUPPLY_PROP_VOLTAGE_NOW,
132 POWER_SUPPLY_PROP_CURRENT_NOW,
133 POWER_SUPPLY_PROP_CAPACITY,
134 POWER_SUPPLY_PROP_TEMP,
135 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
136 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
137 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Simon Que4495b0a2014-08-04 13:47:46 +0200138 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700139 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
140 POWER_SUPPLY_PROP_ENERGY_NOW,
141 POWER_SUPPLY_PROP_ENERGY_FULL,
142 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
Rhyland Klein51d07562011-01-25 11:10:06 -0800143 POWER_SUPPLY_PROP_CHARGE_NOW,
144 POWER_SUPPLY_PROP_CHARGE_FULL,
145 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200146 /* Properties of type `const char *' */
147 POWER_SUPPLY_PROP_MANUFACTURER,
148 POWER_SUPPLY_PROP_MODEL_NAME
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700149};
150
Brian Norris76b16f4c2018-06-12 13:20:41 -0700151/* Supports special manufacturer commands from TI BQ20Z75 IC. */
152#define SBS_FLAGS_TI_BQ20Z75 BIT(0)
153
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800154struct sbs_info {
Rhyland Kleinbb879102011-02-28 16:55:28 -0800155 struct i2c_client *client;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100156 struct power_supply *power_supply;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800157 bool is_present;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800158 struct gpio_desc *gpio_detect;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800159 bool enable_detection;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700160 int last_state;
161 int poll_time;
Phil Reid389958b2016-09-20 09:01:12 +0800162 u32 i2c_retry_count;
163 u32 poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700164 struct delayed_work work;
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700165 struct mutex mode_lock;
Brian Norris76b16f4c2018-06-12 13:20:41 -0700166 u32 flags;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700167};
168
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200169static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
170static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
Frans Klaverf4ed9502015-06-10 14:16:56 +0200171static bool force_load;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200172
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800173static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700174{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800175 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200176 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800177 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800178
179 while (retries > 0) {
180 ret = i2c_smbus_read_word_data(client, address);
181 if (ret >= 0)
182 break;
183 retries--;
184 }
185
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700186 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800187 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700188 "%s: i2c read at address 0x%x failed\n",
189 __func__, address);
190 return ret;
191 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800192
Phil Reida1bbec72017-06-15 21:59:29 +0800193 return ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700194}
195
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200196static int sbs_read_string_data(struct i2c_client *client, u8 address,
197 char *values)
198{
199 struct sbs_info *chip = i2c_get_clientdata(client);
200 s32 ret = 0, block_length = 0;
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200201 int retries_length, retries_block;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200202 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
203
Phil Reid389958b2016-09-20 09:01:12 +0800204 retries_length = chip->i2c_retry_count;
205 retries_block = chip->i2c_retry_count;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200206
207 /* Adapter needs to support these two functions */
208 if (!i2c_check_functionality(client->adapter,
209 I2C_FUNC_SMBUS_BYTE_DATA |
210 I2C_FUNC_SMBUS_I2C_BLOCK)){
211 return -ENODEV;
212 }
213
214 /* Get the length of block data */
215 while (retries_length > 0) {
216 ret = i2c_smbus_read_byte_data(client, address);
217 if (ret >= 0)
218 break;
219 retries_length--;
220 }
221
222 if (ret < 0) {
223 dev_dbg(&client->dev,
224 "%s: i2c read at address 0x%x failed\n",
225 __func__, address);
226 return ret;
227 }
228
229 /* block_length does not include NULL terminator */
230 block_length = ret;
231 if (block_length > I2C_SMBUS_BLOCK_MAX) {
232 dev_err(&client->dev,
233 "%s: Returned block_length is longer than 0x%x\n",
234 __func__, I2C_SMBUS_BLOCK_MAX);
235 return -EINVAL;
236 }
237
238 /* Get the block data */
239 while (retries_block > 0) {
240 ret = i2c_smbus_read_i2c_block_data(
241 client, address,
242 block_length + 1, block_buffer);
243 if (ret >= 0)
244 break;
245 retries_block--;
246 }
247
248 if (ret < 0) {
249 dev_dbg(&client->dev,
250 "%s: i2c read at address 0x%x failed\n",
251 __func__, address);
252 return ret;
253 }
254
255 /* block_buffer[0] == block_length */
256 memcpy(values, block_buffer + 1, block_length);
257 values[block_length] = '\0';
258
Phil Reida1bbec72017-06-15 21:59:29 +0800259 return ret;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200260}
261
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800262static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700263 u16 value)
264{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800265 struct sbs_info *chip = i2c_get_clientdata(client);
Wolfram Sang9410b7d2017-10-29 00:42:10 +0200266 int retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800267 s32 ret = 0;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800268
269 while (retries > 0) {
Phil Reida1bbec72017-06-15 21:59:29 +0800270 ret = i2c_smbus_write_word_data(client, address, value);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800271 if (ret >= 0)
272 break;
273 retries--;
274 }
275
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700276 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800277 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700278 "%s: i2c write to address 0x%x failed\n",
279 __func__, address);
280 return ret;
281 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800282
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700283 return 0;
284}
285
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200286static int sbs_status_correct(struct i2c_client *client, int *intval)
287{
288 int ret;
289
290 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
291 if (ret < 0)
292 return ret;
293
294 ret = (s16)ret;
295
296 /* Not drawing current means full (cannot be not charging) */
297 if (ret == 0)
298 *intval = POWER_SUPPLY_STATUS_FULL;
299
300 if (*intval == POWER_SUPPLY_STATUS_FULL) {
301 /* Drawing or providing current when full */
302 if (ret > 0)
303 *intval = POWER_SUPPLY_STATUS_CHARGING;
304 else if (ret < 0)
305 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
306 }
307
308 return 0;
309}
310
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800311static int sbs_get_battery_presence_and_health(
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700312 struct i2c_client *client, enum power_supply_property psp,
313 union power_supply_propval *val)
314{
Brian Norris76b16f4c2018-06-12 13:20:41 -0700315 int ret;
316
317 if (psp == POWER_SUPPLY_PROP_PRESENT) {
318 /* Dummy command; if it succeeds, battery is present. */
319 ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
320 if (ret < 0)
321 val->intval = 0; /* battery disconnected */
322 else
323 val->intval = 1; /* battery present */
324 } else { /* POWER_SUPPLY_PROP_HEALTH */
325 /* SBS spec doesn't have a general health command. */
326 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
327 }
328
329 return 0;
330}
331
332static int sbs_get_ti_battery_presence_and_health(
333 struct i2c_client *client, enum power_supply_property psp,
334 union power_supply_propval *val)
335{
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700336 s32 ret;
337
Guenter Roeck17c6d392016-09-08 19:10:00 -0700338 /*
339 * Write to ManufacturerAccess with ManufacturerAccess command
Brian Norris76b16f4c2018-06-12 13:20:41 -0700340 * and then read the status.
Guenter Roeck17c6d392016-09-08 19:10:00 -0700341 */
Brian Norris76b16f4c2018-06-12 13:20:41 -0700342 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
343 MANUFACTURER_ACCESS_STATUS);
344 if (ret < 0) {
345 if (psp == POWER_SUPPLY_PROP_PRESENT)
346 val->intval = 0; /* battery removed */
347 return ret;
348 }
Guenter Roeck17c6d392016-09-08 19:10:00 -0700349
350 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800351 if (ret < 0) {
352 if (psp == POWER_SUPPLY_PROP_PRESENT)
353 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700354 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800355 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700356
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800357 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
358 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700359 val->intval = 0;
360 return 0;
361 }
362
363 /* Mask the upper nibble of 2nd byte and
364 * lower byte of response then
365 * shift the result by 8 to get status*/
366 ret &= 0x0F00;
367 ret >>= 8;
368 if (psp == POWER_SUPPLY_PROP_PRESENT) {
369 if (ret == 0x0F)
370 /* battery removed */
371 val->intval = 0;
372 else
373 val->intval = 1;
374 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
375 if (ret == 0x09)
376 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
377 else if (ret == 0x0B)
378 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
379 else if (ret == 0x0C)
380 val->intval = POWER_SUPPLY_HEALTH_DEAD;
381 else
382 val->intval = POWER_SUPPLY_HEALTH_GOOD;
383 }
384
385 return 0;
386}
387
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800388static int sbs_get_battery_property(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700389 int reg_offset, enum power_supply_property psp,
390 union power_supply_propval *val)
391{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800392 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700393 s32 ret;
394
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800395 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700396 if (ret < 0)
397 return ret;
398
399 /* returned values are 16 bit */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800400 if (sbs_data[reg_offset].min_value < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700401 ret = (s16)ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700402
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800403 if (ret >= sbs_data[reg_offset].min_value &&
404 ret <= sbs_data[reg_offset].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700405 val->intval = ret;
Joshua Clayton957cb722016-08-11 09:59:12 -0700406 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
407 if (!(ret & BATTERY_INITIALIZED))
408 val->intval =
409 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
410 else if (ret & BATTERY_FULL_CHARGED)
411 val->intval =
412 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
413 else if (ret & BATTERY_FULL_DISCHARGED)
414 val->intval =
415 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
416 else
417 val->intval =
418 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700419 return 0;
Joshua Clayton957cb722016-08-11 09:59:12 -0700420 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
421 return 0;
422 }
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700423
424 if (ret & BATTERY_FULL_CHARGED)
425 val->intval = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700426 else if (ret & BATTERY_DISCHARGING)
427 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
428 else
429 val->intval = POWER_SUPPLY_STATUS_CHARGING;
430
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200431 sbs_status_correct(client, &val->intval);
432
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800433 if (chip->poll_time == 0)
434 chip->last_state = val->intval;
435 else if (chip->last_state != val->intval) {
436 cancel_delayed_work_sync(&chip->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100437 power_supply_changed(chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800438 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700439 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700440 } else {
441 if (psp == POWER_SUPPLY_PROP_STATUS)
442 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700443 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
444 /* sbs spec says that this can be >100 %
445 * even if max value is 100 %
446 */
447 val->intval = min(ret, 100);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700448 else
449 val->intval = 0;
450 }
451
452 return 0;
453}
454
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200455static int sbs_get_battery_string_property(struct i2c_client *client,
456 int reg_offset, enum power_supply_property psp, char *val)
457{
458 s32 ret;
459
460 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
461
462 if (ret < 0)
463 return ret;
464
465 return 0;
466}
467
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800468static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700469 enum power_supply_property psp, union power_supply_propval *val)
470{
471#define BASE_UNIT_CONVERSION 1000
472#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800473#define TIME_UNIT_CONVERSION 60
474#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700475 switch (psp) {
476 case POWER_SUPPLY_PROP_ENERGY_NOW:
477 case POWER_SUPPLY_PROP_ENERGY_FULL:
478 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800479 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800480 * Convert to µWh
481 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700482 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
483 break;
484
485 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Simon Que4495b0a2014-08-04 13:47:46 +0200486 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700487 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
488 case POWER_SUPPLY_PROP_CURRENT_NOW:
Rhyland Klein51d07562011-01-25 11:10:06 -0800489 case POWER_SUPPLY_PROP_CHARGE_NOW:
490 case POWER_SUPPLY_PROP_CHARGE_FULL:
491 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700492 val->intval *= BASE_UNIT_CONVERSION;
493 break;
494
495 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800496 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800497 * so convert it to 0.1°C
498 */
499 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700500 break;
501
502 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
503 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800504 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800505 * Convert to seconds
506 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700507 val->intval *= TIME_UNIT_CONVERSION;
508 break;
509
510 default:
511 dev_dbg(&client->dev,
512 "%s: no need for unit conversion %d\n", __func__, psp);
513 }
514}
515
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800516static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
517 enum sbs_battery_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800518{
519 int ret, original_val;
520
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800521 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800522 if (original_val < 0)
523 return original_val;
524
525 if ((original_val & BATTERY_MODE_MASK) == mode)
526 return mode;
527
528 if (mode == BATTERY_MODE_AMPS)
529 ret = original_val & ~BATTERY_MODE_MASK;
530 else
531 ret = original_val | BATTERY_MODE_MASK;
532
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800533 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800534 if (ret < 0)
535 return ret;
536
Phil Reidadcf04c2017-07-11 17:43:43 +0800537 usleep_range(1000, 2000);
538
Rhyland Klein51d07562011-01-25 11:10:06 -0800539 return original_val & BATTERY_MODE_MASK;
540}
541
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800542static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700543 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700544 union power_supply_propval *val)
545{
546 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800547 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800548
549 if (power_supply_is_amp_property(psp))
550 mode = BATTERY_MODE_AMPS;
551
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800552 mode = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800553 if (mode < 0)
554 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700555
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800556 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700557 if (ret < 0)
558 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700559
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700560 val->intval = ret;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700561
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800562 ret = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800563 if (ret < 0)
564 return ret;
565
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700566 return 0;
567}
568
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800569static char sbs_serial[5];
570static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700571 union power_supply_propval *val)
572{
573 int ret;
574
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800575 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700576 if (ret < 0)
577 return ret;
578
Wolfram Sang5e9bee52017-10-29 00:45:59 +0200579 sprintf(sbs_serial, "%04x", ret);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800580 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700581
582 return 0;
583}
584
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800585static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800586 enum power_supply_property psp)
587{
588 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800589 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
590 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800591 return count;
592
593 dev_warn(&client->dev,
594 "%s: Invalid Property - %d\n", __func__, psp);
595
596 return -EINVAL;
597}
598
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800599static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700600 enum power_supply_property psp,
601 union power_supply_propval *val)
602{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800603 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100604 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800605 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700606
Phil Reid1cf85552017-08-24 17:31:10 +0800607 if (chip->gpio_detect) {
608 ret = gpiod_get_value_cansleep(chip->gpio_detect);
609 if (ret < 0)
610 return ret;
611 if (psp == POWER_SUPPLY_PROP_PRESENT) {
612 val->intval = ret;
613 chip->is_present = val->intval;
614 return 0;
615 }
616 if (ret == 0)
617 return -ENODATA;
618 }
619
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700620 switch (psp) {
621 case POWER_SUPPLY_PROP_PRESENT:
622 case POWER_SUPPLY_PROP_HEALTH:
Brian Norris76b16f4c2018-06-12 13:20:41 -0700623 if (client->flags & SBS_FLAGS_TI_BQ20Z75)
624 ret = sbs_get_ti_battery_presence_and_health(client,
625 psp, val);
626 else
627 ret = sbs_get_battery_presence_and_health(client, psp,
628 val);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800629 if (psp == POWER_SUPPLY_PROP_PRESENT)
630 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700631 break;
632
633 case POWER_SUPPLY_PROP_TECHNOLOGY:
634 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
Nikolaus Voss5da50982012-05-09 08:30:44 +0200635 goto done; /* don't trigger power_supply_changed()! */
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700636
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700637 case POWER_SUPPLY_PROP_ENERGY_NOW:
638 case POWER_SUPPLY_PROP_ENERGY_FULL:
639 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800640 case POWER_SUPPLY_PROP_CHARGE_NOW:
641 case POWER_SUPPLY_PROP_CHARGE_FULL:
642 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800643 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800644 if (ret < 0)
645 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700646
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700647 /* sbs_get_battery_capacity() will change the battery mode
648 * temporarily to read the requested attribute. Ensure we stay
649 * in the desired mode for the duration of the attribute read.
650 */
651 mutex_lock(&chip->mode_lock);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800652 ret = sbs_get_battery_capacity(client, ret, psp, val);
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700653 mutex_unlock(&chip->mode_lock);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700654 break;
655
656 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800657 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700658 break;
659
660 case POWER_SUPPLY_PROP_STATUS:
Joshua Clayton957cb722016-08-11 09:59:12 -0700661 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700662 case POWER_SUPPLY_PROP_CYCLE_COUNT:
663 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
664 case POWER_SUPPLY_PROP_CURRENT_NOW:
665 case POWER_SUPPLY_PROP_TEMP:
666 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
667 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Simon Que4495b0a2014-08-04 13:47:46 +0200668 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700669 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Shawn Nematbakhshbfa953d2017-06-13 10:53:26 -0700670 case POWER_SUPPLY_PROP_CAPACITY:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800671 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800672 if (ret < 0)
673 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700674
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800675 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700676 break;
677
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200678 case POWER_SUPPLY_PROP_MODEL_NAME:
679 ret = sbs_get_property_index(client, psp);
680 if (ret < 0)
681 break;
682
683 ret = sbs_get_battery_string_property(client, ret, psp,
684 model_name);
685 val->strval = model_name;
686 break;
687
688 case POWER_SUPPLY_PROP_MANUFACTURER:
689 ret = sbs_get_property_index(client, psp);
690 if (ret < 0)
691 break;
692
693 ret = sbs_get_battery_string_property(client, ret, psp,
694 manufacturer);
695 val->strval = manufacturer;
696 break;
697
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700698 default:
699 dev_err(&client->dev,
700 "%s: INVALID property\n", __func__);
701 return -EINVAL;
702 }
703
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800704 if (!chip->enable_detection)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800705 goto done;
706
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800707 if (!chip->gpio_detect &&
708 chip->is_present != (ret >= 0)) {
709 chip->is_present = (ret >= 0);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100710 power_supply_changed(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800711 }
712
713done:
714 if (!ret) {
715 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800716 sbs_unit_adjustment(client, psp, val);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800717 }
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700718
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700719 dev_dbg(&client->dev,
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800720 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700721
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800722 if (ret && chip->is_present)
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800723 return ret;
724
725 /* battery not present, so return NODATA for properties */
726 if (ret)
727 return -ENODATA;
728
729 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700730}
731
Phil Reidcda3b012017-05-01 16:49:58 +0800732static void sbs_supply_changed(struct sbs_info *chip)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800733{
Phil Reidd2cec822016-07-25 10:42:58 +0800734 struct power_supply *battery = chip->power_supply;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800735 int ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800736
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800737 ret = gpiod_get_value_cansleep(chip->gpio_detect);
738 if (ret < 0)
Phil Reidcda3b012017-05-01 16:49:58 +0800739 return;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800740 chip->is_present = ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800741 power_supply_changed(battery);
Phil Reidcda3b012017-05-01 16:49:58 +0800742}
Rhyland Kleinbb879102011-02-28 16:55:28 -0800743
Phil Reidcda3b012017-05-01 16:49:58 +0800744static irqreturn_t sbs_irq(int irq, void *devid)
745{
746 sbs_supply_changed(devid);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800747 return IRQ_HANDLED;
748}
749
Phil Reidcda3b012017-05-01 16:49:58 +0800750static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
751 unsigned int data)
752{
753 sbs_supply_changed(i2c_get_clientdata(client));
754}
755
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800756static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700757{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100758 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700759
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700760 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800761 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700762
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800763 schedule_delayed_work(&chip->work, HZ);
Phil Reid389958b2016-09-20 09:01:12 +0800764 chip->poll_time = chip->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700765}
766
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800767static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700768{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800769 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700770 s32 ret;
771
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800772 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700773
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800774 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700775 /* if the read failed, give up on this work */
776 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800777 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700778 return;
779 }
780
781 if (ret & BATTERY_FULL_CHARGED)
782 ret = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700783 else if (ret & BATTERY_DISCHARGING)
784 ret = POWER_SUPPLY_STATUS_DISCHARGING;
785 else
786 ret = POWER_SUPPLY_STATUS_CHARGING;
787
Paul Kocialkowski7f93e1f2017-04-25 17:09:05 +0200788 sbs_status_correct(chip->client, &ret);
789
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800790 if (chip->last_state != ret) {
791 chip->poll_time = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100792 power_supply_changed(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700793 return;
794 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800795 if (chip->poll_time > 0) {
796 schedule_delayed_work(&chip->work, HZ);
797 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700798 return;
799 }
800}
801
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100802static const struct power_supply_desc sbs_default_desc = {
803 .type = POWER_SUPPLY_TYPE_BATTERY,
804 .properties = sbs_properties,
805 .num_properties = ARRAY_SIZE(sbs_properties),
806 .get_property = sbs_get_property,
807 .external_power_changed = sbs_external_power_changed,
808};
809
Bill Pembertonc8afa642012-11-19 13:22:23 -0500810static int sbs_probe(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700811 const struct i2c_device_id *id)
812{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800813 struct sbs_info *chip;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100814 struct power_supply_desc *sbs_desc;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800815 struct sbs_platform_data *pdata = client->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100816 struct power_supply_config psy_cfg = {};
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700817 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800818 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -0800819
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100820 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
821 sizeof(*sbs_desc), GFP_KERNEL);
822 if (!sbs_desc)
Rhyland Klein52f56c62011-12-05 17:50:49 -0800823 return -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100824
825 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
826 dev_name(&client->dev));
827 if (!sbs_desc->name)
828 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700829
Phil Reid9239a862016-07-25 10:42:57 +0800830 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100831 if (!chip)
832 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700833
Brian Norris76b16f4c2018-06-12 13:20:41 -0700834 chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800835 chip->client = client;
836 chip->enable_detection = false;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100837 psy_cfg.of_node = client->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100838 psy_cfg.drv_data = chip;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800839 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
Shawn Nematbakhshfe8a6532017-06-13 10:53:25 -0700840 mutex_init(&chip->mode_lock);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700841
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200842 /* use pdata if available, fall back to DT properties,
843 * or hardcoded defaults if not
844 */
845 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
846 &chip->i2c_retry_count);
847 if (rc)
Phil Reid389958b2016-09-20 09:01:12 +0800848 chip->i2c_retry_count = 0;
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700849
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200850 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
851 &chip->poll_retry_count);
852 if (rc)
853 chip->poll_retry_count = 0;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800854
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200855 if (pdata) {
856 chip->poll_retry_count = pdata->poll_retry_count;
857 chip->i2c_retry_count = pdata->i2c_retry_count;
858 }
Phil Reid389958b2016-09-20 09:01:12 +0800859 chip->i2c_retry_count = chip->i2c_retry_count + 1;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800860
861 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
862 "sbs,battery-detect", GPIOD_IN);
863 if (IS_ERR(chip->gpio_detect)) {
864 dev_err(&client->dev, "Failed to get gpio: %ld\n",
865 PTR_ERR(chip->gpio_detect));
866 return PTR_ERR(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800867 }
868
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800869 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700870
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800871 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800872 goto skip_gpio;
873
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800874 irq = gpiod_to_irq(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800875 if (irq <= 0) {
876 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800877 goto skip_gpio;
878 }
879
Phil Reidd2cec822016-07-25 10:42:58 +0800880 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
Ryosuke Saitobb8fe8e2017-04-21 12:13:17 +0900881 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Phil Reidd2cec822016-07-25 10:42:58 +0800882 dev_name(&client->dev), chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800883 if (rc) {
884 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800885 goto skip_gpio;
886 }
887
Rhyland Kleinbb879102011-02-28 16:55:28 -0800888skip_gpio:
Olof Johanssona22b41a2012-09-06 11:32:29 -0700889 /*
Frans Klaverf4ed9502015-06-10 14:16:56 +0200890 * Before we register, we might need to make sure we can actually talk
Olof Johanssona22b41a2012-09-06 11:32:29 -0700891 * to the battery.
892 */
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800893 if (!(force_load || chip->gpio_detect)) {
Frans Klaverf4ed9502015-06-10 14:16:56 +0200894 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
895
896 if (rc < 0) {
897 dev_err(&client->dev, "%s: Failed to get device status\n",
898 __func__);
899 goto exit_psupply;
900 }
Olof Johanssona22b41a2012-09-06 11:32:29 -0700901 }
Rhyland Kleinbb879102011-02-28 16:55:28 -0800902
Phil Reid492ff9d82016-07-25 10:42:59 +0800903 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100904 &psy_cfg);
905 if (IS_ERR(chip->power_supply)) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700906 dev_err(&client->dev,
907 "%s: Failed to register power supply\n", __func__);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100908 rc = PTR_ERR(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800909 goto exit_psupply;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700910 }
911
912 dev_info(&client->dev,
913 "%s: battery gas gauge device registered\n", client->name);
914
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800915 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700916
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800917 chip->enable_detection = true;
Rhyland Kleinee177d92011-05-24 12:06:50 -0700918
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700919 return 0;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800920
921exit_psupply:
Rhyland Kleinbb879102011-02-28 16:55:28 -0800922 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700923}
924
Bill Pemberton415ec692012-11-19 13:26:07 -0500925static int sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700926{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800927 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700928
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800929 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700930
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700931 return 0;
932}
933
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100934#if defined CONFIG_PM_SLEEP
935
936static int sbs_suspend(struct device *dev)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700937{
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100938 struct i2c_client *client = to_i2c_client(dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800939 struct sbs_info *chip = i2c_get_clientdata(client);
Brian Norris76b16f4c2018-06-12 13:20:41 -0700940 int ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700941
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800942 if (chip->poll_time > 0)
943 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700944
Brian Norris76b16f4c2018-06-12 13:20:41 -0700945 if (chip->flags & SBS_FLAGS_TI_BQ20Z75) {
946 /* Write to manufacturer access with sleep command. */
947 ret = sbs_write_word_data(client,
948 sbs_data[REG_MANUFACTURER_DATA].addr,
949 MANUFACTURER_ACCESS_SLEEP);
950 if (chip->is_present && ret < 0)
951 return ret;
952 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700953
954 return 0;
955}
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100956
957static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
958#define SBS_PM_OPS (&sbs_pm_ops)
959
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700960#else
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100961#define SBS_PM_OPS NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700962#endif
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700963
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800964static const struct i2c_device_id sbs_id[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700965 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800966 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700967 {}
968};
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800969MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700970
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200971static const struct of_device_id sbs_dt_ids[] = {
972 { .compatible = "sbs,sbs-battery" },
Brian Norris76b16f4c2018-06-12 13:20:41 -0700973 {
974 .compatible = "ti,bq20z75",
975 .data = (void *)SBS_FLAGS_TI_BQ20Z75,
976 },
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200977 { }
978};
979MODULE_DEVICE_TABLE(of, sbs_dt_ids);
980
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800981static struct i2c_driver sbs_battery_driver = {
982 .probe = sbs_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500983 .remove = sbs_remove,
Phil Reidcda3b012017-05-01 16:49:58 +0800984 .alert = sbs_alert,
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800985 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700986 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800987 .name = "sbs-battery",
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200988 .of_match_table = sbs_dt_ids,
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100989 .pm = SBS_PM_OPS,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700990 },
991};
Axel Lin5ff92e72012-01-21 14:42:54 +0800992module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700993
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800994MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700995MODULE_LICENSE("GPL");
Frans Klaverf4ed9502015-06-10 14:16:56 +0200996
997module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
998MODULE_PARM_DESC(force_load,
999 "Attempt to load the driver even if no battery is connected");