blob: 8bb2eb38eb1c872e7a50f7aa765c9c4c712c79f1 [file] [log] [blame]
Rhyland Kleina7640bf2010-09-05 15:31:23 -07001/*
Rhyland Klein3ddca0622011-12-05 17:50:46 -08002 * Gas Gauge driver for SBS Compliant Batteries
Rhyland Kleina7640bf2010-09-05 15:31:23 -07003 *
4 * Copyright (c) 2010, NVIDIA Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/err.h>
25#include <linux/power_supply.h>
26#include <linux/i2c.h>
27#include <linux/slab.h>
Rhyland Kleinbb879102011-02-28 16:55:28 -080028#include <linux/interrupt.h>
Phil Reid3b5dd3a2016-09-01 15:50:52 +080029#include <linux/gpio/consumer.h>
Sachin Kamat075ed032013-03-14 15:49:46 +053030#include <linux/of.h>
Frans Klaverf4ed9502015-06-10 14:16:56 +020031#include <linux/stat.h>
Rhyland Kleinbb879102011-02-28 16:55:28 -080032
Rhyland Klein3ddca0622011-12-05 17:50:46 -080033#include <linux/power/sbs-battery.h>
Rhyland Kleina7640bf2010-09-05 15:31:23 -070034
35enum {
36 REG_MANUFACTURER_DATA,
37 REG_TEMPERATURE,
38 REG_VOLTAGE,
39 REG_CURRENT,
40 REG_CAPACITY,
41 REG_TIME_TO_EMPTY,
42 REG_TIME_TO_FULL,
43 REG_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -070044 REG_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070045 REG_CYCLE_COUNT,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070046 REG_SERIAL_NUMBER,
47 REG_REMAINING_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080048 REG_REMAINING_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070049 REG_FULL_CHARGE_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080050 REG_FULL_CHARGE_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070051 REG_DESIGN_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080052 REG_DESIGN_CAPACITY_CHARGE,
Simon Que4495b0a2014-08-04 13:47:46 +020053 REG_DESIGN_VOLTAGE_MIN,
54 REG_DESIGN_VOLTAGE_MAX,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020055 REG_MANUFACTURER,
56 REG_MODEL_NAME,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070057};
58
Rhyland Klein51d07562011-01-25 11:10:06 -080059/* Battery Mode defines */
60#define BATTERY_MODE_OFFSET 0x03
61#define BATTERY_MODE_MASK 0x8000
Rhyland Klein3ddca0622011-12-05 17:50:46 -080062enum sbs_battery_mode {
Rhyland Klein51d07562011-01-25 11:10:06 -080063 BATTERY_MODE_AMPS,
64 BATTERY_MODE_WATTS
65};
66
Rhyland Kleina7640bf2010-09-05 15:31:23 -070067/* manufacturer access defines */
68#define MANUFACTURER_ACCESS_STATUS 0x0006
69#define MANUFACTURER_ACCESS_SLEEP 0x0011
70
71/* battery status value bits */
Joshua Clayton957cb722016-08-11 09:59:12 -070072#define BATTERY_INITIALIZED 0x80
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070073#define BATTERY_DISCHARGING 0x40
Rhyland Kleina7640bf2010-09-05 15:31:23 -070074#define BATTERY_FULL_CHARGED 0x20
75#define BATTERY_FULL_DISCHARGED 0x10
76
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020077/* min_value and max_value are only valid for numerical data */
Rhyland Klein3ddca0622011-12-05 17:50:46 -080078#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
Rhyland Kleina7640bf2010-09-05 15:31:23 -070079 .psp = _psp, \
80 .addr = _addr, \
81 .min_value = _min_value, \
82 .max_value = _max_value, \
83}
84
Rhyland Klein3ddca0622011-12-05 17:50:46 -080085static const struct chip_data {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070086 enum power_supply_property psp;
87 u8 addr;
88 int min_value;
89 int max_value;
Rhyland Klein3ddca0622011-12-05 17:50:46 -080090} sbs_data[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070091 [REG_MANUFACTURER_DATA] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080092 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070093 [REG_TEMPERATURE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080094 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070095 [REG_VOLTAGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080096 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070097 [REG_CURRENT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080098 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070099 [REG_CAPACITY] =
Nikolaus Vossb1f092f2012-04-25 08:59:03 +0200100 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700101 [REG_REMAINING_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800102 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800103 [REG_REMAINING_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800104 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700105 [REG_FULL_CHARGE_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800106 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800107 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800108 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700109 [REG_TIME_TO_EMPTY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800110 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700111 [REG_TIME_TO_FULL] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800112 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700113 [REG_STATUS] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800114 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
Joshua Clayton957cb722016-08-11 09:59:12 -0700115 [REG_CAPACITY_LEVEL] =
116 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700117 [REG_CYCLE_COUNT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800118 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700119 [REG_DESIGN_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800120 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800121 [REG_DESIGN_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800122 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
Simon Que4495b0a2014-08-04 13:47:46 +0200123 [REG_DESIGN_VOLTAGE_MIN] =
124 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
125 [REG_DESIGN_VOLTAGE_MAX] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800126 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700127 [REG_SERIAL_NUMBER] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800128 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200129 /* Properties of type `const char *' */
130 [REG_MANUFACTURER] =
131 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
132 [REG_MODEL_NAME] =
133 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700134};
135
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800136static enum power_supply_property sbs_properties[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700137 POWER_SUPPLY_PROP_STATUS,
Joshua Clayton957cb722016-08-11 09:59:12 -0700138 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700139 POWER_SUPPLY_PROP_HEALTH,
140 POWER_SUPPLY_PROP_PRESENT,
141 POWER_SUPPLY_PROP_TECHNOLOGY,
142 POWER_SUPPLY_PROP_CYCLE_COUNT,
143 POWER_SUPPLY_PROP_VOLTAGE_NOW,
144 POWER_SUPPLY_PROP_CURRENT_NOW,
145 POWER_SUPPLY_PROP_CAPACITY,
146 POWER_SUPPLY_PROP_TEMP,
147 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
148 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
149 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Simon Que4495b0a2014-08-04 13:47:46 +0200150 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700151 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
152 POWER_SUPPLY_PROP_ENERGY_NOW,
153 POWER_SUPPLY_PROP_ENERGY_FULL,
154 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
Rhyland Klein51d07562011-01-25 11:10:06 -0800155 POWER_SUPPLY_PROP_CHARGE_NOW,
156 POWER_SUPPLY_PROP_CHARGE_FULL,
157 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200158 /* Properties of type `const char *' */
159 POWER_SUPPLY_PROP_MANUFACTURER,
160 POWER_SUPPLY_PROP_MODEL_NAME
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700161};
162
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800163struct sbs_info {
Rhyland Kleinbb879102011-02-28 16:55:28 -0800164 struct i2c_client *client;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100165 struct power_supply *power_supply;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800166 bool is_present;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800167 struct gpio_desc *gpio_detect;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800168 bool enable_detection;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700169 int last_state;
170 int poll_time;
Phil Reid389958b2016-09-20 09:01:12 +0800171 u32 i2c_retry_count;
172 u32 poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700173 struct delayed_work work;
174 int ignore_changes;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700175};
176
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200177static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
178static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
Frans Klaverf4ed9502015-06-10 14:16:56 +0200179static bool force_load;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200180
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800181static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700182{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800183 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800184 s32 ret = 0;
185 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700186
Phil Reid389958b2016-09-20 09:01:12 +0800187 retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800188
189 while (retries > 0) {
190 ret = i2c_smbus_read_word_data(client, address);
191 if (ret >= 0)
192 break;
193 retries--;
194 }
195
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700196 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800197 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700198 "%s: i2c read at address 0x%x failed\n",
199 __func__, address);
200 return ret;
201 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800202
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700203 return le16_to_cpu(ret);
204}
205
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200206static int sbs_read_string_data(struct i2c_client *client, u8 address,
207 char *values)
208{
209 struct sbs_info *chip = i2c_get_clientdata(client);
210 s32 ret = 0, block_length = 0;
211 int retries_length = 1, retries_block = 1;
212 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
213
Phil Reid389958b2016-09-20 09:01:12 +0800214 retries_length = chip->i2c_retry_count;
215 retries_block = chip->i2c_retry_count;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200216
217 /* Adapter needs to support these two functions */
218 if (!i2c_check_functionality(client->adapter,
219 I2C_FUNC_SMBUS_BYTE_DATA |
220 I2C_FUNC_SMBUS_I2C_BLOCK)){
221 return -ENODEV;
222 }
223
224 /* Get the length of block data */
225 while (retries_length > 0) {
226 ret = i2c_smbus_read_byte_data(client, address);
227 if (ret >= 0)
228 break;
229 retries_length--;
230 }
231
232 if (ret < 0) {
233 dev_dbg(&client->dev,
234 "%s: i2c read at address 0x%x failed\n",
235 __func__, address);
236 return ret;
237 }
238
239 /* block_length does not include NULL terminator */
240 block_length = ret;
241 if (block_length > I2C_SMBUS_BLOCK_MAX) {
242 dev_err(&client->dev,
243 "%s: Returned block_length is longer than 0x%x\n",
244 __func__, I2C_SMBUS_BLOCK_MAX);
245 return -EINVAL;
246 }
247
248 /* Get the block data */
249 while (retries_block > 0) {
250 ret = i2c_smbus_read_i2c_block_data(
251 client, address,
252 block_length + 1, block_buffer);
253 if (ret >= 0)
254 break;
255 retries_block--;
256 }
257
258 if (ret < 0) {
259 dev_dbg(&client->dev,
260 "%s: i2c read at address 0x%x failed\n",
261 __func__, address);
262 return ret;
263 }
264
265 /* block_buffer[0] == block_length */
266 memcpy(values, block_buffer + 1, block_length);
267 values[block_length] = '\0';
268
269 return le16_to_cpu(ret);
270}
271
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800272static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700273 u16 value)
274{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800275 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800276 s32 ret = 0;
277 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700278
Phil Reid389958b2016-09-20 09:01:12 +0800279 retries = chip->i2c_retry_count;
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800280
281 while (retries > 0) {
282 ret = i2c_smbus_write_word_data(client, address,
283 le16_to_cpu(value));
284 if (ret >= 0)
285 break;
286 retries--;
287 }
288
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700289 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800290 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700291 "%s: i2c write to address 0x%x failed\n",
292 __func__, address);
293 return ret;
294 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800295
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700296 return 0;
297}
298
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800299static int sbs_get_battery_presence_and_health(
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700300 struct i2c_client *client, enum power_supply_property psp,
301 union power_supply_propval *val)
302{
303 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800304 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800305
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800306 if (psp == POWER_SUPPLY_PROP_PRESENT && chip->gpio_detect) {
307 ret = gpiod_get_value_cansleep(chip->gpio_detect);
308 if (ret < 0)
309 return ret;
310 val->intval = ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800311 chip->is_present = val->intval;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800312 return ret;
313 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700314
Guenter Roeck17c6d392016-09-08 19:10:00 -0700315 /*
316 * Write to ManufacturerAccess with ManufacturerAccess command
317 * and then read the status. Do not check for error on the write
318 * since not all batteries implement write access to this command,
319 * while others mandate it.
320 */
321 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
322 MANUFACTURER_ACCESS_STATUS);
323
324 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800325 if (ret < 0) {
326 if (psp == POWER_SUPPLY_PROP_PRESENT)
327 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700328 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800329 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700330
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800331 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
332 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700333 val->intval = 0;
334 return 0;
335 }
336
337 /* Mask the upper nibble of 2nd byte and
338 * lower byte of response then
339 * shift the result by 8 to get status*/
340 ret &= 0x0F00;
341 ret >>= 8;
342 if (psp == POWER_SUPPLY_PROP_PRESENT) {
343 if (ret == 0x0F)
344 /* battery removed */
345 val->intval = 0;
346 else
347 val->intval = 1;
348 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
349 if (ret == 0x09)
350 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
351 else if (ret == 0x0B)
352 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
353 else if (ret == 0x0C)
354 val->intval = POWER_SUPPLY_HEALTH_DEAD;
355 else
356 val->intval = POWER_SUPPLY_HEALTH_GOOD;
357 }
358
359 return 0;
360}
361
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800362static int sbs_get_battery_property(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700363 int reg_offset, enum power_supply_property psp,
364 union power_supply_propval *val)
365{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800366 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700367 s32 ret;
368
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800369 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700370 if (ret < 0)
371 return ret;
372
373 /* returned values are 16 bit */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800374 if (sbs_data[reg_offset].min_value < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700375 ret = (s16)ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700376
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800377 if (ret >= sbs_data[reg_offset].min_value &&
378 ret <= sbs_data[reg_offset].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700379 val->intval = ret;
Joshua Clayton957cb722016-08-11 09:59:12 -0700380 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
381 if (!(ret & BATTERY_INITIALIZED))
382 val->intval =
383 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
384 else if (ret & BATTERY_FULL_CHARGED)
385 val->intval =
386 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
387 else if (ret & BATTERY_FULL_DISCHARGED)
388 val->intval =
389 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
390 else
391 val->intval =
392 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700393 return 0;
Joshua Clayton957cb722016-08-11 09:59:12 -0700394 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
395 return 0;
396 }
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700397
398 if (ret & BATTERY_FULL_CHARGED)
399 val->intval = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700400 else if (ret & BATTERY_DISCHARGING)
401 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
402 else
403 val->intval = POWER_SUPPLY_STATUS_CHARGING;
404
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800405 if (chip->poll_time == 0)
406 chip->last_state = val->intval;
407 else if (chip->last_state != val->intval) {
408 cancel_delayed_work_sync(&chip->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100409 power_supply_changed(chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800410 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700411 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700412 } else {
413 if (psp == POWER_SUPPLY_PROP_STATUS)
414 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
415 else
416 val->intval = 0;
417 }
418
419 return 0;
420}
421
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200422static int sbs_get_battery_string_property(struct i2c_client *client,
423 int reg_offset, enum power_supply_property psp, char *val)
424{
425 s32 ret;
426
427 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
428
429 if (ret < 0)
430 return ret;
431
432 return 0;
433}
434
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800435static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700436 enum power_supply_property psp, union power_supply_propval *val)
437{
438#define BASE_UNIT_CONVERSION 1000
439#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800440#define TIME_UNIT_CONVERSION 60
441#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700442 switch (psp) {
443 case POWER_SUPPLY_PROP_ENERGY_NOW:
444 case POWER_SUPPLY_PROP_ENERGY_FULL:
445 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800446 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800447 * Convert to µWh
448 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700449 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
450 break;
451
452 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Simon Que4495b0a2014-08-04 13:47:46 +0200453 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700454 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
455 case POWER_SUPPLY_PROP_CURRENT_NOW:
Rhyland Klein51d07562011-01-25 11:10:06 -0800456 case POWER_SUPPLY_PROP_CHARGE_NOW:
457 case POWER_SUPPLY_PROP_CHARGE_FULL:
458 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700459 val->intval *= BASE_UNIT_CONVERSION;
460 break;
461
462 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800463 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800464 * so convert it to 0.1°C
465 */
466 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700467 break;
468
469 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
470 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800471 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800472 * Convert to seconds
473 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700474 val->intval *= TIME_UNIT_CONVERSION;
475 break;
476
477 default:
478 dev_dbg(&client->dev,
479 "%s: no need for unit conversion %d\n", __func__, psp);
480 }
481}
482
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800483static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
484 enum sbs_battery_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800485{
486 int ret, original_val;
487
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800488 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800489 if (original_val < 0)
490 return original_val;
491
492 if ((original_val & BATTERY_MODE_MASK) == mode)
493 return mode;
494
495 if (mode == BATTERY_MODE_AMPS)
496 ret = original_val & ~BATTERY_MODE_MASK;
497 else
498 ret = original_val | BATTERY_MODE_MASK;
499
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800500 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800501 if (ret < 0)
502 return ret;
503
504 return original_val & BATTERY_MODE_MASK;
505}
506
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800507static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700508 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700509 union power_supply_propval *val)
510{
511 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800512 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800513
514 if (power_supply_is_amp_property(psp))
515 mode = BATTERY_MODE_AMPS;
516
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800517 mode = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800518 if (mode < 0)
519 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700520
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800521 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700522 if (ret < 0)
523 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700524
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700525 if (psp == POWER_SUPPLY_PROP_CAPACITY) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800526 /* sbs spec says that this can be >100 %
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700527 * even if max value is 100 % */
528 val->intval = min(ret, 100);
529 } else
530 val->intval = ret;
531
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800532 ret = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800533 if (ret < 0)
534 return ret;
535
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700536 return 0;
537}
538
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800539static char sbs_serial[5];
540static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700541 union power_supply_propval *val)
542{
543 int ret;
544
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800545 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700546 if (ret < 0)
547 return ret;
548
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800549 ret = sprintf(sbs_serial, "%04x", ret);
550 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700551
552 return 0;
553}
554
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800555static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800556 enum power_supply_property psp)
557{
558 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800559 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
560 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800561 return count;
562
563 dev_warn(&client->dev,
564 "%s: Invalid Property - %d\n", __func__, psp);
565
566 return -EINVAL;
567}
568
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800569static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700570 enum power_supply_property psp,
571 union power_supply_propval *val)
572{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800573 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100574 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800575 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700576
577 switch (psp) {
578 case POWER_SUPPLY_PROP_PRESENT:
579 case POWER_SUPPLY_PROP_HEALTH:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800580 ret = sbs_get_battery_presence_and_health(client, psp, val);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800581 if (psp == POWER_SUPPLY_PROP_PRESENT)
582 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700583 break;
584
585 case POWER_SUPPLY_PROP_TECHNOLOGY:
586 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
Nikolaus Voss5da50982012-05-09 08:30:44 +0200587 goto done; /* don't trigger power_supply_changed()! */
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700588
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700589 case POWER_SUPPLY_PROP_ENERGY_NOW:
590 case POWER_SUPPLY_PROP_ENERGY_FULL:
591 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800592 case POWER_SUPPLY_PROP_CHARGE_NOW:
593 case POWER_SUPPLY_PROP_CHARGE_FULL:
594 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700595 case POWER_SUPPLY_PROP_CAPACITY:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800596 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800597 if (ret < 0)
598 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700599
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800600 ret = sbs_get_battery_capacity(client, ret, psp, val);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700601 break;
602
603 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800604 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700605 break;
606
607 case POWER_SUPPLY_PROP_STATUS:
Joshua Clayton957cb722016-08-11 09:59:12 -0700608 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700609 case POWER_SUPPLY_PROP_CYCLE_COUNT:
610 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
611 case POWER_SUPPLY_PROP_CURRENT_NOW:
612 case POWER_SUPPLY_PROP_TEMP:
613 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
614 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Simon Que4495b0a2014-08-04 13:47:46 +0200615 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700616 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800617 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800618 if (ret < 0)
619 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700620
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800621 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700622 break;
623
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200624 case POWER_SUPPLY_PROP_MODEL_NAME:
625 ret = sbs_get_property_index(client, psp);
626 if (ret < 0)
627 break;
628
629 ret = sbs_get_battery_string_property(client, ret, psp,
630 model_name);
631 val->strval = model_name;
632 break;
633
634 case POWER_SUPPLY_PROP_MANUFACTURER:
635 ret = sbs_get_property_index(client, psp);
636 if (ret < 0)
637 break;
638
639 ret = sbs_get_battery_string_property(client, ret, psp,
640 manufacturer);
641 val->strval = manufacturer;
642 break;
643
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700644 default:
645 dev_err(&client->dev,
646 "%s: INVALID property\n", __func__);
647 return -EINVAL;
648 }
649
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800650 if (!chip->enable_detection)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800651 goto done;
652
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800653 if (!chip->gpio_detect &&
654 chip->is_present != (ret >= 0)) {
655 chip->is_present = (ret >= 0);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100656 power_supply_changed(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800657 }
658
659done:
660 if (!ret) {
661 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800662 sbs_unit_adjustment(client, psp, val);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800663 }
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700664
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700665 dev_dbg(&client->dev,
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800666 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700667
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800668 if (ret && chip->is_present)
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800669 return ret;
670
671 /* battery not present, so return NODATA for properties */
672 if (ret)
673 return -ENODATA;
674
675 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700676}
677
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800678static irqreturn_t sbs_irq(int irq, void *devid)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800679{
Phil Reidd2cec822016-07-25 10:42:58 +0800680 struct sbs_info *chip = devid;
681 struct power_supply *battery = chip->power_supply;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800682 int ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800683
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800684 ret = gpiod_get_value_cansleep(chip->gpio_detect);
685 if (ret < 0)
686 return ret;
687 chip->is_present = ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800688 power_supply_changed(battery);
689
690 return IRQ_HANDLED;
691}
692
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800693static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700694{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100695 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700696
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800697 if (chip->ignore_changes > 0) {
698 chip->ignore_changes--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700699 return;
700 }
701
702 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800703 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700704
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800705 schedule_delayed_work(&chip->work, HZ);
Phil Reid389958b2016-09-20 09:01:12 +0800706 chip->poll_time = chip->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700707}
708
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800709static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700710{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800711 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700712 s32 ret;
713
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800714 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700715
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800716 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700717 /* if the read failed, give up on this work */
718 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800719 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700720 return;
721 }
722
723 if (ret & BATTERY_FULL_CHARGED)
724 ret = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700725 else if (ret & BATTERY_DISCHARGING)
726 ret = POWER_SUPPLY_STATUS_DISCHARGING;
727 else
728 ret = POWER_SUPPLY_STATUS_CHARGING;
729
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800730 if (chip->last_state != ret) {
731 chip->poll_time = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100732 power_supply_changed(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700733 return;
734 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800735 if (chip->poll_time > 0) {
736 schedule_delayed_work(&chip->work, HZ);
737 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700738 return;
739 }
740}
741
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100742static const struct power_supply_desc sbs_default_desc = {
743 .type = POWER_SUPPLY_TYPE_BATTERY,
744 .properties = sbs_properties,
745 .num_properties = ARRAY_SIZE(sbs_properties),
746 .get_property = sbs_get_property,
747 .external_power_changed = sbs_external_power_changed,
748};
749
Bill Pembertonc8afa642012-11-19 13:22:23 -0500750static int sbs_probe(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700751 const struct i2c_device_id *id)
752{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800753 struct sbs_info *chip;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100754 struct power_supply_desc *sbs_desc;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800755 struct sbs_platform_data *pdata = client->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100756 struct power_supply_config psy_cfg = {};
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700757 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800758 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -0800759
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100760 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
761 sizeof(*sbs_desc), GFP_KERNEL);
762 if (!sbs_desc)
Rhyland Klein52f56c62011-12-05 17:50:49 -0800763 return -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100764
765 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
766 dev_name(&client->dev));
767 if (!sbs_desc->name)
768 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700769
Phil Reid9239a862016-07-25 10:42:57 +0800770 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100771 if (!chip)
772 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700773
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800774 chip->client = client;
775 chip->enable_detection = false;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100776 psy_cfg.of_node = client->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100777 psy_cfg.drv_data = chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700778 /* ignore first notification of external change, it is generated
779 * from the power_supply_register call back
780 */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800781 chip->ignore_changes = 1;
782 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700783
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200784 /* use pdata if available, fall back to DT properties,
785 * or hardcoded defaults if not
786 */
787 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
788 &chip->i2c_retry_count);
789 if (rc)
Phil Reid389958b2016-09-20 09:01:12 +0800790 chip->i2c_retry_count = 0;
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700791
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200792 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
793 &chip->poll_retry_count);
794 if (rc)
795 chip->poll_retry_count = 0;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800796
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200797 if (pdata) {
798 chip->poll_retry_count = pdata->poll_retry_count;
799 chip->i2c_retry_count = pdata->i2c_retry_count;
800 }
Phil Reid389958b2016-09-20 09:01:12 +0800801 chip->i2c_retry_count = chip->i2c_retry_count + 1;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800802
803 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
804 "sbs,battery-detect", GPIOD_IN);
805 if (IS_ERR(chip->gpio_detect)) {
806 dev_err(&client->dev, "Failed to get gpio: %ld\n",
807 PTR_ERR(chip->gpio_detect));
808 return PTR_ERR(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800809 }
810
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800811 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700812
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800813 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800814 goto skip_gpio;
815
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800816 irq = gpiod_to_irq(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800817 if (irq <= 0) {
818 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800819 goto skip_gpio;
820 }
821
Phil Reidd2cec822016-07-25 10:42:58 +0800822 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
Rhyland Kleinbb879102011-02-28 16:55:28 -0800823 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Phil Reidd2cec822016-07-25 10:42:58 +0800824 dev_name(&client->dev), chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800825 if (rc) {
826 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800827 goto skip_gpio;
828 }
829
Rhyland Kleinbb879102011-02-28 16:55:28 -0800830skip_gpio:
Olof Johanssona22b41a2012-09-06 11:32:29 -0700831 /*
Frans Klaverf4ed9502015-06-10 14:16:56 +0200832 * Before we register, we might need to make sure we can actually talk
Olof Johanssona22b41a2012-09-06 11:32:29 -0700833 * to the battery.
834 */
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800835 if (!(force_load || chip->gpio_detect)) {
Frans Klaverf4ed9502015-06-10 14:16:56 +0200836 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
837
838 if (rc < 0) {
839 dev_err(&client->dev, "%s: Failed to get device status\n",
840 __func__);
841 goto exit_psupply;
842 }
Olof Johanssona22b41a2012-09-06 11:32:29 -0700843 }
Rhyland Kleinbb879102011-02-28 16:55:28 -0800844
Phil Reid492ff9d82016-07-25 10:42:59 +0800845 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100846 &psy_cfg);
847 if (IS_ERR(chip->power_supply)) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700848 dev_err(&client->dev,
849 "%s: Failed to register power supply\n", __func__);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100850 rc = PTR_ERR(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800851 goto exit_psupply;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700852 }
853
854 dev_info(&client->dev,
855 "%s: battery gas gauge device registered\n", client->name);
856
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800857 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700858
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800859 chip->enable_detection = true;
Rhyland Kleinee177d92011-05-24 12:06:50 -0700860
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700861 return 0;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800862
863exit_psupply:
Rhyland Kleinbb879102011-02-28 16:55:28 -0800864 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700865}
866
Bill Pemberton415ec692012-11-19 13:26:07 -0500867static int sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700868{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800869 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700870
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800871 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700872
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700873 return 0;
874}
875
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100876#if defined CONFIG_PM_SLEEP
877
878static int sbs_suspend(struct device *dev)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700879{
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100880 struct i2c_client *client = to_i2c_client(dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800881 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700882
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800883 if (chip->poll_time > 0)
884 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700885
Guenter Roeck17c6d392016-09-08 19:10:00 -0700886 /*
887 * Write to manufacturer access with sleep command.
888 * Support is manufacturer dependend, so ignore errors.
889 */
890 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700891 MANUFACTURER_ACCESS_SLEEP);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700892
893 return 0;
894}
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100895
896static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
897#define SBS_PM_OPS (&sbs_pm_ops)
898
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700899#else
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100900#define SBS_PM_OPS NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700901#endif
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700902
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800903static const struct i2c_device_id sbs_id[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700904 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800905 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700906 {}
907};
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800908MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700909
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200910static const struct of_device_id sbs_dt_ids[] = {
911 { .compatible = "sbs,sbs-battery" },
912 { .compatible = "ti,bq20z75" },
913 { }
914};
915MODULE_DEVICE_TABLE(of, sbs_dt_ids);
916
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800917static struct i2c_driver sbs_battery_driver = {
918 .probe = sbs_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500919 .remove = sbs_remove,
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800920 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700921 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800922 .name = "sbs-battery",
Arnd Bergmann9edeaad2016-09-06 15:16:33 +0200923 .of_match_table = sbs_dt_ids,
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100924 .pm = SBS_PM_OPS,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700925 },
926};
Axel Lin5ff92e72012-01-21 14:42:54 +0800927module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700928
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800929MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700930MODULE_LICENSE("GPL");
Frans Klaverf4ed9502015-06-10 14:16:56 +0200931
932module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
933MODULE_PARM_DESC(force_load,
934 "Attempt to load the driver even if no battery is connected");