bq27520_fuelgauger.c 24 KB

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