blob: 8b4c6a8681b035736868202880135d05fe435103 [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 Klein3ddca0622011-12-05 17:50:46 -0800166 struct sbs_platform_data *pdata;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800167 bool is_present;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800168 struct gpio_desc *gpio_detect;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800169 bool enable_detection;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700170 int last_state;
171 int poll_time;
172 struct delayed_work work;
173 int ignore_changes;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700174};
175
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200176static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
177static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
Frans Klaverf4ed9502015-06-10 14:16:56 +0200178static bool force_load;
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200179
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800180static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700181{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800182 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800183 s32 ret = 0;
184 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700185
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800186 if (chip->pdata)
187 retries = max(chip->pdata->i2c_retry_count + 1, 1);
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
214 if (chip->pdata) {
215 retries_length = max(chip->pdata->i2c_retry_count + 1, 1);
216 retries_block = max(chip->pdata->i2c_retry_count + 1, 1);
217 }
218
219 /* Adapter needs to support these two functions */
220 if (!i2c_check_functionality(client->adapter,
221 I2C_FUNC_SMBUS_BYTE_DATA |
222 I2C_FUNC_SMBUS_I2C_BLOCK)){
223 return -ENODEV;
224 }
225
226 /* Get the length of block data */
227 while (retries_length > 0) {
228 ret = i2c_smbus_read_byte_data(client, address);
229 if (ret >= 0)
230 break;
231 retries_length--;
232 }
233
234 if (ret < 0) {
235 dev_dbg(&client->dev,
236 "%s: i2c read at address 0x%x failed\n",
237 __func__, address);
238 return ret;
239 }
240
241 /* block_length does not include NULL terminator */
242 block_length = ret;
243 if (block_length > I2C_SMBUS_BLOCK_MAX) {
244 dev_err(&client->dev,
245 "%s: Returned block_length is longer than 0x%x\n",
246 __func__, I2C_SMBUS_BLOCK_MAX);
247 return -EINVAL;
248 }
249
250 /* Get the block data */
251 while (retries_block > 0) {
252 ret = i2c_smbus_read_i2c_block_data(
253 client, address,
254 block_length + 1, block_buffer);
255 if (ret >= 0)
256 break;
257 retries_block--;
258 }
259
260 if (ret < 0) {
261 dev_dbg(&client->dev,
262 "%s: i2c read at address 0x%x failed\n",
263 __func__, address);
264 return ret;
265 }
266
267 /* block_buffer[0] == block_length */
268 memcpy(values, block_buffer + 1, block_length);
269 values[block_length] = '\0';
270
271 return le16_to_cpu(ret);
272}
273
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800274static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700275 u16 value)
276{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800277 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800278 s32 ret = 0;
279 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700280
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800281 if (chip->pdata)
282 retries = max(chip->pdata->i2c_retry_count + 1, 1);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800283
284 while (retries > 0) {
285 ret = i2c_smbus_write_word_data(client, address,
286 le16_to_cpu(value));
287 if (ret >= 0)
288 break;
289 retries--;
290 }
291
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700292 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800293 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700294 "%s: i2c write to address 0x%x failed\n",
295 __func__, address);
296 return ret;
297 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800298
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700299 return 0;
300}
301
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800302static int sbs_get_battery_presence_and_health(
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700303 struct i2c_client *client, enum power_supply_property psp,
304 union power_supply_propval *val)
305{
306 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800307 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800308
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800309 if (psp == POWER_SUPPLY_PROP_PRESENT && chip->gpio_detect) {
310 ret = gpiod_get_value_cansleep(chip->gpio_detect);
311 if (ret < 0)
312 return ret;
313 val->intval = ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800314 chip->is_present = val->intval;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800315 return ret;
316 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700317
318 /* Write to ManufacturerAccess with
319 * ManufacturerAccess command and then
320 * read the status */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800321 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
322 MANUFACTURER_ACCESS_STATUS);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800323 if (ret < 0) {
324 if (psp == POWER_SUPPLY_PROP_PRESENT)
325 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700326 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800327 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700328
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800329 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800330 if (ret < 0)
331 return ret;
332
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800333 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
334 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700335 val->intval = 0;
336 return 0;
337 }
338
339 /* Mask the upper nibble of 2nd byte and
340 * lower byte of response then
341 * shift the result by 8 to get status*/
342 ret &= 0x0F00;
343 ret >>= 8;
344 if (psp == POWER_SUPPLY_PROP_PRESENT) {
345 if (ret == 0x0F)
346 /* battery removed */
347 val->intval = 0;
348 else
349 val->intval = 1;
350 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
351 if (ret == 0x09)
352 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
353 else if (ret == 0x0B)
354 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
355 else if (ret == 0x0C)
356 val->intval = POWER_SUPPLY_HEALTH_DEAD;
357 else
358 val->intval = POWER_SUPPLY_HEALTH_GOOD;
359 }
360
361 return 0;
362}
363
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800364static int sbs_get_battery_property(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700365 int reg_offset, enum power_supply_property psp,
366 union power_supply_propval *val)
367{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800368 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700369 s32 ret;
370
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800371 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700372 if (ret < 0)
373 return ret;
374
375 /* returned values are 16 bit */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800376 if (sbs_data[reg_offset].min_value < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700377 ret = (s16)ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700378
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800379 if (ret >= sbs_data[reg_offset].min_value &&
380 ret <= sbs_data[reg_offset].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700381 val->intval = ret;
Joshua Clayton957cb722016-08-11 09:59:12 -0700382 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
383 if (!(ret & BATTERY_INITIALIZED))
384 val->intval =
385 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
386 else if (ret & BATTERY_FULL_CHARGED)
387 val->intval =
388 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
389 else if (ret & BATTERY_FULL_DISCHARGED)
390 val->intval =
391 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
392 else
393 val->intval =
394 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700395 return 0;
Joshua Clayton957cb722016-08-11 09:59:12 -0700396 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
397 return 0;
398 }
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700399
400 if (ret & BATTERY_FULL_CHARGED)
401 val->intval = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700402 else if (ret & BATTERY_DISCHARGING)
403 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
404 else
405 val->intval = POWER_SUPPLY_STATUS_CHARGING;
406
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800407 if (chip->poll_time == 0)
408 chip->last_state = val->intval;
409 else if (chip->last_state != val->intval) {
410 cancel_delayed_work_sync(&chip->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100411 power_supply_changed(chip->power_supply);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800412 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700413 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700414 } else {
415 if (psp == POWER_SUPPLY_PROP_STATUS)
416 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
417 else
418 val->intval = 0;
419 }
420
421 return 0;
422}
423
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200424static int sbs_get_battery_string_property(struct i2c_client *client,
425 int reg_offset, enum power_supply_property psp, char *val)
426{
427 s32 ret;
428
429 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
430
431 if (ret < 0)
432 return ret;
433
434 return 0;
435}
436
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800437static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700438 enum power_supply_property psp, union power_supply_propval *val)
439{
440#define BASE_UNIT_CONVERSION 1000
441#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800442#define TIME_UNIT_CONVERSION 60
443#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700444 switch (psp) {
445 case POWER_SUPPLY_PROP_ENERGY_NOW:
446 case POWER_SUPPLY_PROP_ENERGY_FULL:
447 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800448 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800449 * Convert to µWh
450 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700451 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
452 break;
453
454 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Simon Que4495b0a2014-08-04 13:47:46 +0200455 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700456 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
457 case POWER_SUPPLY_PROP_CURRENT_NOW:
Rhyland Klein51d07562011-01-25 11:10:06 -0800458 case POWER_SUPPLY_PROP_CHARGE_NOW:
459 case POWER_SUPPLY_PROP_CHARGE_FULL:
460 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700461 val->intval *= BASE_UNIT_CONVERSION;
462 break;
463
464 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800465 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800466 * so convert it to 0.1°C
467 */
468 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700469 break;
470
471 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
472 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800473 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800474 * Convert to seconds
475 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700476 val->intval *= TIME_UNIT_CONVERSION;
477 break;
478
479 default:
480 dev_dbg(&client->dev,
481 "%s: no need for unit conversion %d\n", __func__, psp);
482 }
483}
484
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800485static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
486 enum sbs_battery_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800487{
488 int ret, original_val;
489
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800490 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800491 if (original_val < 0)
492 return original_val;
493
494 if ((original_val & BATTERY_MODE_MASK) == mode)
495 return mode;
496
497 if (mode == BATTERY_MODE_AMPS)
498 ret = original_val & ~BATTERY_MODE_MASK;
499 else
500 ret = original_val | BATTERY_MODE_MASK;
501
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800502 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800503 if (ret < 0)
504 return ret;
505
506 return original_val & BATTERY_MODE_MASK;
507}
508
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800509static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700510 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700511 union power_supply_propval *val)
512{
513 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800514 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800515
516 if (power_supply_is_amp_property(psp))
517 mode = BATTERY_MODE_AMPS;
518
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800519 mode = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800520 if (mode < 0)
521 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700522
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800523 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700524 if (ret < 0)
525 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700526
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700527 if (psp == POWER_SUPPLY_PROP_CAPACITY) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800528 /* sbs spec says that this can be >100 %
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700529 * even if max value is 100 % */
530 val->intval = min(ret, 100);
531 } else
532 val->intval = ret;
533
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800534 ret = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800535 if (ret < 0)
536 return ret;
537
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700538 return 0;
539}
540
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800541static char sbs_serial[5];
542static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700543 union power_supply_propval *val)
544{
545 int ret;
546
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800547 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700548 if (ret < 0)
549 return ret;
550
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800551 ret = sprintf(sbs_serial, "%04x", ret);
552 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700553
554 return 0;
555}
556
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800557static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800558 enum power_supply_property psp)
559{
560 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800561 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
562 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800563 return count;
564
565 dev_warn(&client->dev,
566 "%s: Invalid Property - %d\n", __func__, psp);
567
568 return -EINVAL;
569}
570
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800571static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700572 enum power_supply_property psp,
573 union power_supply_propval *val)
574{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800575 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100576 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800577 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700578
579 switch (psp) {
580 case POWER_SUPPLY_PROP_PRESENT:
581 case POWER_SUPPLY_PROP_HEALTH:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800582 ret = sbs_get_battery_presence_and_health(client, psp, val);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800583 if (psp == POWER_SUPPLY_PROP_PRESENT)
584 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700585 break;
586
587 case POWER_SUPPLY_PROP_TECHNOLOGY:
588 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
Nikolaus Voss5da50982012-05-09 08:30:44 +0200589 goto done; /* don't trigger power_supply_changed()! */
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700590
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700591 case POWER_SUPPLY_PROP_ENERGY_NOW:
592 case POWER_SUPPLY_PROP_ENERGY_FULL:
593 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800594 case POWER_SUPPLY_PROP_CHARGE_NOW:
595 case POWER_SUPPLY_PROP_CHARGE_FULL:
596 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700597 case POWER_SUPPLY_PROP_CAPACITY:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800598 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800599 if (ret < 0)
600 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700601
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800602 ret = sbs_get_battery_capacity(client, ret, psp, val);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700603 break;
604
605 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800606 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700607 break;
608
609 case POWER_SUPPLY_PROP_STATUS:
Joshua Clayton957cb722016-08-11 09:59:12 -0700610 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700611 case POWER_SUPPLY_PROP_CYCLE_COUNT:
612 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
613 case POWER_SUPPLY_PROP_CURRENT_NOW:
614 case POWER_SUPPLY_PROP_TEMP:
615 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
616 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Simon Que4495b0a2014-08-04 13:47:46 +0200617 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700618 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800619 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800620 if (ret < 0)
621 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700622
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800623 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700624 break;
625
Cheng-Yi Chiang9ea89402014-08-04 13:47:45 +0200626 case POWER_SUPPLY_PROP_MODEL_NAME:
627 ret = sbs_get_property_index(client, psp);
628 if (ret < 0)
629 break;
630
631 ret = sbs_get_battery_string_property(client, ret, psp,
632 model_name);
633 val->strval = model_name;
634 break;
635
636 case POWER_SUPPLY_PROP_MANUFACTURER:
637 ret = sbs_get_property_index(client, psp);
638 if (ret < 0)
639 break;
640
641 ret = sbs_get_battery_string_property(client, ret, psp,
642 manufacturer);
643 val->strval = manufacturer;
644 break;
645
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700646 default:
647 dev_err(&client->dev,
648 "%s: INVALID property\n", __func__);
649 return -EINVAL;
650 }
651
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800652 if (!chip->enable_detection)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800653 goto done;
654
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800655 if (!chip->gpio_detect &&
656 chip->is_present != (ret >= 0)) {
657 chip->is_present = (ret >= 0);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100658 power_supply_changed(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800659 }
660
661done:
662 if (!ret) {
663 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800664 sbs_unit_adjustment(client, psp, val);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800665 }
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700666
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700667 dev_dbg(&client->dev,
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800668 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700669
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800670 if (ret && chip->is_present)
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800671 return ret;
672
673 /* battery not present, so return NODATA for properties */
674 if (ret)
675 return -ENODATA;
676
677 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700678}
679
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800680static irqreturn_t sbs_irq(int irq, void *devid)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800681{
Phil Reidd2cec822016-07-25 10:42:58 +0800682 struct sbs_info *chip = devid;
683 struct power_supply *battery = chip->power_supply;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800684 int ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800685
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800686 ret = gpiod_get_value_cansleep(chip->gpio_detect);
687 if (ret < 0)
688 return ret;
689 chip->is_present = ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800690 power_supply_changed(battery);
691
692 return IRQ_HANDLED;
693}
694
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800695static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700696{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100697 struct sbs_info *chip = power_supply_get_drvdata(psy);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700698
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800699 if (chip->ignore_changes > 0) {
700 chip->ignore_changes--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700701 return;
702 }
703
704 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800705 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700706
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800707 schedule_delayed_work(&chip->work, HZ);
708 chip->poll_time = chip->pdata->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700709}
710
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800711static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700712{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800713 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700714 s32 ret;
715
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800716 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700717
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800718 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700719 /* if the read failed, give up on this work */
720 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800721 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700722 return;
723 }
724
725 if (ret & BATTERY_FULL_CHARGED)
726 ret = POWER_SUPPLY_STATUS_FULL;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700727 else if (ret & BATTERY_DISCHARGING)
728 ret = POWER_SUPPLY_STATUS_DISCHARGING;
729 else
730 ret = POWER_SUPPLY_STATUS_CHARGING;
731
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800732 if (chip->last_state != ret) {
733 chip->poll_time = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100734 power_supply_changed(chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700735 return;
736 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800737 if (chip->poll_time > 0) {
738 schedule_delayed_work(&chip->work, HZ);
739 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700740 return;
741 }
742}
743
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700744#if defined(CONFIG_OF)
745
746#include <linux/of_device.h>
747#include <linux/of_gpio.h>
748
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800749static const struct of_device_id sbs_dt_ids[] = {
750 { .compatible = "sbs,sbs-battery" },
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700751 { .compatible = "ti,bq20z75" },
752 { }
753};
Olof Johansson62df3932012-01-06 05:45:34 +0400754MODULE_DEVICE_TABLE(of, sbs_dt_ids);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700755
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800756static struct sbs_platform_data *sbs_of_populate_pdata(struct sbs_info *chip)
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700757{
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800758 struct i2c_client *client = chip->client;
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700759 struct device_node *of_node = client->dev.of_node;
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800760 struct sbs_platform_data *pdata;
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700761 int rc;
762 u32 prop;
763
764 /* verify this driver matches this device */
765 if (!of_node)
766 return NULL;
767
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700768 /* first make sure at least one property is set, otherwise
769 * it won't change behavior from running without pdata.
770 */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800771 if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800772 !of_get_property(of_node, "sbs,poll-retry-count", NULL))
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700773 goto of_out;
774
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800775 pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700776 GFP_KERNEL);
777 if (!pdata)
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800778 return ERR_PTR(-ENOMEM);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700779
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800780 rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700781 if (!rc)
782 pdata->i2c_retry_count = prop;
783
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800784 rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700785 if (!rc)
786 pdata->poll_retry_count = prop;
787
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700788of_out:
789 return pdata;
790}
791#else
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800792static struct sbs_platform_data *sbs_of_populate_pdata(
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800793 struct sbs_info *client)
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700794{
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800795 return ERR_PTR(-ENOSYS);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700796}
797#endif
798
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100799static const struct power_supply_desc sbs_default_desc = {
800 .type = POWER_SUPPLY_TYPE_BATTERY,
801 .properties = sbs_properties,
802 .num_properties = ARRAY_SIZE(sbs_properties),
803 .get_property = sbs_get_property,
804 .external_power_changed = sbs_external_power_changed,
805};
806
Bill Pembertonc8afa642012-11-19 13:22:23 -0500807static int sbs_probe(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700808 const struct i2c_device_id *id)
809{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800810 struct sbs_info *chip;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100811 struct power_supply_desc *sbs_desc;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800812 struct sbs_platform_data *pdata = client->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100813 struct power_supply_config psy_cfg = {};
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700814 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800815 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -0800816
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100817 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
818 sizeof(*sbs_desc), GFP_KERNEL);
819 if (!sbs_desc)
Rhyland Klein52f56c62011-12-05 17:50:49 -0800820 return -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100821
822 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
823 dev_name(&client->dev));
824 if (!sbs_desc->name)
825 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700826
Phil Reid9239a862016-07-25 10:42:57 +0800827 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100828 if (!chip)
829 return -ENOMEM;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700830
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800831 chip->client = client;
832 chip->enable_detection = 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
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800841 if (!pdata)
842 pdata = sbs_of_populate_pdata(chip);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700843
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800844 if (IS_ERR(pdata))
845 return PTR_ERR(pdata);
846
847 chip->pdata = pdata;
848
849 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
850 "sbs,battery-detect", GPIOD_IN);
851 if (IS_ERR(chip->gpio_detect)) {
852 dev_err(&client->dev, "Failed to get gpio: %ld\n",
853 PTR_ERR(chip->gpio_detect));
854 return PTR_ERR(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800855 }
856
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800857 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700858
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800859 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800860 goto skip_gpio;
861
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800862 irq = gpiod_to_irq(chip->gpio_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800863 if (irq <= 0) {
864 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800865 goto skip_gpio;
866 }
867
Phil Reidd2cec822016-07-25 10:42:58 +0800868 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
Rhyland Kleinbb879102011-02-28 16:55:28 -0800869 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Phil Reidd2cec822016-07-25 10:42:58 +0800870 dev_name(&client->dev), chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800871 if (rc) {
872 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800873 goto skip_gpio;
874 }
875
Rhyland Kleinbb879102011-02-28 16:55:28 -0800876skip_gpio:
Olof Johanssona22b41a2012-09-06 11:32:29 -0700877 /*
Frans Klaverf4ed9502015-06-10 14:16:56 +0200878 * Before we register, we might need to make sure we can actually talk
Olof Johanssona22b41a2012-09-06 11:32:29 -0700879 * to the battery.
880 */
Phil Reid3b5dd3a2016-09-01 15:50:52 +0800881 if (!(force_load || chip->gpio_detect)) {
Frans Klaverf4ed9502015-06-10 14:16:56 +0200882 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
883
884 if (rc < 0) {
885 dev_err(&client->dev, "%s: Failed to get device status\n",
886 __func__);
887 goto exit_psupply;
888 }
Olof Johanssona22b41a2012-09-06 11:32:29 -0700889 }
Rhyland Kleinbb879102011-02-28 16:55:28 -0800890
Phil Reid492ff9d82016-07-25 10:42:59 +0800891 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100892 &psy_cfg);
893 if (IS_ERR(chip->power_supply)) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700894 dev_err(&client->dev,
895 "%s: Failed to register power supply\n", __func__);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100896 rc = PTR_ERR(chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800897 goto exit_psupply;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700898 }
899
900 dev_info(&client->dev,
901 "%s: battery gas gauge device registered\n", client->name);
902
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800903 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700904
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800905 chip->enable_detection = true;
Rhyland Kleinee177d92011-05-24 12:06:50 -0700906
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700907 return 0;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800908
909exit_psupply:
Rhyland Kleinbb879102011-02-28 16:55:28 -0800910 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700911}
912
Bill Pemberton415ec692012-11-19 13:26:07 -0500913static int sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700914{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800915 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700916
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800917 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700918
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700919 return 0;
920}
921
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100922#if defined CONFIG_PM_SLEEP
923
924static int sbs_suspend(struct device *dev)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700925{
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100926 struct i2c_client *client = to_i2c_client(dev);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800927 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700928 s32 ret;
929
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800930 if (chip->poll_time > 0)
931 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700932
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700933 /* write to manufacturer access with sleep command */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800934 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700935 MANUFACTURER_ACCESS_SLEEP);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800936 if (chip->is_present && ret < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700937 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700938
939 return 0;
940}
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100941
942static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
943#define SBS_PM_OPS (&sbs_pm_ops)
944
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700945#else
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100946#define SBS_PM_OPS NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700947#endif
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700948
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800949static const struct i2c_device_id sbs_id[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700950 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800951 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700952 {}
953};
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800954MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700955
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800956static struct i2c_driver sbs_battery_driver = {
957 .probe = sbs_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500958 .remove = sbs_remove,
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800959 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700960 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800961 .name = "sbs-battery",
Sachin Kamat075ed032013-03-14 15:49:46 +0530962 .of_match_table = of_match_ptr(sbs_dt_ids),
Lars-Peter Clausen9c1d1af2013-03-10 14:34:07 +0100963 .pm = SBS_PM_OPS,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700964 },
965};
Axel Lin5ff92e72012-01-21 14:42:54 +0800966module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700967
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800968MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700969MODULE_LICENSE("GPL");
Frans Klaverf4ed9502015-06-10 14:16:56 +0200970
971module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
972MODULE_PARM_DESC(force_load,
973 "Attempt to load the driver even if no battery is connected");