blob: 3c191cd691cb83f9c26ac57ddd62e8a00b8ca8eb [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
2 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
3 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
4 *
5 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
18#include <linux/param.h>
19#include <linux/jiffies.h>
20#include <linux/workqueue.h>
21#include <linux/delay.h>
22#include <linux/platform_device.h>
23#include <linux/power_supply.h>
24#include <linux/idr.h>
25#include <linux/i2c.h>
26#include <linux/slab.h>
27#include <asm/unaligned.h>
28#include <linux/time.h>
29#include <linux/i2c/bq27520.h>
30#include <linux/mfd/pmic8058.h>
31#include <linux/regulator/pmic8058-regulator.h>
32#include <linux/gpio.h>
33#include <linux/regulator/consumer.h>
34#include <linux/regulator/machine.h>
35#include <linux/err.h>
36#include <linux/msm-charger.h>
37
38#define DRIVER_VERSION "1.1.0"
39/* Bq27520 standard data commands */
40#define BQ27520_REG_CNTL 0x00
41#define BQ27520_REG_AR 0x02
42#define BQ27520_REG_ARTTE 0x04
43#define BQ27520_REG_TEMP 0x06
44#define BQ27520_REG_VOLT 0x08
45#define BQ27520_REG_FLAGS 0x0A
46#define BQ27520_REG_NAC 0x0C
47#define BQ27520_REG_FAC 0x0e
48#define BQ27520_REG_RM 0x10
49#define BQ27520_REG_FCC 0x12
50#define BQ27520_REG_AI 0x14
51#define BQ27520_REG_TTE 0x16
52#define BQ27520_REG_TTF 0x18
53#define BQ27520_REG_SI 0x1a
54#define BQ27520_REG_STTE 0x1c
55#define BQ27520_REG_MLI 0x1e
56#define BQ27520_REG_MLTTE 0x20
57#define BQ27520_REG_AE 0x22
58#define BQ27520_REG_AP 0x24
59#define BQ27520_REG_TTECP 0x26
60#define BQ27520_REG_SOH 0x28
61#define BQ27520_REG_SOC 0x2c
62#define BQ27520_REG_NIC 0x2e
63#define BQ27520_REG_ICR 0x30
64#define BQ27520_REG_LOGIDX 0x32
65#define BQ27520_REG_LOGBUF 0x34
66#define BQ27520_FLAG_DSC BIT(0)
67#define BQ27520_FLAG_FC BIT(9)
68#define BQ27520_FLAG_BAT_DET BIT(3)
69#define BQ27520_CS_DLOGEN BIT(15)
70#define BQ27520_CS_SS BIT(13)
71/* Control subcommands */
72#define BQ27520_SUBCMD_CTNL_STATUS 0x0000
73#define BQ27520_SUBCMD_DEVCIE_TYPE 0x0001
74#define BQ27520_SUBCMD_FW_VER 0x0002
75#define BQ27520_SUBCMD_HW_VER 0x0003
76#define BQ27520_SUBCMD_DF_CSUM 0x0004
77#define BQ27520_SUBCMD_PREV_MACW 0x0007
78#define BQ27520_SUBCMD_CHEM_ID 0x0008
79#define BQ27520_SUBCMD_BD_OFFSET 0x0009
80#define BQ27520_SUBCMD_INT_OFFSET 0x000a
81#define BQ27520_SUBCMD_CC_VER 0x000b
82#define BQ27520_SUBCMD_OCV 0x000c
83#define BQ27520_SUBCMD_BAT_INS 0x000d
84#define BQ27520_SUBCMD_BAT_REM 0x000e
85#define BQ27520_SUBCMD_SET_HIB 0x0011
86#define BQ27520_SUBCMD_CLR_HIB 0x0012
87#define BQ27520_SUBCMD_SET_SLP 0x0013
88#define BQ27520_SUBCMD_CLR_SLP 0x0014
89#define BQ27520_SUBCMD_FCT_RES 0x0015
90#define BQ27520_SUBCMD_ENABLE_DLOG 0x0018
91#define BQ27520_SUBCMD_DISABLE_DLOG 0x0019
92#define BQ27520_SUBCMD_SEALED 0x0020
93#define BQ27520_SUBCMD_ENABLE_IT 0x0021
94#define BQ27520_SUBCMD_DISABLE_IT 0x0023
95#define BQ27520_SUBCMD_CAL_MODE 0x0040
96#define BQ27520_SUBCMD_RESET 0x0041
97
98#define ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN (-2731)
99#define BQ27520_INIT_DELAY ((HZ)*1)
100#define BQ27520_POLLING_STATUS ((HZ)*3)
101#define BQ27520_COULOMB_POLL ((HZ)*30)
102
103/* If the system has several batteries we need a different name for each
104 * of them...
105 */
106static DEFINE_IDR(battery_id);
107static DEFINE_MUTEX(battery_mutex);
108
109struct bq27520_device_info;
110struct bq27520_access_methods {
111 int (*read)(u8 reg, int *rt_value, int b_single,
112 struct bq27520_device_info *di);
113};
114
115struct bq27520_device_info {
116 struct device *dev;
117 int id;
118 struct bq27520_access_methods *bus;
119 struct i2c_client *client;
120 const struct bq27520_platform_data *pdata;
121 struct work_struct counter;
122 /* 300ms delay is needed after bq27520 is powered up
123 * and before any successful I2C transaction
124 */
125 struct delayed_work hw_config;
126 uint32_t irq;
127};
128
129enum {
130 GET_BATTERY_STATUS,
131 GET_BATTERY_TEMPERATURE,
132 GET_BATTERY_VOLTAGE,
133 GET_BATTERY_CAPACITY,
134 NUM_OF_STATUS,
135};
136
137struct bq27520_status {
138 /* Informations owned and maintained by Bq27520 driver, updated
139 * by poller or SOC_INT interrupt, decoupling from I/Oing
140 * hardware directly
141 */
142 int status[NUM_OF_STATUS];
143 spinlock_t lock;
144 struct delayed_work poller;
145};
146
147static struct bq27520_status current_battery_status;
148static struct bq27520_device_info *bq27520_di;
149static int coulomb_counter;
150static spinlock_t lock; /* protect access to coulomb_counter */
151static struct timer_list timer; /* charge counter timer every 30 secs */
152
153static int bq27520_i2c_txsubcmd(u8 reg, unsigned short subcmd,
154 struct bq27520_device_info *di);
155
156static int bq27520_read(u8 reg, int *rt_value, int b_single,
157 struct bq27520_device_info *di)
158{
159 return di->bus->read(reg, rt_value, b_single, di);
160}
161
162/*
163 * Return the battery temperature in tenths of degree Celsius
164 * Or < 0 if something fails.
165 */
166static int bq27520_battery_temperature(struct bq27520_device_info *di)
167{
168 int ret, temp = 0;
169
170 ret = bq27520_read(BQ27520_REG_TEMP, &temp, 0, di);
171 if (ret) {
172 dev_err(di->dev, "error %d reading temperature\n", ret);
173 return ret;
174 }
175
176 return temp + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
177}
178
179/*
180 * Return the battery Voltage in milivolts
181 * Or < 0 if something fails.
182 */
183static int bq27520_battery_voltage(struct bq27520_device_info *di)
184{
185 int ret, volt = 0;
186
187 ret = bq27520_read(BQ27520_REG_VOLT, &volt, 0, di);
188 if (ret) {
189 dev_err(di->dev, "error %d reading voltage\n", ret);
190 return ret;
191 }
192
193 return volt;
194}
195
196/*
197 * Return the battery Relative State-of-Charge
198 * Or < 0 if something fails.
199 */
200static int bq27520_battery_rsoc(struct bq27520_device_info *di)
201{
202 int ret, rsoc = 0;
203
204 ret = bq27520_read(BQ27520_REG_SOC, &rsoc, 0, di);
205
206 if (ret) {
207 dev_err(di->dev,
208 "error %d reading relative State-of-Charge\n", ret);
209 return ret;
210 }
211
212 return rsoc;
213}
214
215static void bq27520_cntl_cmd(struct bq27520_device_info *di,
216 int subcmd)
217{
218 bq27520_i2c_txsubcmd(BQ27520_REG_CNTL, subcmd, di);
219}
220
221/*
222 * i2c specific code
223 */
224static int bq27520_i2c_txsubcmd(u8 reg, unsigned short subcmd,
225 struct bq27520_device_info *di)
226{
227 struct i2c_msg msg;
228 unsigned char data[3];
229
230 if (!di->client)
231 return -ENODEV;
232
233 memset(data, 0, sizeof(data));
234 data[0] = reg;
235 data[1] = subcmd & 0x00FF;
236 data[2] = (subcmd & 0xFF00) >> 8;
237
238 msg.addr = di->client->addr;
239 msg.flags = 0;
240 msg.len = 3;
241 msg.buf = data;
242
243 if (i2c_transfer(di->client->adapter, &msg, 1) < 0)
244 return -EIO;
245
246 return 0;
247}
248
249static int bq27520_chip_config(struct bq27520_device_info *di)
250{
251 int flags = 0, ret = 0;
252
253 bq27520_cntl_cmd(di, BQ27520_SUBCMD_CTNL_STATUS);
254 udelay(66);
255 ret = bq27520_read(BQ27520_REG_CNTL, &flags, 0, di);
256 if (ret < 0) {
257 dev_err(di->dev, "error %d reading register %02x\n",
258 ret, BQ27520_REG_CNTL);
259 return ret;
260 }
261 udelay(66);
262
263 bq27520_cntl_cmd(di, BQ27520_SUBCMD_ENABLE_IT);
264 udelay(66);
265
266 if (di->pdata->enable_dlog && !(flags & BQ27520_CS_DLOGEN)) {
267 bq27520_cntl_cmd(di, BQ27520_SUBCMD_ENABLE_DLOG);
268 udelay(66);
269 }
270
271 return 0;
272}
273
274static void bq27520_every_30secs(unsigned long data)
275{
276 struct bq27520_device_info *di = (struct bq27520_device_info *)data;
277
278 schedule_work(&di->counter);
279 mod_timer(&timer, jiffies + BQ27520_COULOMB_POLL);
280}
281
282static void bq27520_coulomb_counter_work(struct work_struct *work)
283{
284 int value = 0, temp = 0, index = 0, ret = 0, count = 0;
285 struct bq27520_device_info *di;
286 unsigned long flags;
287
288 di = container_of(work, struct bq27520_device_info, counter);
289
290 /* retrieve 30 values from FIFO of coulomb data logging buffer
291 * and average over time
292 */
293 do {
294 ret = bq27520_read(BQ27520_REG_LOGBUF, &temp, 0, di);
295 if (ret < 0)
296 break;
297 if (temp != 0x7FFF) {
298 ++count;
299 value += temp;
300 }
301 udelay(66);
302 ret = bq27520_read(BQ27520_REG_LOGIDX, &index, 0, di);
303 if (ret < 0)
304 break;
305 udelay(66);
306 } while (index != 0 || temp != 0x7FFF);
307
308 if (ret < 0) {
309 dev_err(di->dev, "Error %d reading datalog register\n", ret);
310 return;
311 }
312
313 if (count) {
314 spin_lock_irqsave(&lock, flags);
315 coulomb_counter = value/count;
316 spin_unlock_irqrestore(&lock, flags);
317 }
318}
319
320static int bq27520_is_battery_present(void)
321{
322 return 1;
323}
324
325static int bq27520_is_battery_temp_within_range(void)
326{
327 return 1;
328}
329
330static int bq27520_is_battery_id_valid(void)
331{
332 return 1;
333}
334
335static int bq27520_status_getter(int function)
336{
337 int status = 0;
338 unsigned long flags;
339
340 spin_lock_irqsave(&current_battery_status.lock, flags);
341 status = current_battery_status.status[function];
342 spin_unlock_irqrestore(&current_battery_status.lock, flags);
343
344 return status;
345}
346
347static int bq27520_get_battery_mvolts(void)
348{
349 return bq27520_status_getter(GET_BATTERY_VOLTAGE);
350}
351
352static int bq27520_get_battery_temperature(void)
353{
354 return bq27520_status_getter(GET_BATTERY_TEMPERATURE);
355}
356
357static int bq27520_get_battery_status(void)
358{
359 return bq27520_status_getter(GET_BATTERY_STATUS);
360}
361
362static int bq27520_get_remaining_capacity(void)
363{
364 return bq27520_status_getter(GET_BATTERY_CAPACITY);
365}
366
367static struct msm_battery_gauge bq27520_batt_gauge = {
368 .get_battery_mvolts = bq27520_get_battery_mvolts,
369 .get_battery_temperature = bq27520_get_battery_temperature,
370 .is_battery_present = bq27520_is_battery_present,
371 .is_battery_temp_within_range = bq27520_is_battery_temp_within_range,
372 .is_battery_id_valid = bq27520_is_battery_id_valid,
373 .get_battery_status = bq27520_get_battery_status,
374 .get_batt_remaining_capacity = bq27520_get_remaining_capacity,
375};
376
377static void update_current_battery_status(int data)
378{
379 int status[4], ret = 0;
380 unsigned long flag;
381
382 memset(status, 0, sizeof status);
383 ret = bq27520_battery_rsoc(bq27520_di);
384 status[GET_BATTERY_CAPACITY] = (ret < 0) ? 0 : ret;
385
386 status[GET_BATTERY_VOLTAGE] = bq27520_battery_voltage(bq27520_di);
387 status[GET_BATTERY_TEMPERATURE] =
388 bq27520_battery_temperature(bq27520_di);
389
390 spin_lock_irqsave(&current_battery_status.lock, flag);
391 current_battery_status.status[GET_BATTERY_STATUS] = data;
392 current_battery_status.status[GET_BATTERY_VOLTAGE] =
393 status[GET_BATTERY_VOLTAGE];
394 current_battery_status.status[GET_BATTERY_TEMPERATURE] =
395 status[GET_BATTERY_TEMPERATURE];
396 current_battery_status.status[GET_BATTERY_CAPACITY] =
397 status[GET_BATTERY_CAPACITY];
398 spin_unlock_irqrestore(&current_battery_status.lock, flag);
399}
400
401/* only if battery charging satus changes then notify msm_charger. otherwise
402 * only refresh current_batter_status
403 */
404static int if_notify_msm_charger(int *data)
405{
406 int ret = 0, flags = 0, status = 0;
407 unsigned long flag;
408
409 ret = bq27520_read(BQ27520_REG_FLAGS, &flags, 0, bq27520_di);
410 if (ret < 0) {
411 dev_err(bq27520_di->dev, "error %d reading register %02x\n",
412 ret, BQ27520_REG_FLAGS);
413 status = POWER_SUPPLY_STATUS_UNKNOWN;
414 } else {
415 if (flags & BQ27520_FLAG_FC)
416 status = POWER_SUPPLY_STATUS_FULL;
417 else if (flags & BQ27520_FLAG_DSC)
418 status = POWER_SUPPLY_STATUS_DISCHARGING;
419 else
420 status = POWER_SUPPLY_STATUS_CHARGING;
421 }
422
423 *data = status;
424 spin_lock_irqsave(&current_battery_status.lock, flag);
425 ret = (status != current_battery_status.status[GET_BATTERY_STATUS]);
426 spin_unlock_irqrestore(&current_battery_status.lock, flag);
427 return ret;
428}
429
430static void battery_status_poller(struct work_struct *work)
431{
432 int status = 0, temp = 0;
433
434 temp = if_notify_msm_charger(&status);
435 update_current_battery_status(status);
436 if (temp)
437 msm_charger_notify_event(NULL, CHG_BATT_STATUS_CHANGE);
438
439 schedule_delayed_work(&current_battery_status.poller,
440 BQ27520_POLLING_STATUS);
441}
442
443static void bq27520_hw_config(struct work_struct *work)
444{
445 int ret = 0, flags = 0, type = 0, fw_ver = 0, status = 0;
446 struct bq27520_device_info *di;
447
448 di = container_of(work, struct bq27520_device_info, hw_config.work);
449
450 pr_debug(KERN_INFO "Enter bq27520_hw_config\n");
451 ret = bq27520_chip_config(di);
452 if (ret) {
453 dev_err(di->dev, "Failed to config Bq27520 ret = %d\n", ret);
454 return;
455 }
456 /* bq27520 is ready for access, update current_battery_status by reading
457 * from hardware
458 */
459 if_notify_msm_charger(&status);
460 update_current_battery_status(status);
Michael Bohan98f5fd42011-10-05 11:38:59 -0700461 msm_battery_gauge_register(&bq27520_batt_gauge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462 msm_charger_notify_event(NULL, CHG_BATT_STATUS_CHANGE);
463
464 enable_irq(di->irq);
465
466 /* poll battery status every 3 seconds, if charging status changes,
467 * notify msm_charger
468 */
469 schedule_delayed_work(&current_battery_status.poller,
470 BQ27520_POLLING_STATUS);
471
472 if (di->pdata->enable_dlog) {
473 schedule_work(&di->counter);
474 init_timer(&timer);
475 timer.function = &bq27520_every_30secs;
476 timer.data = (unsigned long)di;
477 timer.expires = jiffies + BQ27520_COULOMB_POLL;
478 add_timer(&timer);
479 }
480
481 bq27520_cntl_cmd(di, BQ27520_SUBCMD_CTNL_STATUS);
482 udelay(66);
483 bq27520_read(BQ27520_REG_CNTL, &flags, 0, di);
484 bq27520_cntl_cmd(di, BQ27520_SUBCMD_DEVCIE_TYPE);
485 udelay(66);
486 bq27520_read(BQ27520_REG_CNTL, &type, 0, di);
487 bq27520_cntl_cmd(di, BQ27520_SUBCMD_FW_VER);
488 udelay(66);
489 bq27520_read(BQ27520_REG_CNTL, &fw_ver, 0, di);
490
491 dev_info(di->dev, "DEVICE_TYPE is 0x%02X, FIRMWARE_VERSION\
492 is 0x%02X\n", type, fw_ver);
493 dev_info(di->dev, "Complete bq27520 configuration 0x%02X\n", flags);
494}
495
496static int bq27520_read_i2c(u8 reg, int *rt_value, int b_single,
497 struct bq27520_device_info *di)
498{
499 struct i2c_client *client = di->client;
500 struct i2c_msg msg[1];
501 unsigned char data[2];
502 int err;
503
504 if (!client->adapter)
505 return -ENODEV;
506
507 msg->addr = client->addr;
508 msg->flags = 0;
509 msg->len = 1;
510 msg->buf = data;
511
512 data[0] = reg;
513 err = i2c_transfer(client->adapter, msg, 1);
514
515 if (err >= 0) {
516 if (!b_single)
517 msg->len = 2;
518 else
519 msg->len = 1;
520
521 msg->flags = I2C_M_RD;
522 err = i2c_transfer(client->adapter, msg, 1);
523 if (err >= 0) {
524 if (!b_single)
525 *rt_value = get_unaligned_le16(data);
526 else
527 *rt_value = data[0];
528
529 return 0;
530 }
531 }
532 return err;
533}
534
535#ifdef CONFIG_BQ27520_TEST_ENABLE
536static int reg;
537static int subcmd;
538static ssize_t bq27520_read_stdcmd(struct device *dev,
539 struct device_attribute *attr, char *buf)
540{
541 int ret;
542 int temp = 0;
543 struct platform_device *client;
544 struct bq27520_device_info *di;
545
546 client = to_platform_device(dev);
547 di = platform_get_drvdata(client);
548
549 if (reg <= BQ27520_REG_ICR && reg > 0x00) {
550 ret = bq27520_read(reg, &temp, 0, di);
551 if (ret)
552 ret = snprintf(buf, PAGE_SIZE, "Read Error!\n");
553 else
554 ret = snprintf(buf, PAGE_SIZE, "0x%02x\n", temp);
555 } else
556 ret = snprintf(buf, PAGE_SIZE, "Register Error!\n");
557
558 return ret;
559}
560
561static ssize_t bq27520_write_stdcmd(struct device *dev,
562 struct device_attribute *attr, const char *buf, size_t count)
563{
564 ssize_t ret = strnlen(buf, PAGE_SIZE);
565 int cmd;
566
567 sscanf(buf, "%x", &cmd);
568 reg = cmd;
569 dev_info(dev, "recv'd cmd is 0x%02X\n", reg);
570 return ret;
571}
572
573static ssize_t bq27520_read_subcmd(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
576 int ret, temp = 0;
577 struct platform_device *client;
578 struct bq27520_device_info *di;
579
580 client = to_platform_device(dev);
581 di = platform_get_drvdata(client);
582
583 if (subcmd == BQ27520_SUBCMD_DEVCIE_TYPE ||
584 subcmd == BQ27520_SUBCMD_FW_VER ||
585 subcmd == BQ27520_SUBCMD_HW_VER ||
586 subcmd == BQ27520_SUBCMD_CHEM_ID) {
587
588 bq27520_cntl_cmd(di, subcmd);/* Retrieve Chip status */
589 udelay(66);
590 ret = bq27520_read(BQ27520_REG_CNTL, &temp, 0, di);
591
592 if (ret)
593 ret = snprintf(buf, PAGE_SIZE, "Read Error!\n");
594 else
595 ret = snprintf(buf, PAGE_SIZE, "0x%02x\n", temp);
596 } else
597 ret = snprintf(buf, PAGE_SIZE, "Register Error!\n");
598
599 return ret;
600}
601
602static ssize_t bq27520_write_subcmd(struct device *dev,
603 struct device_attribute *attr, const char *buf, size_t count)
604{
605 ssize_t ret = strnlen(buf, PAGE_SIZE);
606 int cmd;
607
608 sscanf(buf, "%x", &cmd);
609 subcmd = cmd;
610 return ret;
611}
612
613static DEVICE_ATTR(std_cmd, S_IRUGO|S_IWUGO, bq27520_read_stdcmd,
614 bq27520_write_stdcmd);
615static DEVICE_ATTR(sub_cmd, S_IRUGO|S_IWUGO, bq27520_read_subcmd,
616 bq27520_write_subcmd);
617static struct attribute *fs_attrs[] = {
618 &dev_attr_std_cmd.attr,
619 &dev_attr_sub_cmd.attr,
620 NULL,
621};
622static struct attribute_group fs_attr_group = {
623 .attrs = fs_attrs,
624};
625
626static struct platform_device this_device = {
627 .name = "bq27520-test",
628 .id = -1,
629 .dev.platform_data = NULL,
630};
631#endif
632
633static irqreturn_t soc_irqhandler(int irq, void *dev_id)
634{
635 int status = 0, temp = 0;
636
637 temp = if_notify_msm_charger(&status);
638 update_current_battery_status(status);
639 if (temp)
640 msm_charger_notify_event(NULL, CHG_BATT_STATUS_CHANGE);
641 return IRQ_HANDLED;
642}
643
644static struct regulator *vreg_bq27520;
645static int bq27520_power(bool enable, struct bq27520_device_info *di)
646{
647 int rc = 0, ret;
648 const struct bq27520_platform_data *platdata;
649
650 platdata = di->pdata;
651 if (enable) {
652 /* switch on Vreg_S3 */
653 rc = regulator_enable(vreg_bq27520);
654 if (rc < 0) {
655 dev_err(di->dev, "%s: vreg %s %s failed (%d)\n",
656 __func__, platdata->vreg_name, "enable", rc);
657 goto vreg_fail;
658 }
659
660 /* Battery gauge enable and switch on onchip 2.5V LDO */
661 rc = gpio_request(platdata->chip_en, "GAUGE_EN");
662 if (rc) {
663 dev_err(di->dev, "%s: fail to request gpio %d (%d)\n",
664 __func__, platdata->chip_en, rc);
665 goto vreg_fail;
666 }
667
668 gpio_direction_output(platdata->chip_en, 0);
669 gpio_set_value(platdata->chip_en, 1);
670 rc = gpio_request(platdata->soc_int, "GAUGE_SOC_INT");
671 if (rc) {
672 dev_err(di->dev, "%s: fail to request gpio %d (%d)\n",
673 __func__, platdata->soc_int, rc);
674 goto gpio_fail;
675 }
676 gpio_direction_input(platdata->soc_int);
677 di->irq = gpio_to_irq(platdata->soc_int);
678 rc = request_threaded_irq(di->irq, NULL, soc_irqhandler,
679 IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING,
680 "BQ27520_IRQ", di);
681 if (rc) {
682 dev_err(di->dev, "%s: fail to request irq %d (%d)\n",
683 __func__, platdata->soc_int, rc);
684 goto irqreq_fail;
685 } else {
686 disable_irq_nosync(di->irq);
687 }
688 } else {
689 free_irq(di->irq, di);
690 gpio_free(platdata->soc_int);
691 /* switch off on-chip 2.5V LDO and disable Battery gauge */
692 gpio_set_value(platdata->chip_en, 0);
693 gpio_free(platdata->chip_en);
694 /* switch off Vreg_S3 */
695 rc = regulator_disable(vreg_bq27520);
696 if (rc < 0) {
697 dev_err(di->dev, "%s: vreg %s %s failed (%d)\n",
698 __func__, platdata->vreg_name, "disable", rc);
699 goto vreg_fail;
700 }
701 }
702 return rc;
703
704irqreq_fail:
705 gpio_free(platdata->soc_int);
706gpio_fail:
707 gpio_set_value(platdata->chip_en, 0);
708 gpio_free(platdata->chip_en);
709vreg_fail:
710 ret = !enable ? regulator_enable(vreg_bq27520) :
711 regulator_disable(vreg_bq27520);
712 if (ret < 0) {
713 dev_err(di->dev, "%s: vreg %s %s failed (%d) in err path\n",
714 __func__, platdata->vreg_name,
715 !enable ? "enable" : "disable", ret);
716 }
717 return rc;
718}
719
720static int bq27520_dev_setup(bool enable, struct bq27520_device_info *di)
721{
722 int rc;
723 const struct bq27520_platform_data *platdata;
724
725 platdata = di->pdata;
726 if (enable) {
727 /* enable and set voltage Vreg_S3 */
728 vreg_bq27520 = regulator_get(NULL,
729 platdata->vreg_name);
730 if (IS_ERR(vreg_bq27520)) {
731 dev_err(di->dev, "%s: regulator get of %s\
732 failed (%ld)\n", __func__, platdata->vreg_name,
733 PTR_ERR(vreg_bq27520));
734 rc = PTR_ERR(vreg_bq27520);
735 goto vreg_get_fail;
736 }
737 rc = regulator_set_voltage(vreg_bq27520,
738 platdata->vreg_value, platdata->vreg_value);
739 if (rc) {
740 dev_err(di->dev, "%s: regulator_set_voltage(%s) failed\
741 (%d)\n", __func__, platdata->vreg_name, rc);
742 goto vreg_get_fail;
743 }
744 } else {
745 regulator_put(vreg_bq27520);
746 }
747 return 0;
748
749vreg_get_fail:
750 regulator_put(vreg_bq27520);
751 return rc;
752}
753
754static int bq27520_battery_probe(struct i2c_client *client,
755 const struct i2c_device_id *id)
756{
757 struct bq27520_device_info *di;
758 struct bq27520_access_methods *bus;
759 const struct bq27520_platform_data *pdata;
760 int num, retval = 0;
761
762 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
763 return -ENODEV;
764
765 pdata = client->dev.platform_data;
766
767 /* Get new ID for the new battery device */
768 retval = idr_pre_get(&battery_id, GFP_KERNEL);
769 if (retval == 0)
770 return -ENOMEM;
771 mutex_lock(&battery_mutex);
772 retval = idr_get_new(&battery_id, client, &num);
773 mutex_unlock(&battery_mutex);
774 if (retval < 0)
775 return retval;
776
777 di = kzalloc(sizeof(*di), GFP_KERNEL);
778 if (!di) {
779 dev_err(&client->dev, "failed to allocate device info data\n");
780 retval = -ENOMEM;
781 goto batt_failed_1;
782 }
783 di->id = num;
784 di->pdata = pdata;
785
786 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
787 if (!bus) {
788 dev_err(&client->dev, "failed to allocate data\n");
789 retval = -ENOMEM;
790 goto batt_failed_2;
791 }
792
793 i2c_set_clientdata(client, di);
794 di->dev = &client->dev;
795 bus->read = &bq27520_read_i2c;
796 di->bus = bus;
797 di->client = client;
798
799#ifdef CONFIG_BQ27520_TEST_ENABLE
800 platform_set_drvdata(&this_device, di);
801 retval = platform_device_register(&this_device);
802 if (!retval) {
803 retval = sysfs_create_group(&this_device.dev.kobj,
804 &fs_attr_group);
805 if (retval)
806 goto batt_failed_3;
807 } else
808 goto batt_failed_3;
809#endif
810
811 retval = bq27520_dev_setup(true, di);
812 if (retval) {
813 dev_err(&client->dev, "failed to setup ret = %d\n", retval);
814 goto batt_failed_3;
815 }
816
817 retval = bq27520_power(true, di);
818 if (retval) {
819 dev_err(&client->dev, "failed to powerup ret = %d\n", retval);
820 goto batt_failed_3;
821 }
822
823 spin_lock_init(&lock);
824
825 bq27520_di = di;
826 if (pdata->enable_dlog)
827 INIT_WORK(&di->counter, bq27520_coulomb_counter_work);
828
829 INIT_DELAYED_WORK(&current_battery_status.poller,
830 battery_status_poller);
831 INIT_DELAYED_WORK(&di->hw_config, bq27520_hw_config);
832 schedule_delayed_work(&di->hw_config, BQ27520_INIT_DELAY);
833
834 return 0;
835
836batt_failed_3:
837 kfree(bus);
838batt_failed_2:
839 kfree(di);
840batt_failed_1:
841 mutex_lock(&battery_mutex);
842 idr_remove(&battery_id, num);
843 mutex_unlock(&battery_mutex);
844
845 return retval;
846}
847
848static int bq27520_battery_remove(struct i2c_client *client)
849{
850 struct bq27520_device_info *di = i2c_get_clientdata(client);
851
852 if (di->pdata->enable_dlog) {
853 del_timer_sync(&timer);
854 cancel_work_sync(&di->counter);
855 bq27520_cntl_cmd(di, BQ27520_SUBCMD_DISABLE_DLOG);
856 udelay(66);
857 }
858
859 bq27520_cntl_cmd(di, BQ27520_SUBCMD_DISABLE_IT);
860 cancel_delayed_work_sync(&di->hw_config);
861 cancel_delayed_work_sync(&current_battery_status.poller);
862
863 bq27520_dev_setup(false, di);
864 bq27520_power(false, di);
865
866 kfree(di->bus);
867
868 mutex_lock(&battery_mutex);
869 idr_remove(&battery_id, di->id);
870 mutex_unlock(&battery_mutex);
871
872 kfree(di);
873 return 0;
874}
875
876#ifdef CONFIG_PM
877static int bq27520_suspend(struct device *dev)
878{
879 struct bq27520_device_info *di = dev_get_drvdata(dev);
880
881 disable_irq_nosync(di->irq);
882 if (di->pdata->enable_dlog) {
883 del_timer_sync(&timer);
884 cancel_work_sync(&di->counter);
885 }
886
887 cancel_delayed_work_sync(&current_battery_status.poller);
888 return 0;
889}
890
891static int bq27520_resume(struct device *dev)
892{
893 struct bq27520_device_info *di = dev_get_drvdata(dev);
894
895 enable_irq(di->irq);
896 if (di->pdata->enable_dlog)
897 add_timer(&timer);
898
899 schedule_delayed_work(&current_battery_status.poller,
900 BQ27520_POLLING_STATUS);
901 return 0;
902}
903
904static const struct dev_pm_ops bq27520_pm_ops = {
905 .suspend = bq27520_suspend,
906 .resume = bq27520_resume,
907};
908#endif
909
910static const struct i2c_device_id bq27520_id[] = {
911 { "bq27520", 1 },
912 {},
913};
914MODULE_DEVICE_TABLE(i2c, BQ27520_id);
915
916static struct i2c_driver bq27520_battery_driver = {
917 .driver = {
918 .name = "bq27520-battery",
919 .owner = THIS_MODULE,
920#ifdef CONFIG_PM
921 .pm = &bq27520_pm_ops,
922#endif
923 },
924 .probe = bq27520_battery_probe,
925 .remove = bq27520_battery_remove,
926 .id_table = bq27520_id,
927};
928
929static void init_battery_status(void)
930{
931 spin_lock_init(&current_battery_status.lock);
932 current_battery_status.status[GET_BATTERY_STATUS] =
933 POWER_SUPPLY_STATUS_UNKNOWN;
934}
935
936static int __init bq27520_battery_init(void)
937{
938 int ret;
939
940 /* initialize current_battery_status, and register with msm-charger */
941 init_battery_status();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942
943 ret = i2c_add_driver(&bq27520_battery_driver);
944 if (ret)
945 printk(KERN_ERR "Unable to register driver ret = %d\n", ret);
946
947 return ret;
948}
949module_init(bq27520_battery_init);
950
951static void __exit bq27520_battery_exit(void)
952{
953 i2c_del_driver(&bq27520_battery_driver);
954 msm_battery_gauge_unregister(&bq27520_batt_gauge);
955}
956module_exit(bq27520_battery_exit);
957
958MODULE_LICENSE("GPL v2");
959MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
960MODULE_DESCRIPTION("BQ27520 battery monitor driver");