bq27541_fuelgauger.c 15 KB

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