blob: d6226d68b5746d67cfb8561afa0906684280b76d [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>
29#include <linux/gpio.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,
44 REG_CYCLE_COUNT,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070045 REG_SERIAL_NUMBER,
46 REG_REMAINING_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080047 REG_REMAINING_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070048 REG_FULL_CHARGE_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080049 REG_FULL_CHARGE_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070050 REG_DESIGN_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080051 REG_DESIGN_CAPACITY_CHARGE,
Simon Que4495b0a2014-08-04 13:47:46 +020052 REG_DESIGN_VOLTAGE_MIN,
53 REG_DESIGN_VOLTAGE_MAX,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020054 REG_MANUFACTURER,
55 REG_MODEL_NAME,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070056};
57
Rhyland Klein51d07562011-01-25 11:10:06 -080058/* Battery Mode defines */
59#define BATTERY_MODE_OFFSET 0x03
60#define BATTERY_MODE_MASK 0x8000
Rhyland Klein3ddca0622011-12-05 17:50:46 -080061enum sbs_battery_mode {
Rhyland Klein51d07562011-01-25 11:10:06 -080062 BATTERY_MODE_AMPS,
63 BATTERY_MODE_WATTS
64};
65
Rhyland Kleina7640bf2010-09-05 15:31:23 -070066/* manufacturer access defines */
67#define MANUFACTURER_ACCESS_STATUS 0x0006
68#define MANUFACTURER_ACCESS_SLEEP 0x0011
69
70/* battery status value bits */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070071#define BATTERY_DISCHARGING 0x40
Rhyland Kleina7640bf2010-09-05 15:31:23 -070072#define BATTERY_FULL_CHARGED 0x20
73#define BATTERY_FULL_DISCHARGED 0x10
74
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +020075/* min_value and max_value are only valid for numerical data */
Rhyland Klein3ddca0622011-12-05 17:50:46 -080076#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
Rhyland Kleina7640bf2010-09-05 15:31:23 -070077 .psp = _psp, \
78 .addr = _addr, \
79 .min_value = _min_value, \
80 .max_value = _max_value, \
81}
82
Rhyland Klein3ddca0622011-12-05 17:50:46 -080083static const struct chip_data {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070084 enum power_supply_property psp;
85 u8 addr;
86 int min_value;
87 int max_value;
Rhyland Klein3ddca0622011-12-05 17:50:46 -080088} sbs_data[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070089 [REG_MANUFACTURER_DATA] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080090 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070091 [REG_TEMPERATURE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080092 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070093 [REG_VOLTAGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080094 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070095 [REG_CURRENT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080096 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070097 [REG_CAPACITY] =
Nikolaus Vossb1f092f2012-04-25 08:59:03 +020098 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070099 [REG_REMAINING_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800100 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800101 [REG_REMAINING_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800102 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700103 [REG_FULL_CHARGE_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800104 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800105 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800106 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700107 [REG_TIME_TO_EMPTY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800108 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700109 [REG_TIME_TO_FULL] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800110 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700111 [REG_STATUS] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800112 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700113 [REG_CYCLE_COUNT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800114 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700115 [REG_DESIGN_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800116 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800117 [REG_DESIGN_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800118 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
Simon Que4495b0a2014-08-04 13:47:46 +0200119 [REG_DESIGN_VOLTAGE_MIN] =
120 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
121 [REG_DESIGN_VOLTAGE_MAX] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800122 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700123 [REG_SERIAL_NUMBER] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800124 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200125 /* Properties of type `const char *' */
126 [REG_MANUFACTURER] =
127 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
128 [REG_MODEL_NAME] =
129 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700130};
131
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800132static enum power_supply_property sbs_properties[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700133 POWER_SUPPLY_PROP_STATUS,
134 POWER_SUPPLY_PROP_HEALTH,
135 POWER_SUPPLY_PROP_PRESENT,
136 POWER_SUPPLY_PROP_TECHNOLOGY,
137 POWER_SUPPLY_PROP_CYCLE_COUNT,
138 POWER_SUPPLY_PROP_VOLTAGE_NOW,
139 POWER_SUPPLY_PROP_CURRENT_NOW,
140 POWER_SUPPLY_PROP_CAPACITY,
141 POWER_SUPPLY_PROP_TEMP,
142 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
143 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
144 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Simon Que4495b0a2014-08-04 13:47:46 +0200145 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700146 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
147 POWER_SUPPLY_PROP_ENERGY_NOW,
148 POWER_SUPPLY_PROP_ENERGY_FULL,
149 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
Rhyland Klein51d07562011-01-25 11:10:06 -0800150 POWER_SUPPLY_PROP_CHARGE_NOW,
151 POWER_SUPPLY_PROP_CHARGE_FULL,
152 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200153 /* Properties of type `const char *' */
154 POWER_SUPPLY_PROP_MANUFACTURER,
155 POWER_SUPPLY_PROP_MODEL_NAME
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700156};
157
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800158struct sbs_info {
Rhyland Kleinbb879102011-02-28 16:55:28 -0800159 struct i2c_client *client;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100160 struct power_supply *power_supply;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800161 struct sbs_platform_data *pdata;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800162 bool is_present;
163 bool gpio_detect;
164 bool enable_detection;
165 int irq;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700166 int last_state;
167 int poll_time;
168 struct delayed_work work;
169 int ignore_changes;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700170};
171
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200172static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
173static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
Frans Klaverf4ed9502015-06-10 14:16:56 +0200174static bool force_load;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200175
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800176static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700177{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800178 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800179 s32 ret = 0;
180 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700181
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800182 if (chip->pdata)
183 retries = max(chip->pdata->i2c_retry_count + 1, 1);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800184
185 while (retries > 0) {
186 ret = i2c_smbus_read_word_data(client, address);
187 if (ret >= 0)
188 break;
189 retries--;
190 }
191
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700192 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800193 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700194 "%s: i2c read at address 0x%x failed\n",
195 __func__, address);
196 return ret;
197 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800198
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700199 return le16_to_cpu(ret);
200}
201
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200202static int sbs_read_string_data(struct i2c_client *client, u8 address,
203 char *values)
204{
205 struct sbs_info *chip = i2c_get_clientdata(client);
206 s32 ret = 0, block_length = 0;
207 int retries_length = 1, retries_block = 1;
208 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
209
210 if (chip->pdata) {
211 retries_length = max(chip->pdata->i2c_retry_count + 1, 1);
212 retries_block = max(chip->pdata->i2c_retry_count + 1, 1);
213 }
214
215 /* Adapter needs to support these two functions */
216 if (!i2c_check_functionality(client->adapter,
217 I2C_FUNC_SMBUS_BYTE_DATA |
218 I2C_FUNC_SMBUS_I2C_BLOCK)){
219 return -ENODEV;
220 }
221
222 /* Get the length of block data */
223 while (retries_length > 0) {
224 ret = i2c_smbus_read_byte_data(client, address);
225 if (ret >= 0)
226 break;
227 retries_length--;
228 }
229
230 if (ret < 0) {
231 dev_dbg(&client->dev,
232 "%s: i2c read at address 0x%x failed\n",
233 __func__, address);
234 return ret;
235 }
236
237 /* block_length does not include NULL terminator */
238 block_length = ret;
239 if (block_length > I2C_SMBUS_BLOCK_MAX) {
240 dev_err(&client->dev,
241 "%s: Returned block_length is longer than 0x%x\n",
242 __func__, I2C_SMBUS_BLOCK_MAX);
243 return -EINVAL;
244 }
245
246 /* Get the block data */
247 while (retries_block > 0) {
248 ret = i2c_smbus_read_i2c_block_data(
249 client, address,
250 block_length + 1, block_buffer);
251 if (ret >= 0)
252 break;
253 retries_block--;
254 }
255
256 if (ret < 0) {
257 dev_dbg(&client->dev,
258 "%s: i2c read at address 0x%x failed\n",
259 __func__, address);
260 return ret;
261 }
262
263 /* block_buffer[0] == block_length */
264 memcpy(values, block_buffer + 1, block_length);
265 values[block_length] = '\0';
266
267 return le16_to_cpu(ret);
268}
269
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800270static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700271 u16 value)
272{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800273 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800274 s32 ret = 0;
275 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700276
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800277 if (chip->pdata)
278 retries = max(chip->pdata->i2c_retry_count + 1, 1);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800279
280 while (retries > 0) {
281 ret = i2c_smbus_write_word_data(client, address,
282 le16_to_cpu(value));
283 if (ret >= 0)
284 break;
285 retries--;
286 }
287
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700288 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800289 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700290 "%s: i2c write to address 0x%x failed\n",
291 __func__, address);
292 return ret;
293 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800294
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700295 return 0;
296}
297
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800298static int sbs_get_battery_presence_and_health(
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700299 struct i2c_client *client, enum power_supply_property psp,
300 union power_supply_propval *val)
301{
302 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800303 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800304
305 if (psp == POWER_SUPPLY_PROP_PRESENT &&
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800306 chip->gpio_detect) {
307 ret = gpio_get_value(chip->pdata->battery_detect);
308 if (ret == chip->pdata->battery_detect_present)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800309 val->intval = 1;
310 else
311 val->intval = 0;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800312 chip->is_present = val->intval;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800313 return ret;
314 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700315
316 /* Write to ManufacturerAccess with
317 * ManufacturerAccess command and then
318 * read the status */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800319 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
320 MANUFACTURER_ACCESS_STATUS);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800321 if (ret < 0) {
322 if (psp == POWER_SUPPLY_PROP_PRESENT)
323 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700324 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800325 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700326
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800327 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800328 if (ret < 0)
329 return ret;
330
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;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700380 if (psp != POWER_SUPPLY_PROP_STATUS)
381 return 0;
382
383 if (ret & BATTERY_FULL_CHARGED)
384 val->intval = POWER_SUPPLY_STATUS_FULL;
385 else if (ret & BATTERY_FULL_DISCHARGED)
386 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
387 else if (ret & BATTERY_DISCHARGING)
388 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
389 else
390 val->intval = POWER_SUPPLY_STATUS_CHARGING;
391
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800392 if (chip->poll_time == 0)
393 chip->last_state = val->intval;
394 else if (chip->last_state != val->intval) {
395 cancel_delayed_work_sync(&chip->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100396 power_supply_changed(chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800397 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700398 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700399 } else {
400 if (psp == POWER_SUPPLY_PROP_STATUS)
401 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
402 else
403 val->intval = 0;
404 }
405
406 return 0;
407}
408
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200409static int sbs_get_battery_string_property(struct i2c_client *client,
410 int reg_offset, enum power_supply_property psp, char *val)
411{
412 s32 ret;
413
414 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
415
416 if (ret < 0)
417 return ret;
418
419 return 0;
420}
421
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800422static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700423 enum power_supply_property psp, union power_supply_propval *val)
424{
425#define BASE_UNIT_CONVERSION 1000
426#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800427#define TIME_UNIT_CONVERSION 60
428#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700429 switch (psp) {
430 case POWER_SUPPLY_PROP_ENERGY_NOW:
431 case POWER_SUPPLY_PROP_ENERGY_FULL:
432 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800433 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800434 * Convert to µWh
435 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700436 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
437 break;
438
439 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Simon Que4495b0a2014-08-04 13:47:46 +0200440 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700441 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
442 case POWER_SUPPLY_PROP_CURRENT_NOW:
Rhyland Klein51d07562011-01-25 11:10:06 -0800443 case POWER_SUPPLY_PROP_CHARGE_NOW:
444 case POWER_SUPPLY_PROP_CHARGE_FULL:
445 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700446 val->intval *= BASE_UNIT_CONVERSION;
447 break;
448
449 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800450 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800451 * so convert it to 0.1°C
452 */
453 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700454 break;
455
456 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
457 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800458 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800459 * Convert to seconds
460 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700461 val->intval *= TIME_UNIT_CONVERSION;
462 break;
463
464 default:
465 dev_dbg(&client->dev,
466 "%s: no need for unit conversion %d\n", __func__, psp);
467 }
468}
469
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800470static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
471 enum sbs_battery_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800472{
473 int ret, original_val;
474
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800475 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800476 if (original_val < 0)
477 return original_val;
478
479 if ((original_val & BATTERY_MODE_MASK) == mode)
480 return mode;
481
482 if (mode == BATTERY_MODE_AMPS)
483 ret = original_val & ~BATTERY_MODE_MASK;
484 else
485 ret = original_val | BATTERY_MODE_MASK;
486
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800487 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800488 if (ret < 0)
489 return ret;
490
491 return original_val & BATTERY_MODE_MASK;
492}
493
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800494static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700495 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700496 union power_supply_propval *val)
497{
498 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800499 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800500
501 if (power_supply_is_amp_property(psp))
502 mode = BATTERY_MODE_AMPS;
503
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800504 mode = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800505 if (mode < 0)
506 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700507
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800508 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700509 if (ret < 0)
510 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700511
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700512 if (psp == POWER_SUPPLY_PROP_CAPACITY) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800513 /* sbs spec says that this can be >100 %
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700514 * even if max value is 100 % */
515 val->intval = min(ret, 100);
516 } else
517 val->intval = ret;
518
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800519 ret = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800520 if (ret < 0)
521 return ret;
522
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700523 return 0;
524}
525
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800526static char sbs_serial[5];
527static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700528 union power_supply_propval *val)
529{
530 int ret;
531
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800532 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700533 if (ret < 0)
534 return ret;
535
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800536 ret = sprintf(sbs_serial, "%04x", ret);
537 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700538
539 return 0;
540}
541
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800542static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800543 enum power_supply_property psp)
544{
545 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800546 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
547 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800548 return count;
549
550 dev_warn(&client->dev,
551 "%s: Invalid Property - %d\n", __func__, psp);
552
553 return -EINVAL;
554}
555
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800556static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700557 enum power_supply_property psp,
558 union power_supply_propval *val)
559{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800560 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100561 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800562 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700563
564 switch (psp) {
565 case POWER_SUPPLY_PROP_PRESENT:
566 case POWER_SUPPLY_PROP_HEALTH:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800567 ret = sbs_get_battery_presence_and_health(client, psp, val);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800568 if (psp == POWER_SUPPLY_PROP_PRESENT)
569 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700570 break;
571
572 case POWER_SUPPLY_PROP_TECHNOLOGY:
573 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
Nikolaus Voss5da50982012-05-09 08:30:44 +0200574 goto done; /* don't trigger power_supply_changed()! */
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700575
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700576 case POWER_SUPPLY_PROP_ENERGY_NOW:
577 case POWER_SUPPLY_PROP_ENERGY_FULL:
578 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800579 case POWER_SUPPLY_PROP_CHARGE_NOW:
580 case POWER_SUPPLY_PROP_CHARGE_FULL:
581 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700582 case POWER_SUPPLY_PROP_CAPACITY:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800583 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800584 if (ret < 0)
585 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700586
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800587 ret = sbs_get_battery_capacity(client, ret, psp, val);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700588 break;
589
590 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800591 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700592 break;
593
594 case POWER_SUPPLY_PROP_STATUS:
595 case POWER_SUPPLY_PROP_CYCLE_COUNT:
596 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
597 case POWER_SUPPLY_PROP_CURRENT_NOW:
598 case POWER_SUPPLY_PROP_TEMP:
599 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
600 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Simon Que4495b0a2014-08-04 13:47:46 +0200601 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700602 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800603 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800604 if (ret < 0)
605 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700606
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800607 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700608 break;
609
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200610 case POWER_SUPPLY_PROP_MODEL_NAME:
611 ret = sbs_get_property_index(client, psp);
612 if (ret < 0)
613 break;
614
615 ret = sbs_get_battery_string_property(client, ret, psp,
616 model_name);
617 val->strval = model_name;
618 break;
619
620 case POWER_SUPPLY_PROP_MANUFACTURER:
621 ret = sbs_get_property_index(client, psp);
622 if (ret < 0)
623 break;
624
625 ret = sbs_get_battery_string_property(client, ret, psp,
626 manufacturer);
627 val->strval = manufacturer;
628 break;
629
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700630 default:
631 dev_err(&client->dev,
632 "%s: INVALID property\n", __func__);
633 return -EINVAL;
634 }
635
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800636 if (!chip->enable_detection)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800637 goto done;
638
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800639 if (!chip->gpio_detect &&
640 chip->is_present != (ret >= 0)) {
641 chip->is_present = (ret >= 0);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100642 power_supply_changed(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800643 }
644
645done:
646 if (!ret) {
647 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800648 sbs_unit_adjustment(client, psp, val);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800649 }
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700650
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700651 dev_dbg(&client->dev,
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800652 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700653
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800654 if (ret && chip->is_present)
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800655 return ret;
656
657 /* battery not present, so return NODATA for properties */
658 if (ret)
659 return -ENODATA;
660
661 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700662}
663
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800664static irqreturn_t sbs_irq(int irq, void *devid)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800665{
666 struct power_supply *battery = devid;
667
668 power_supply_changed(battery);
669
670 return IRQ_HANDLED;
671}
672
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800673static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700674{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100675 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700676
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800677 if (chip->ignore_changes > 0) {
678 chip->ignore_changes--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700679 return;
680 }
681
682 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800683 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700684
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800685 schedule_delayed_work(&chip->work, HZ);
686 chip->poll_time = chip->pdata->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700687}
688
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800689static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700690{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800691 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700692 s32 ret;
693
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800694 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700695
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800696 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700697 /* if the read failed, give up on this work */
698 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800699 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700700 return;
701 }
702
703 if (ret & BATTERY_FULL_CHARGED)
704 ret = POWER_SUPPLY_STATUS_FULL;
705 else if (ret & BATTERY_FULL_DISCHARGED)
706 ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
707 else if (ret & BATTERY_DISCHARGING)
708 ret = POWER_SUPPLY_STATUS_DISCHARGING;
709 else
710 ret = POWER_SUPPLY_STATUS_CHARGING;
711
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800712 if (chip->last_state != ret) {
713 chip->poll_time = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100714 power_supply_changed(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700715 return;
716 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800717 if (chip->poll_time > 0) {
718 schedule_delayed_work(&chip->work, HZ);
719 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700720 return;
721 }
722}
723
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700724#if defined(CONFIG_OF)
725
726#include <linux/of_device.h>
727#include <linux/of_gpio.h>
728
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800729static const struct of_device_id sbs_dt_ids[] = {
730 { .compatible = "sbs,sbs-battery" },
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700731 { .compatible = "ti,bq20z75" },
732 { }
733};
Olof Johansson62df3932012-01-06 05:45:34 +0400734MODULE_DEVICE_TABLE(of, sbs_dt_ids);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700735
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800736static struct sbs_platform_data *sbs_of_populate_pdata(
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700737 struct i2c_client *client)
738{
739 struct device_node *of_node = client->dev.of_node;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800740 struct sbs_platform_data *pdata = client->dev.platform_data;
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700741 enum of_gpio_flags gpio_flags;
742 int rc;
743 u32 prop;
744
745 /* verify this driver matches this device */
746 if (!of_node)
747 return NULL;
748
749 /* if platform data is set, honor it */
750 if (pdata)
751 return pdata;
752
753 /* first make sure at least one property is set, otherwise
754 * it won't change behavior from running without pdata.
755 */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800756 if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
757 !of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
758 !of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700759 goto of_out;
760
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800761 pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700762 GFP_KERNEL);
763 if (!pdata)
764 goto of_out;
765
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800766 rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700767 if (!rc)
768 pdata->i2c_retry_count = prop;
769
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800770 rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700771 if (!rc)
772 pdata->poll_retry_count = prop;
773
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800774 if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700775 pdata->battery_detect = -1;
776 goto of_out;
777 }
778
779 pdata->battery_detect = of_get_named_gpio_flags(of_node,
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800780 "sbs,battery-detect-gpios", 0, &gpio_flags);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700781
782 if (gpio_flags & OF_GPIO_ACTIVE_LOW)
783 pdata->battery_detect_present = 0;
784 else
785 pdata->battery_detect_present = 1;
786
787of_out:
788 return pdata;
789}
790#else
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800791static struct sbs_platform_data *sbs_of_populate_pdata(
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700792 struct i2c_client *client)
793{
794 return client->dev.platform_data;
795}
796#endif
797
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100798static const struct power_supply_desc sbs_default_desc = {
799 .type = POWER_SUPPLY_TYPE_BATTERY,
800 .properties = sbs_properties,
801 .num_properties = ARRAY_SIZE(sbs_properties),
802 .get_property = sbs_get_property,
803 .external_power_changed = sbs_external_power_changed,
804};
805
Bill Pembertonc8afa642012-11-19 13:22:23 -0500806static int sbs_probe(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700807 const struct i2c_device_id *id)
808{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800809 struct sbs_info *chip;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100810 struct power_supply_desc *sbs_desc;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800811 struct sbs_platform_data *pdata = client->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100812 struct power_supply_config psy_cfg = {};
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700813 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800814 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -0800815
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100816 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
817 sizeof(*sbs_desc), GFP_KERNEL);
818 if (!sbs_desc)
Rhyland Klein52f56c62011-12-05 17:50:49 -0800819 return -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100820
821 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
822 dev_name(&client->dev));
823 if (!sbs_desc->name)
824 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700825
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800826 chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100827 if (!chip)
828 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700829
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800830 chip->client = client;
831 chip->enable_detection = false;
832 chip->gpio_detect = false;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100833 psy_cfg.of_node = client->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100834 psy_cfg.drv_data = chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700835 /* ignore first notification of external change, it is generated
836 * from the power_supply_register call back
837 */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800838 chip->ignore_changes = 1;
839 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700840
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800841 pdata = sbs_of_populate_pdata(client);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700842
Rhyland Kleinbb879102011-02-28 16:55:28 -0800843 if (pdata) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800844 chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
845 chip->pdata = pdata;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800846 }
847
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800848 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700849
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800850 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800851 goto skip_gpio;
852
853 rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
854 if (rc) {
855 dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800856 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800857 goto skip_gpio;
858 }
859
860 rc = gpio_direction_input(pdata->battery_detect);
861 if (rc) {
862 dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
863 gpio_free(pdata->battery_detect);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800864 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800865 goto skip_gpio;
866 }
867
868 irq = gpio_to_irq(pdata->battery_detect);
869 if (irq <= 0) {
870 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
871 gpio_free(pdata->battery_detect);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800872 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800873 goto skip_gpio;
874 }
875
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800876 rc = request_irq(irq, sbs_irq,
Rhyland Kleinbb879102011-02-28 16:55:28 -0800877 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100878 dev_name(&client->dev), chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800879 if (rc) {
880 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
881 gpio_free(pdata->battery_detect);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800882 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800883 goto skip_gpio;
884 }
885
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800886 chip->irq = irq;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800887
888skip_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 */
Frans Klaverf4ed9502015-06-10 14:16:56 +0200893 if (!force_load) {
894 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
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100903 chip->power_supply = power_supply_register(&client->dev, sbs_desc,
904 &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 Klein3ddca0622011-12-05 17:50:46 -0800922 if (chip->irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100923 free_irq(chip->irq, chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800924 if (chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800925 gpio_free(pdata->battery_detect);
926
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800927 kfree(chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800928
929 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700930}
931
Bill Pemberton415ec692012-11-19 13:26:07 -0500932static int sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700933{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800934 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700935
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800936 if (chip->irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100937 free_irq(chip->irq, chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800938 if (chip->gpio_detect)
939 gpio_free(chip->pdata->battery_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800940
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100941 power_supply_unregister(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700942
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800943 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700944
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800945 kfree(chip);
946 chip = NULL;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700947
948 return 0;
949}
950
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100951#if defined CONFIG_PM_SLEEP
952
953static int sbs_suspend(struct device *dev)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700954{
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100955 struct i2c_client *client = to_i2c_client(dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800956 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700957 s32 ret;
958
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800959 if (chip->poll_time > 0)
960 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700961
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700962 /* write to manufacturer access with sleep command */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800963 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700964 MANUFACTURER_ACCESS_SLEEP);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800965 if (chip->is_present && ret < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700966 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700967
968 return 0;
969}
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100970
971static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
972#define SBS_PM_OPS (&sbs_pm_ops)
973
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700974#else
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100975#define SBS_PM_OPS NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700976#endif
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700977
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800978static const struct i2c_device_id sbs_id[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700979 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800980 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700981 {}
982};
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800983MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700984
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800985static struct i2c_driver sbs_battery_driver = {
986 .probe = sbs_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500987 .remove = sbs_remove,
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800988 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700989 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800990 .name = "sbs-battery",
Sachin Kamat075ed032013-03-14 15:49:46 +0530991 .of_match_table = of_match_ptr(sbs_dt_ids),
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100992 .pm = SBS_PM_OPS,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700993 },
994};
Axel Lin5ff92e72012-01-21 14:42:54 +0800995module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700996
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800997MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700998MODULE_LICENSE("GPL");
Frans Klaverf4ed9502015-06-10 14:16:56 +0200999
1000module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
1001MODULE_PARM_DESC(force_load,
1002 "Attempt to load the driver even if no battery is connected");