blob: a5b6849d4123b51b60d2431bbcd72cc9f13c86f4 [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>
30
Rhyland Klein3ddca0622011-12-05 17:50:46 -080031#include <linux/power/sbs-battery.h>
Rhyland Kleina7640bf2010-09-05 15:31:23 -070032
33enum {
34 REG_MANUFACTURER_DATA,
35 REG_TEMPERATURE,
36 REG_VOLTAGE,
37 REG_CURRENT,
38 REG_CAPACITY,
39 REG_TIME_TO_EMPTY,
40 REG_TIME_TO_FULL,
41 REG_STATUS,
42 REG_CYCLE_COUNT,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070043 REG_SERIAL_NUMBER,
44 REG_REMAINING_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080045 REG_REMAINING_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070046 REG_FULL_CHARGE_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080047 REG_FULL_CHARGE_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070048 REG_DESIGN_CAPACITY,
Rhyland Klein51d07562011-01-25 11:10:06 -080049 REG_DESIGN_CAPACITY_CHARGE,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070050 REG_DESIGN_VOLTAGE,
Rhyland Kleina7640bf2010-09-05 15:31:23 -070051};
52
Rhyland Klein51d07562011-01-25 11:10:06 -080053/* Battery Mode defines */
54#define BATTERY_MODE_OFFSET 0x03
55#define BATTERY_MODE_MASK 0x8000
Rhyland Klein3ddca0622011-12-05 17:50:46 -080056enum sbs_battery_mode {
Rhyland Klein51d07562011-01-25 11:10:06 -080057 BATTERY_MODE_AMPS,
58 BATTERY_MODE_WATTS
59};
60
Rhyland Kleina7640bf2010-09-05 15:31:23 -070061/* manufacturer access defines */
62#define MANUFACTURER_ACCESS_STATUS 0x0006
63#define MANUFACTURER_ACCESS_SLEEP 0x0011
64
65/* battery status value bits */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070066#define BATTERY_DISCHARGING 0x40
Rhyland Kleina7640bf2010-09-05 15:31:23 -070067#define BATTERY_FULL_CHARGED 0x20
68#define BATTERY_FULL_DISCHARGED 0x10
69
Rhyland Klein3ddca0622011-12-05 17:50:46 -080070#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
Rhyland Kleina7640bf2010-09-05 15:31:23 -070071 .psp = _psp, \
72 .addr = _addr, \
73 .min_value = _min_value, \
74 .max_value = _max_value, \
75}
76
Rhyland Klein3ddca0622011-12-05 17:50:46 -080077static const struct chip_data {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070078 enum power_supply_property psp;
79 u8 addr;
80 int min_value;
81 int max_value;
Rhyland Klein3ddca0622011-12-05 17:50:46 -080082} sbs_data[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -070083 [REG_MANUFACTURER_DATA] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080084 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070085 [REG_TEMPERATURE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080086 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070087 [REG_VOLTAGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080088 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070089 [REG_CURRENT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080090 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
Rhyland Kleina7640bf2010-09-05 15:31:23 -070091 [REG_CAPACITY] =
Nikolaus Vossb1f092f2012-04-25 08:59:03 +020092 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070093 [REG_REMAINING_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080094 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -080095 [REG_REMAINING_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080096 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -070097 [REG_FULL_CHARGE_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -080098 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -080099 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800100 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700101 [REG_TIME_TO_EMPTY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800102 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700103 [REG_TIME_TO_FULL] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800104 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700105 [REG_STATUS] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800106 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700107 [REG_CYCLE_COUNT] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800108 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700109 [REG_DESIGN_CAPACITY] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800110 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
Rhyland Klein51d07562011-01-25 11:10:06 -0800111 [REG_DESIGN_CAPACITY_CHARGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800112 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700113 [REG_DESIGN_VOLTAGE] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800114 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700115 [REG_SERIAL_NUMBER] =
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800116 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700117};
118
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800119static enum power_supply_property sbs_properties[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700120 POWER_SUPPLY_PROP_STATUS,
121 POWER_SUPPLY_PROP_HEALTH,
122 POWER_SUPPLY_PROP_PRESENT,
123 POWER_SUPPLY_PROP_TECHNOLOGY,
124 POWER_SUPPLY_PROP_CYCLE_COUNT,
125 POWER_SUPPLY_PROP_VOLTAGE_NOW,
126 POWER_SUPPLY_PROP_CURRENT_NOW,
127 POWER_SUPPLY_PROP_CAPACITY,
128 POWER_SUPPLY_PROP_TEMP,
129 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
130 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
131 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700132 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
133 POWER_SUPPLY_PROP_ENERGY_NOW,
134 POWER_SUPPLY_PROP_ENERGY_FULL,
135 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
Rhyland Klein51d07562011-01-25 11:10:06 -0800136 POWER_SUPPLY_PROP_CHARGE_NOW,
137 POWER_SUPPLY_PROP_CHARGE_FULL,
138 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700139};
140
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800141struct sbs_info {
Rhyland Kleinbb879102011-02-28 16:55:28 -0800142 struct i2c_client *client;
143 struct power_supply power_supply;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800144 struct sbs_platform_data *pdata;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800145 bool is_present;
146 bool gpio_detect;
147 bool enable_detection;
148 int irq;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700149 int last_state;
150 int poll_time;
151 struct delayed_work work;
152 int ignore_changes;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700153};
154
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800155static int sbs_read_word_data(struct i2c_client *client, u8 address)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700156{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800157 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800158 s32 ret = 0;
159 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700160
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800161 if (chip->pdata)
162 retries = max(chip->pdata->i2c_retry_count + 1, 1);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800163
164 while (retries > 0) {
165 ret = i2c_smbus_read_word_data(client, address);
166 if (ret >= 0)
167 break;
168 retries--;
169 }
170
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700171 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800172 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700173 "%s: i2c read at address 0x%x failed\n",
174 __func__, address);
175 return ret;
176 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800177
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700178 return le16_to_cpu(ret);
179}
180
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800181static int sbs_write_word_data(struct i2c_client *client, u8 address,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700182 u16 value)
183{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800184 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800185 s32 ret = 0;
186 int retries = 1;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700187
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800188 if (chip->pdata)
189 retries = max(chip->pdata->i2c_retry_count + 1, 1);
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800190
191 while (retries > 0) {
192 ret = i2c_smbus_write_word_data(client, address,
193 le16_to_cpu(value));
194 if (ret >= 0)
195 break;
196 retries--;
197 }
198
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700199 if (ret < 0) {
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800200 dev_dbg(&client->dev,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700201 "%s: i2c write to address 0x%x failed\n",
202 __func__, address);
203 return ret;
204 }
Rhyland Kleinff28fce2011-02-28 16:55:29 -0800205
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700206 return 0;
207}
208
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800209static int sbs_get_battery_presence_and_health(
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700210 struct i2c_client *client, enum power_supply_property psp,
211 union power_supply_propval *val)
212{
213 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800214 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800215
216 if (psp == POWER_SUPPLY_PROP_PRESENT &&
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800217 chip->gpio_detect) {
218 ret = gpio_get_value(chip->pdata->battery_detect);
219 if (ret == chip->pdata->battery_detect_present)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800220 val->intval = 1;
221 else
222 val->intval = 0;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800223 chip->is_present = val->intval;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800224 return ret;
225 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700226
227 /* Write to ManufacturerAccess with
228 * ManufacturerAccess command and then
229 * read the status */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800230 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
231 MANUFACTURER_ACCESS_STATUS);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800232 if (ret < 0) {
233 if (psp == POWER_SUPPLY_PROP_PRESENT)
234 val->intval = 0; /* battery removed */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700235 return ret;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800236 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700237
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800238 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800239 if (ret < 0)
240 return ret;
241
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800242 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
243 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700244 val->intval = 0;
245 return 0;
246 }
247
248 /* Mask the upper nibble of 2nd byte and
249 * lower byte of response then
250 * shift the result by 8 to get status*/
251 ret &= 0x0F00;
252 ret >>= 8;
253 if (psp == POWER_SUPPLY_PROP_PRESENT) {
254 if (ret == 0x0F)
255 /* battery removed */
256 val->intval = 0;
257 else
258 val->intval = 1;
259 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
260 if (ret == 0x09)
261 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
262 else if (ret == 0x0B)
263 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
264 else if (ret == 0x0C)
265 val->intval = POWER_SUPPLY_HEALTH_DEAD;
266 else
267 val->intval = POWER_SUPPLY_HEALTH_GOOD;
268 }
269
270 return 0;
271}
272
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800273static int sbs_get_battery_property(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700274 int reg_offset, enum power_supply_property psp,
275 union power_supply_propval *val)
276{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800277 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700278 s32 ret;
279
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800280 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700281 if (ret < 0)
282 return ret;
283
284 /* returned values are 16 bit */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800285 if (sbs_data[reg_offset].min_value < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700286 ret = (s16)ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700287
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800288 if (ret >= sbs_data[reg_offset].min_value &&
289 ret <= sbs_data[reg_offset].max_value) {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700290 val->intval = ret;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700291 if (psp != POWER_SUPPLY_PROP_STATUS)
292 return 0;
293
294 if (ret & BATTERY_FULL_CHARGED)
295 val->intval = POWER_SUPPLY_STATUS_FULL;
296 else if (ret & BATTERY_FULL_DISCHARGED)
297 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
298 else if (ret & BATTERY_DISCHARGING)
299 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
300 else
301 val->intval = POWER_SUPPLY_STATUS_CHARGING;
302
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800303 if (chip->poll_time == 0)
304 chip->last_state = val->intval;
305 else if (chip->last_state != val->intval) {
306 cancel_delayed_work_sync(&chip->work);
307 power_supply_changed(&chip->power_supply);
308 chip->poll_time = 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700309 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700310 } else {
311 if (psp == POWER_SUPPLY_PROP_STATUS)
312 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
313 else
314 val->intval = 0;
315 }
316
317 return 0;
318}
319
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800320static void sbs_unit_adjustment(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700321 enum power_supply_property psp, union power_supply_propval *val)
322{
323#define BASE_UNIT_CONVERSION 1000
324#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
Benson Leung909a78b2011-02-27 17:41:48 -0800325#define TIME_UNIT_CONVERSION 60
326#define TEMP_KELVIN_TO_CELSIUS 2731
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700327 switch (psp) {
328 case POWER_SUPPLY_PROP_ENERGY_NOW:
329 case POWER_SUPPLY_PROP_ENERGY_FULL:
330 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800331 /* sbs provides energy in units of 10mWh.
Benson Leung909a78b2011-02-27 17:41:48 -0800332 * Convert to µWh
333 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700334 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
335 break;
336
337 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
338 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
339 case POWER_SUPPLY_PROP_CURRENT_NOW:
Rhyland Klein51d07562011-01-25 11:10:06 -0800340 case POWER_SUPPLY_PROP_CHARGE_NOW:
341 case POWER_SUPPLY_PROP_CHARGE_FULL:
342 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700343 val->intval *= BASE_UNIT_CONVERSION;
344 break;
345
346 case POWER_SUPPLY_PROP_TEMP:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800347 /* sbs provides battery temperature in 0.1K
Benson Leung909a78b2011-02-27 17:41:48 -0800348 * so convert it to 0.1°C
349 */
350 val->intval -= TEMP_KELVIN_TO_CELSIUS;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700351 break;
352
353 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
354 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800355 /* sbs provides time to empty and time to full in minutes.
Benson Leung909a78b2011-02-27 17:41:48 -0800356 * Convert to seconds
357 */
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700358 val->intval *= TIME_UNIT_CONVERSION;
359 break;
360
361 default:
362 dev_dbg(&client->dev,
363 "%s: no need for unit conversion %d\n", __func__, psp);
364 }
365}
366
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800367static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
368 enum sbs_battery_mode mode)
Rhyland Klein51d07562011-01-25 11:10:06 -0800369{
370 int ret, original_val;
371
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800372 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
Rhyland Klein51d07562011-01-25 11:10:06 -0800373 if (original_val < 0)
374 return original_val;
375
376 if ((original_val & BATTERY_MODE_MASK) == mode)
377 return mode;
378
379 if (mode == BATTERY_MODE_AMPS)
380 ret = original_val & ~BATTERY_MODE_MASK;
381 else
382 ret = original_val | BATTERY_MODE_MASK;
383
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800384 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
Rhyland Klein51d07562011-01-25 11:10:06 -0800385 if (ret < 0)
386 return ret;
387
388 return original_val & BATTERY_MODE_MASK;
389}
390
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800391static int sbs_get_battery_capacity(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700392 int reg_offset, enum power_supply_property psp,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700393 union power_supply_propval *val)
394{
395 s32 ret;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800396 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
Rhyland Klein51d07562011-01-25 11:10:06 -0800397
398 if (power_supply_is_amp_property(psp))
399 mode = BATTERY_MODE_AMPS;
400
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800401 mode = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800402 if (mode < 0)
403 return mode;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700404
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800405 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700406 if (ret < 0)
407 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700408
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700409 if (psp == POWER_SUPPLY_PROP_CAPACITY) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800410 /* sbs spec says that this can be >100 %
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700411 * even if max value is 100 % */
412 val->intval = min(ret, 100);
413 } else
414 val->intval = ret;
415
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800416 ret = sbs_set_battery_mode(client, mode);
Rhyland Klein51d07562011-01-25 11:10:06 -0800417 if (ret < 0)
418 return ret;
419
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700420 return 0;
421}
422
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800423static char sbs_serial[5];
424static int sbs_get_battery_serial_number(struct i2c_client *client,
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700425 union power_supply_propval *val)
426{
427 int ret;
428
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800429 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700430 if (ret < 0)
431 return ret;
432
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800433 ret = sprintf(sbs_serial, "%04x", ret);
434 val->strval = sbs_serial;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700435
436 return 0;
437}
438
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800439static int sbs_get_property_index(struct i2c_client *client,
Rhyland Klein51d07562011-01-25 11:10:06 -0800440 enum power_supply_property psp)
441{
442 int count;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800443 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
444 if (psp == sbs_data[count].psp)
Rhyland Klein51d07562011-01-25 11:10:06 -0800445 return count;
446
447 dev_warn(&client->dev,
448 "%s: Invalid Property - %d\n", __func__, psp);
449
450 return -EINVAL;
451}
452
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800453static int sbs_get_property(struct power_supply *psy,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700454 enum power_supply_property psp,
455 union power_supply_propval *val)
456{
Rhyland Kleinbb879102011-02-28 16:55:28 -0800457 int ret = 0;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800458 struct sbs_info *chip = container_of(psy,
459 struct sbs_info, power_supply);
460 struct i2c_client *client = chip->client;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700461
462 switch (psp) {
463 case POWER_SUPPLY_PROP_PRESENT:
464 case POWER_SUPPLY_PROP_HEALTH:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800465 ret = sbs_get_battery_presence_and_health(client, psp, val);
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800466 if (psp == POWER_SUPPLY_PROP_PRESENT)
467 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700468 break;
469
470 case POWER_SUPPLY_PROP_TECHNOLOGY:
471 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
472 break;
473
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700474 case POWER_SUPPLY_PROP_ENERGY_NOW:
475 case POWER_SUPPLY_PROP_ENERGY_FULL:
476 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rhyland Klein51d07562011-01-25 11:10:06 -0800477 case POWER_SUPPLY_PROP_CHARGE_NOW:
478 case POWER_SUPPLY_PROP_CHARGE_FULL:
479 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700480 case POWER_SUPPLY_PROP_CAPACITY:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800481 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800482 if (ret < 0)
483 break;
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700484
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800485 ret = sbs_get_battery_capacity(client, ret, psp, val);
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700486 break;
487
488 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800489 ret = sbs_get_battery_serial_number(client, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700490 break;
491
492 case POWER_SUPPLY_PROP_STATUS:
493 case POWER_SUPPLY_PROP_CYCLE_COUNT:
494 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
495 case POWER_SUPPLY_PROP_CURRENT_NOW:
496 case POWER_SUPPLY_PROP_TEMP:
497 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
498 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700499 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800500 ret = sbs_get_property_index(client, psp);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800501 if (ret < 0)
502 break;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700503
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800504 ret = sbs_get_battery_property(client, ret, psp, val);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700505 break;
506
507 default:
508 dev_err(&client->dev,
509 "%s: INVALID property\n", __func__);
510 return -EINVAL;
511 }
512
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800513 if (!chip->enable_detection)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800514 goto done;
515
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800516 if (!chip->gpio_detect &&
517 chip->is_present != (ret >= 0)) {
518 chip->is_present = (ret >= 0);
519 power_supply_changed(&chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800520 }
521
522done:
523 if (!ret) {
524 /* Convert units to match requirements for power supply class */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800525 sbs_unit_adjustment(client, psp, val);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800526 }
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700527
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700528 dev_dbg(&client->dev,
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800529 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700530
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800531 if (ret && chip->is_present)
Rhyland Kleina7d9ace2011-03-09 16:18:02 -0800532 return ret;
533
534 /* battery not present, so return NODATA for properties */
535 if (ret)
536 return -ENODATA;
537
538 return 0;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700539}
540
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800541static irqreturn_t sbs_irq(int irq, void *devid)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800542{
543 struct power_supply *battery = devid;
544
545 power_supply_changed(battery);
546
547 return IRQ_HANDLED;
548}
549
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800550static void sbs_external_power_changed(struct power_supply *psy)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700551{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800552 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700553
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800554 chip = container_of(psy, struct sbs_info, power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700555
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800556 if (chip->ignore_changes > 0) {
557 chip->ignore_changes--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700558 return;
559 }
560
561 /* cancel outstanding work */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800562 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700563
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800564 schedule_delayed_work(&chip->work, HZ);
565 chip->poll_time = chip->pdata->poll_retry_count;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700566}
567
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800568static void sbs_delayed_work(struct work_struct *work)
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700569{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800570 struct sbs_info *chip;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700571 s32 ret;
572
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800573 chip = container_of(work, struct sbs_info, work.work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700574
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800575 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700576 /* if the read failed, give up on this work */
577 if (ret < 0) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800578 chip->poll_time = 0;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700579 return;
580 }
581
582 if (ret & BATTERY_FULL_CHARGED)
583 ret = POWER_SUPPLY_STATUS_FULL;
584 else if (ret & BATTERY_FULL_DISCHARGED)
585 ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
586 else if (ret & BATTERY_DISCHARGING)
587 ret = POWER_SUPPLY_STATUS_DISCHARGING;
588 else
589 ret = POWER_SUPPLY_STATUS_CHARGING;
590
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800591 if (chip->last_state != ret) {
592 chip->poll_time = 0;
593 power_supply_changed(&chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700594 return;
595 }
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800596 if (chip->poll_time > 0) {
597 schedule_delayed_work(&chip->work, HZ);
598 chip->poll_time--;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700599 return;
600 }
601}
602
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700603#if defined(CONFIG_OF)
604
605#include <linux/of_device.h>
606#include <linux/of_gpio.h>
607
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800608static const struct of_device_id sbs_dt_ids[] = {
609 { .compatible = "sbs,sbs-battery" },
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700610 { .compatible = "ti,bq20z75" },
611 { }
612};
Olof Johansson62df3932012-01-06 05:45:34 +0400613MODULE_DEVICE_TABLE(of, sbs_dt_ids);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700614
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800615static struct sbs_platform_data *sbs_of_populate_pdata(
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700616 struct i2c_client *client)
617{
618 struct device_node *of_node = client->dev.of_node;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800619 struct sbs_platform_data *pdata = client->dev.platform_data;
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700620 enum of_gpio_flags gpio_flags;
621 int rc;
622 u32 prop;
623
624 /* verify this driver matches this device */
625 if (!of_node)
626 return NULL;
627
628 /* if platform data is set, honor it */
629 if (pdata)
630 return pdata;
631
632 /* first make sure at least one property is set, otherwise
633 * it won't change behavior from running without pdata.
634 */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800635 if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
636 !of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
637 !of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700638 goto of_out;
639
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800640 pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700641 GFP_KERNEL);
642 if (!pdata)
643 goto of_out;
644
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800645 rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700646 if (!rc)
647 pdata->i2c_retry_count = prop;
648
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800649 rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700650 if (!rc)
651 pdata->poll_retry_count = prop;
652
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800653 if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700654 pdata->battery_detect = -1;
655 goto of_out;
656 }
657
658 pdata->battery_detect = of_get_named_gpio_flags(of_node,
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800659 "sbs,battery-detect-gpios", 0, &gpio_flags);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700660
661 if (gpio_flags & OF_GPIO_ACTIVE_LOW)
662 pdata->battery_detect_present = 0;
663 else
664 pdata->battery_detect_present = 1;
665
666of_out:
667 return pdata;
668}
669#else
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800670#define sbs_dt_ids NULL
671static struct sbs_platform_data *sbs_of_populate_pdata(
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700672 struct i2c_client *client)
673{
674 return client->dev.platform_data;
675}
676#endif
677
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800678static int __devinit sbs_probe(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700679 const struct i2c_device_id *id)
680{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800681 struct sbs_info *chip;
682 struct sbs_platform_data *pdata = client->dev.platform_data;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700683 int rc;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800684 int irq;
Rhyland Klein52f56c62011-12-05 17:50:49 -0800685 char *name;
686
687 name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev));
688 if (!name) {
689 dev_err(&client->dev, "Failed to allocate device name\n");
690 return -ENOMEM;
691 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700692
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800693 chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
Rhyland Klein52f56c62011-12-05 17:50:49 -0800694 if (!chip) {
695 rc = -ENOMEM;
696 goto exit_free_name;
697 }
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700698
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800699 chip->client = client;
700 chip->enable_detection = false;
701 chip->gpio_detect = false;
Rhyland Klein52f56c62011-12-05 17:50:49 -0800702 chip->power_supply.name = name;
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800703 chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
704 chip->power_supply.properties = sbs_properties;
705 chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties);
706 chip->power_supply.get_property = sbs_get_property;
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700707 /* ignore first notification of external change, it is generated
708 * from the power_supply_register call back
709 */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800710 chip->ignore_changes = 1;
711 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
712 chip->power_supply.external_power_changed = sbs_external_power_changed;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700713
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800714 pdata = sbs_of_populate_pdata(client);
Rhyland Klein6c75ea12011-09-14 13:19:07 -0700715
Rhyland Kleinbb879102011-02-28 16:55:28 -0800716 if (pdata) {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800717 chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
718 chip->pdata = pdata;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800719 }
720
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800721 i2c_set_clientdata(client, chip);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700722
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800723 if (!chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800724 goto skip_gpio;
725
726 rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
727 if (rc) {
728 dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800729 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800730 goto skip_gpio;
731 }
732
733 rc = gpio_direction_input(pdata->battery_detect);
734 if (rc) {
735 dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
736 gpio_free(pdata->battery_detect);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800737 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800738 goto skip_gpio;
739 }
740
741 irq = gpio_to_irq(pdata->battery_detect);
742 if (irq <= 0) {
743 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
744 gpio_free(pdata->battery_detect);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800745 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800746 goto skip_gpio;
747 }
748
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800749 rc = request_irq(irq, sbs_irq,
Rhyland Kleinbb879102011-02-28 16:55:28 -0800750 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800751 dev_name(&client->dev), &chip->power_supply);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800752 if (rc) {
753 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
754 gpio_free(pdata->battery_detect);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800755 chip->gpio_detect = false;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800756 goto skip_gpio;
757 }
758
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800759 chip->irq = irq;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800760
761skip_gpio:
762
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800763 rc = power_supply_register(&client->dev, &chip->power_supply);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700764 if (rc) {
765 dev_err(&client->dev,
766 "%s: Failed to register power supply\n", __func__);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800767 goto exit_psupply;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700768 }
769
770 dev_info(&client->dev,
771 "%s: battery gas gauge device registered\n", client->name);
772
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800773 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700774
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800775 chip->enable_detection = true;
Rhyland Kleinee177d92011-05-24 12:06:50 -0700776
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700777 return 0;
Rhyland Kleinbb879102011-02-28 16:55:28 -0800778
779exit_psupply:
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800780 if (chip->irq)
781 free_irq(chip->irq, &chip->power_supply);
782 if (chip->gpio_detect)
Rhyland Kleinbb879102011-02-28 16:55:28 -0800783 gpio_free(pdata->battery_detect);
784
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800785 kfree(chip);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800786
Rhyland Klein52f56c62011-12-05 17:50:49 -0800787exit_free_name:
788 kfree(name);
789
Rhyland Kleinbb879102011-02-28 16:55:28 -0800790 return rc;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700791}
792
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800793static int __devexit sbs_remove(struct i2c_client *client)
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700794{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800795 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700796
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800797 if (chip->irq)
798 free_irq(chip->irq, &chip->power_supply);
799 if (chip->gpio_detect)
800 gpio_free(chip->pdata->battery_detect);
Rhyland Kleinbb879102011-02-28 16:55:28 -0800801
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800802 power_supply_unregister(&chip->power_supply);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700803
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800804 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700805
Rhyland Klein52f56c62011-12-05 17:50:49 -0800806 kfree(chip->power_supply.name);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800807 kfree(chip);
808 chip = NULL;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700809
810 return 0;
811}
812
813#if defined CONFIG_PM
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800814static int sbs_suspend(struct i2c_client *client,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700815 pm_message_t state)
816{
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800817 struct sbs_info *chip = i2c_get_clientdata(client);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700818 s32 ret;
819
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800820 if (chip->poll_time > 0)
821 cancel_delayed_work_sync(&chip->work);
Rhyland Klein58ddafa2011-05-24 12:05:59 -0700822
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700823 /* write to manufacturer access with sleep command */
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800824 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700825 MANUFACTURER_ACCESS_SLEEP);
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800826 if (chip->is_present && ret < 0)
Rhyland Kleind3ab61e2010-09-21 15:33:55 -0700827 return ret;
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700828
829 return 0;
830}
831#else
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800832#define sbs_suspend NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700833#endif
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800834/* any smbus transaction will wake up sbs */
835#define sbs_resume NULL
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700836
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800837static const struct i2c_device_id sbs_id[] = {
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700838 { "bq20z75", 0 },
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800839 { "sbs-battery", 1 },
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700840 {}
841};
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800842MODULE_DEVICE_TABLE(i2c, sbs_id);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700843
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800844static struct i2c_driver sbs_battery_driver = {
845 .probe = sbs_probe,
846 .remove = __devexit_p(sbs_remove),
847 .suspend = sbs_suspend,
848 .resume = sbs_resume,
849 .id_table = sbs_id,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700850 .driver = {
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800851 .name = "sbs-battery",
852 .of_match_table = sbs_dt_ids,
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700853 },
854};
Axel Lin5ff92e72012-01-21 14:42:54 +0800855module_i2c_driver(sbs_battery_driver);
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700856
Rhyland Klein3ddca0622011-12-05 17:50:46 -0800857MODULE_DESCRIPTION("SBS battery monitor driver");
Rhyland Kleina7640bf2010-09-05 15:31:23 -0700858MODULE_LICENSE("GPL");