blob: 2101c5f00bb66dcf8a5caa953568543e8f39fd41 [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 *
Duy Truong790f06d2013-02-13 16:38:12 -08005 * Copyright (c) 2011, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 *
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/mfd/pmic8058.h>
30#include <linux/regulator/pmic8058-regulator.h>
31#include <linux/gpio.h>
32#include <linux/regulator/consumer.h>
33#include <linux/regulator/machine.h>
34#include <linux/err.h>
35#include <linux/msm-charger.h>
36#include <linux/i2c/bq27520.h> /* use the same platform data as bq27520 */
37
38#define DRIVER_VERSION "1.1.0"
39/* Bq27541 standard data commands */
40#define BQ27541_REG_CNTL 0x00
41#define BQ27541_REG_AR 0x02
42#define BQ27541_REG_ARTTE 0x04
43#define BQ27541_REG_TEMP 0x06
44#define BQ27541_REG_VOLT 0x08
45#define BQ27541_REG_FLAGS 0x0A
46#define BQ27541_REG_NAC 0x0C
47#define BQ27541_REG_FAC 0x0e
48#define BQ27541_REG_RM 0x10
49#define BQ27541_REG_FCC 0x12
50#define BQ27541_REG_AI 0x14
51#define BQ27541_REG_TTE 0x16
52#define BQ27541_REG_TTF 0x18
53#define BQ27541_REG_SI 0x1a
54#define BQ27541_REG_STTE 0x1c
55#define BQ27541_REG_MLI 0x1e
56#define BQ27541_REG_MLTTE 0x20
57#define BQ27541_REG_AE 0x22
58#define BQ27541_REG_AP 0x24
59#define BQ27541_REG_TTECP 0x26
60#define BQ27541_REG_SOH 0x28
61#define BQ27541_REG_SOC 0x2c
62#define BQ27541_REG_NIC 0x2e
63#define BQ27541_REG_ICR 0x30
64#define BQ27541_REG_LOGIDX 0x32
65#define BQ27541_REG_LOGBUF 0x34
66
67#define BQ27541_FLAG_DSC BIT(0)
68#define BQ27541_FLAG_FC BIT(9)
69
70#define BQ27541_CS_DLOGEN BIT(15)
71#define BQ27541_CS_SS BIT(13)
72
73/* Control subcommands */
74#define BQ27541_SUBCMD_CTNL_STATUS 0x0000
75#define BQ27541_SUBCMD_DEVCIE_TYPE 0x0001
76#define BQ27541_SUBCMD_FW_VER 0x0002
77#define BQ27541_SUBCMD_HW_VER 0x0003
78#define BQ27541_SUBCMD_DF_CSUM 0x0004
79#define BQ27541_SUBCMD_PREV_MACW 0x0007
80#define BQ27541_SUBCMD_CHEM_ID 0x0008
81#define BQ27541_SUBCMD_BD_OFFSET 0x0009
82#define BQ27541_SUBCMD_INT_OFFSET 0x000a
83#define BQ27541_SUBCMD_CC_VER 0x000b
84#define BQ27541_SUBCMD_OCV 0x000c
85#define BQ27541_SUBCMD_BAT_INS 0x000d
86#define BQ27541_SUBCMD_BAT_REM 0x000e
87#define BQ27541_SUBCMD_SET_HIB 0x0011
88#define BQ27541_SUBCMD_CLR_HIB 0x0012
89#define BQ27541_SUBCMD_SET_SLP 0x0013
90#define BQ27541_SUBCMD_CLR_SLP 0x0014
91#define BQ27541_SUBCMD_FCT_RES 0x0015
92#define BQ27541_SUBCMD_ENABLE_DLOG 0x0018
93#define BQ27541_SUBCMD_DISABLE_DLOG 0x0019
94#define BQ27541_SUBCMD_SEALED 0x0020
95#define BQ27541_SUBCMD_ENABLE_IT 0x0021
96#define BQ27541_SUBCMD_DISABLE_IT 0x0023
97#define BQ27541_SUBCMD_CAL_MODE 0x0040
98#define BQ27541_SUBCMD_RESET 0x0041
99#define ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN (-2731)
100#define BQ27541_INIT_DELAY ((HZ)*1)
101
102/* If the system has several batteries we need a different name for each
103 * of them...
104 */
105static DEFINE_IDR(battery_id);
106static DEFINE_MUTEX(battery_mutex);
107
108struct bq27541_device_info;
109struct bq27541_access_methods {
110 int (*read)(u8 reg, int *rt_value, int b_single,
111 struct bq27541_device_info *di);
112};
113
114struct bq27541_device_info {
115 struct device *dev;
116 int id;
117 struct bq27541_access_methods *bus;
118 struct i2c_client *client;
119 struct work_struct counter;
120 /* 300ms delay is needed after bq27541 is powered up
121 * and before any successful I2C transaction
122 */
123 struct delayed_work hw_config;
124};
125
126static int coulomb_counter;
127static spinlock_t lock; /* protect access to coulomb_counter */
128
129static int bq27541_i2c_txsubcmd(u8 reg, unsigned short subcmd,
130 struct bq27541_device_info *di);
131
132static int bq27541_read(u8 reg, int *rt_value, int b_single,
133 struct bq27541_device_info *di)
134{
135 return di->bus->read(reg, rt_value, b_single, di);
136}
137
138/*
139 * Return the battery temperature in tenths of degree Celsius
140 * Or < 0 if something fails.
141 */
142static int bq27541_battery_temperature(struct bq27541_device_info *di)
143{
144 int ret;
145 int temp = 0;
146
147 ret = bq27541_read(BQ27541_REG_TEMP, &temp, 0, di);
148 if (ret) {
149 dev_err(di->dev, "error reading temperature\n");
150 return ret;
151 }
152
153 return temp + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
154}
155
156/*
157 * Return the battery Voltage in milivolts
158 * Or < 0 if something fails.
159 */
160static int bq27541_battery_voltage(struct bq27541_device_info *di)
161{
162 int ret;
163 int volt = 0;
164
165 ret = bq27541_read(BQ27541_REG_VOLT, &volt, 0, di);
166 if (ret) {
167 dev_err(di->dev, "error reading voltage\n");
168 return ret;
169 }
170
171 return volt * 1000;
172}
173
174static void bq27541_cntl_cmd(struct bq27541_device_info *di,
175 int subcmd)
176{
177 bq27541_i2c_txsubcmd(BQ27541_REG_CNTL, subcmd, di);
178}
179
180/*
181 * i2c specific code
182 */
183static int bq27541_i2c_txsubcmd(u8 reg, unsigned short subcmd,
184 struct bq27541_device_info *di)
185{
186 struct i2c_msg msg;
187 unsigned char data[3];
188 int ret;
189
190 if (!di->client)
191 return -ENODEV;
192
193 memset(data, 0, sizeof(data));
194 data[0] = reg;
195 data[1] = subcmd & 0x00FF;
196 data[2] = (subcmd & 0xFF00) >> 8;
197
198 msg.addr = di->client->addr;
199 msg.flags = 0;
200 msg.len = 3;
201 msg.buf = data;
202
203 ret = i2c_transfer(di->client->adapter, &msg, 1);
204 if (ret < 0)
205 return -EIO;
206
207 return 0;
208}
209
210static int bq27541_chip_config(struct bq27541_device_info *di)
211{
212 int flags = 0, ret = 0;
213
214 bq27541_cntl_cmd(di, BQ27541_SUBCMD_CTNL_STATUS);
215 udelay(66);
216 ret = bq27541_read(BQ27541_REG_CNTL, &flags, 0, di);
217 if (ret < 0) {
218 dev_err(di->dev, "error reading register %02x ret = %d\n",
219 BQ27541_REG_CNTL, ret);
220 return ret;
221 }
222 udelay(66);
223
224 bq27541_cntl_cmd(di, BQ27541_SUBCMD_ENABLE_IT);
225 udelay(66);
226
227 if (!(flags & BQ27541_CS_DLOGEN)) {
228 bq27541_cntl_cmd(di, BQ27541_SUBCMD_ENABLE_DLOG);
229 udelay(66);
230 }
231
232 return 0;
233}
234
235static void bq27541_coulomb_counter_work(struct work_struct *work)
236{
237 int value = 0, temp = 0, index = 0, ret = 0;
238 struct bq27541_device_info *di;
239 unsigned long flags;
240 int count = 0;
241
242 di = container_of(work, struct bq27541_device_info, counter);
243
244 /* retrieve 30 values from FIFO of coulomb data logging buffer
245 * and average over time
246 */
247 do {
248 ret = bq27541_read(BQ27541_REG_LOGBUF, &temp, 0, di);
249 if (ret < 0)
250 break;
251 if (temp != 0x7FFF) {
252 ++count;
253 value += temp;
254 }
255 /* delay 66uS, waiting time between continuous reading
256 * results
257 */
258 udelay(66);
259 ret = bq27541_read(BQ27541_REG_LOGIDX, &index, 0, di);
260 if (ret < 0)
261 break;
262 udelay(66);
263 } while (index != 0 || temp != 0x7FFF);
264
265 if (ret < 0) {
266 dev_err(di->dev, "Error reading datalog register\n");
267 return;
268 }
269
270 if (count) {
271 spin_lock_irqsave(&lock, flags);
272 coulomb_counter = value/count;
273 spin_unlock_irqrestore(&lock, flags);
274 }
275}
276
277struct bq27541_device_info *bq27541_di;
278
279static int bq27541_get_battery_mvolts(void)
280{
281 return bq27541_battery_voltage(bq27541_di);
282}
283
284static int bq27541_get_battery_temperature(void)
285{
286 return bq27541_battery_temperature(bq27541_di);
287}
288static int bq27541_is_battery_present(void)
289{
290 return 1;
291}
292static int bq27541_is_battery_temp_within_range(void)
293{
294 return 1;
295}
296static int bq27541_is_battery_id_valid(void)
297{
298 return 1;
299}
300
301static struct msm_battery_gauge bq27541_batt_gauge = {
302 .get_battery_mvolts = bq27541_get_battery_mvolts,
303 .get_battery_temperature = bq27541_get_battery_temperature,
304 .is_battery_present = bq27541_is_battery_present,
305 .is_battery_temp_within_range = bq27541_is_battery_temp_within_range,
306 .is_battery_id_valid = bq27541_is_battery_id_valid,
307};
308static void bq27541_hw_config(struct work_struct *work)
309{
310 int ret = 0, flags = 0, type = 0, fw_ver = 0;
311 struct bq27541_device_info *di;
312
313 di = container_of(work, struct bq27541_device_info, hw_config.work);
314 ret = bq27541_chip_config(di);
315 if (ret) {
316 dev_err(di->dev, "Failed to config Bq27541\n");
317 return;
318 }
319 msm_battery_gauge_register(&bq27541_batt_gauge);
320
321 bq27541_cntl_cmd(di, BQ27541_SUBCMD_CTNL_STATUS);
322 udelay(66);
323 bq27541_read(BQ27541_REG_CNTL, &flags, 0, di);
324 bq27541_cntl_cmd(di, BQ27541_SUBCMD_DEVCIE_TYPE);
325 udelay(66);
326 bq27541_read(BQ27541_REG_CNTL, &type, 0, di);
327 bq27541_cntl_cmd(di, BQ27541_SUBCMD_FW_VER);
328 udelay(66);
329 bq27541_read(BQ27541_REG_CNTL, &fw_ver, 0, di);
330
331 dev_info(di->dev, "DEVICE_TYPE is 0x%02X, FIRMWARE_VERSION is 0x%02X\n",
332 type, fw_ver);
333 dev_info(di->dev, "Complete bq27541 configuration 0x%02X\n", flags);
334}
335
336static int bq27541_read_i2c(u8 reg, int *rt_value, int b_single,
337 struct bq27541_device_info *di)
338{
339 struct i2c_client *client = di->client;
340 struct i2c_msg msg[1];
341 unsigned char data[2];
342 int err;
343
344 if (!client->adapter)
345 return -ENODEV;
346
347 msg->addr = client->addr;
348 msg->flags = 0;
349 msg->len = 1;
350 msg->buf = data;
351
352 data[0] = reg;
353 err = i2c_transfer(client->adapter, msg, 1);
354
355 if (err >= 0) {
356 if (!b_single)
357 msg->len = 2;
358 else
359 msg->len = 1;
360
361 msg->flags = I2C_M_RD;
362 err = i2c_transfer(client->adapter, msg, 1);
363 if (err >= 0) {
364 if (!b_single)
365 *rt_value = get_unaligned_le16(data);
366 else
367 *rt_value = data[0];
368
369 return 0;
370 }
371 }
372 return err;
373}
374
375#ifdef CONFIG_BQ27541_TEST_ENABLE
376static int reg;
377static int subcmd;
378static ssize_t bq27541_read_stdcmd(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
381 int ret;
382 int temp = 0;
383 struct platform_device *client;
384 struct bq27541_device_info *di;
385
386 client = to_platform_device(dev);
387 di = platform_get_drvdata(client);
388
389 if (reg <= BQ27541_REG_ICR && reg > 0x00) {
390 ret = bq27541_read(reg, &temp, 0, di);
391 if (ret)
392 ret = snprintf(buf, PAGE_SIZE, "Read Error!\n");
393 else
394 ret = snprintf(buf, PAGE_SIZE, "0x%02x\n", temp);
395 } else
396 ret = snprintf(buf, PAGE_SIZE, "Register Error!\n");
397
398 return ret;
399}
400
401static ssize_t bq27541_write_stdcmd(struct device *dev,
402 struct device_attribute *attr, const char *buf, size_t count)
403{
404 ssize_t ret = strnlen(buf, PAGE_SIZE);
405 int cmd;
406
407 sscanf(buf, "%x", &cmd);
408 reg = cmd;
409 return ret;
410}
411
412static ssize_t bq27541_read_subcmd(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
415 int ret;
416 int temp = 0;
417 struct platform_device *client;
418 struct bq27541_device_info *di;
419
420 client = to_platform_device(dev);
421 di = platform_get_drvdata(client);
422
423 if (subcmd == BQ27541_SUBCMD_DEVCIE_TYPE ||
424 subcmd == BQ27541_SUBCMD_FW_VER ||
425 subcmd == BQ27541_SUBCMD_HW_VER ||
426 subcmd == BQ27541_SUBCMD_CHEM_ID) {
427
428 bq27541_cntl_cmd(di, subcmd); /* Retrieve Chip status */
429 udelay(66);
430 ret = bq27541_read(BQ27541_REG_CNTL, &temp, 0, di);
431
432 if (ret)
433 ret = snprintf(buf, PAGE_SIZE, "Read Error!\n");
434 else
435 ret = snprintf(buf, PAGE_SIZE, "0x%02x\n", temp);
436 } else
437 ret = snprintf(buf, PAGE_SIZE, "Register Error!\n");
438
439 return ret;
440}
441
442static ssize_t bq27541_write_subcmd(struct device *dev,
443 struct device_attribute *attr, const char *buf, size_t count)
444{
445 ssize_t ret = strnlen(buf, PAGE_SIZE);
446 int cmd;
447
448 sscanf(buf, "%x", &cmd);
449 subcmd = cmd;
450 return ret;
451}
452
453static DEVICE_ATTR(std_cmd, S_IRUGO|S_IWUGO, bq27541_read_stdcmd,
454 bq27541_write_stdcmd);
455static DEVICE_ATTR(sub_cmd, S_IRUGO|S_IWUGO, bq27541_read_subcmd,
456 bq27541_write_subcmd);
457static struct attribute *fs_attrs[] = {
458 &dev_attr_std_cmd.attr,
459 &dev_attr_sub_cmd.attr,
460 NULL,
461};
462static struct attribute_group fs_attr_group = {
463 .attrs = fs_attrs,
464};
465
466static struct platform_device this_device = {
467 .name = "bq27541-test",
468 .id = -1,
469 .dev.platform_data = NULL,
470};
471#endif
472
473static int bq27541_battery_probe(struct i2c_client *client,
474 const struct i2c_device_id *id)
475{
476 char *name;
477 struct bq27541_device_info *di;
478 struct bq27541_access_methods *bus;
479 int num;
480 int retval = 0;
481
482 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
483 return -ENODEV;
484
485 /* Get new ID for the new battery device */
486 retval = idr_pre_get(&battery_id, GFP_KERNEL);
487 if (retval == 0)
488 return -ENOMEM;
489 mutex_lock(&battery_mutex);
490 retval = idr_get_new(&battery_id, client, &num);
491 mutex_unlock(&battery_mutex);
492 if (retval < 0)
493 return retval;
494
495 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
496 if (!name) {
497 dev_err(&client->dev, "failed to allocate device name\n");
498 retval = -ENOMEM;
499 goto batt_failed_1;
500 }
501
502 di = kzalloc(sizeof(*di), GFP_KERNEL);
503 if (!di) {
504 dev_err(&client->dev, "failed to allocate device info data\n");
505 retval = -ENOMEM;
506 goto batt_failed_2;
507 }
508 di->id = num;
509
510 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
511 if (!bus) {
512 dev_err(&client->dev, "failed to allocate access method "
513 "data\n");
514 retval = -ENOMEM;
515 goto batt_failed_3;
516 }
517
518 i2c_set_clientdata(client, di);
519 di->dev = &client->dev;
520 bus->read = &bq27541_read_i2c;
521 di->bus = bus;
522 di->client = client;
523
524#ifdef CONFIG_BQ27541_TEST_ENABLE
525 platform_set_drvdata(&this_device, di);
526 retval = platform_device_register(&this_device);
527 if (!retval) {
528 retval = sysfs_create_group(&this_device.dev.kobj,
529 &fs_attr_group);
530 if (retval)
531 goto batt_failed_4;
532 } else
533 goto batt_failed_4;
534#endif
535
536 if (retval) {
537 dev_err(&client->dev, "failed to setup bq27541\n");
538 goto batt_failed_4;
539 }
540
541 if (retval) {
542 dev_err(&client->dev, "failed to powerup bq27541\n");
543 goto batt_failed_4;
544 }
545
546 spin_lock_init(&lock);
547
548 bq27541_di = di;
549 INIT_WORK(&di->counter, bq27541_coulomb_counter_work);
550 INIT_DELAYED_WORK(&di->hw_config, bq27541_hw_config);
551 schedule_delayed_work(&di->hw_config, BQ27541_INIT_DELAY);
552 return 0;
553
554batt_failed_4:
555 kfree(bus);
556batt_failed_3:
557 kfree(di);
558batt_failed_2:
559 kfree(name);
560batt_failed_1:
561 mutex_lock(&battery_mutex);
562 idr_remove(&battery_id, num);
563 mutex_unlock(&battery_mutex);
564
565 return retval;
566}
567
568static int bq27541_battery_remove(struct i2c_client *client)
569{
570 struct bq27541_device_info *di = i2c_get_clientdata(client);
571
572 msm_battery_gauge_unregister(&bq27541_batt_gauge);
573 bq27541_cntl_cmd(di, BQ27541_SUBCMD_DISABLE_DLOG);
574 udelay(66);
575 bq27541_cntl_cmd(di, BQ27541_SUBCMD_DISABLE_IT);
576 cancel_delayed_work_sync(&di->hw_config);
577
578 kfree(di->bus);
579
580 mutex_lock(&battery_mutex);
581 idr_remove(&battery_id, di->id);
582 mutex_unlock(&battery_mutex);
583
584 kfree(di);
585 return 0;
586}
587
588static const struct i2c_device_id bq27541_id[] = {
589 { "bq27541", 1 },
590 {},
591};
592MODULE_DEVICE_TABLE(i2c, BQ27541_id);
593
594static struct i2c_driver bq27541_battery_driver = {
595 .driver = {
596 .name = "bq27541-battery",
597 },
598 .probe = bq27541_battery_probe,
599 .remove = bq27541_battery_remove,
600 .id_table = bq27541_id,
601};
602
603static int __init bq27541_battery_init(void)
604{
605 int ret;
606
607 ret = i2c_add_driver(&bq27541_battery_driver);
608 if (ret)
609 printk(KERN_ERR "Unable to register BQ27541 driver\n");
610
611 return ret;
612}
613module_init(bq27541_battery_init);
614
615static void __exit bq27541_battery_exit(void)
616{
617 i2c_del_driver(&bq27541_battery_driver);
618}
619module_exit(bq27541_battery_exit);
620
621MODULE_LICENSE("GPL v2");
622MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
623MODULE_DESCRIPTION("BQ27541 battery monitor driver");