bq28400_battery.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* Copyright (c) 2012-2013 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. /*
  14. * High Level description:
  15. * http://www.ti.com/lit/ds/symlink/bq28400.pdf
  16. * Thechnical Reference:
  17. * http://www.ti.com/lit/ug/sluu431/sluu431.pdf
  18. */
  19. #define pr_fmt(fmt) "%s: " fmt, __func__
  20. #include <linux/i2c.h>
  21. #include <linux/gpio.h>
  22. #include <linux/errno.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/slab.h>
  29. #include <linux/power_supply.h>
  30. #include <linux/bitops.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/printk.h>
  33. #define BQ28400_NAME "bq28400"
  34. #define BQ28400_REV "1.0"
  35. /* SBS Commands (page 63) */
  36. #define SBS_MANUFACTURER_ACCESS 0x00
  37. #define SBS_BATTERY_MODE 0x03
  38. #define SBS_TEMPERATURE 0x08
  39. #define SBS_VOLTAGE 0x09
  40. #define SBS_CURRENT 0x0A
  41. #define SBS_AVG_CURRENT 0x0B
  42. #define SBS_MAX_ERROR 0x0C
  43. #define SBS_RSOC 0x0D /* Relative State Of Charge */
  44. #define SBS_REMAIN_CAPACITY 0x0F
  45. #define SBS_FULL_CAPACITY 0x10
  46. #define SBS_CHG_CURRENT 0x14
  47. #define SBS_CHG_VOLTAGE 0x15
  48. #define SBS_BATTERY_STATUS 0x16
  49. #define SBS_CYCLE_COUNT 0x17
  50. #define SBS_DESIGN_CAPACITY 0x18
  51. #define SBS_DESIGN_VOLTAGE 0x19
  52. #define SBS_SPEC_INFO 0x1A
  53. #define SBS_MANUFACTURE_DATE 0x1B
  54. #define SBS_SERIAL_NUMBER 0x1C
  55. #define SBS_MANUFACTURER_NAME 0x20
  56. #define SBS_DEVICE_NAME 0x21
  57. #define SBS_DEVICE_CHEMISTRY 0x22
  58. #define SBS_MANUFACTURER_DATA 0x23
  59. #define SBS_AUTHENTICATE 0x2F
  60. #define SBS_CELL_VOLTAGE1 0x3E
  61. #define SBS_CELL_VOLTAGE2 0x3F
  62. /* Extended SBS Commands (page 71) */
  63. #define SBS_FET_CONTROL 0x46
  64. #define SBS_SAFETY_ALERT 0x50
  65. #define SBS_SAFETY_STATUS 0x51
  66. #define SBS_PE_ALERT 0x52
  67. #define SBS_PE_STATUS 0x53
  68. #define SBS_OPERATION_STATUS 0x54
  69. #define SBS_CHARGING_STATUS 0x55
  70. #define SBS_FET_STATUS 0x56
  71. #define SBS_PACK_VOLTAGE 0x5A
  72. #define SBS_TS0_TEMPERATURE 0x5E
  73. #define SBS_FULL_ACCESS_KEY 0x61
  74. #define SBS_PF_KEY 0x62
  75. #define SBS_AUTH_KEY3 0x63
  76. #define SBS_AUTH_KEY2 0x64
  77. #define SBS_AUTH_KEY1 0x65
  78. #define SBS_AUTH_KEY0 0x66
  79. #define SBS_MANUFACTURER_INFO 0x70
  80. #define SBS_SENSE_RESISTOR 0x71
  81. #define SBS_TEMP_RANGE 0x72
  82. /* SBS Sub-Commands (16 bits) */
  83. /* SBS_MANUFACTURER_ACCESS CMD */
  84. #define SUBCMD_DEVICE_TYPE 0x01
  85. #define SUBCMD_FIRMWARE_VERSION 0x02
  86. #define SUBCMD_HARDWARE_VERSION 0x03
  87. #define SUBCMD_DF_CHECKSUM 0x04
  88. #define SUBCMD_EDV 0x05
  89. #define SUBCMD_CHEMISTRY_ID 0x08
  90. /* SBS_CHARGING_STATUS */
  91. #define CHG_STATUS_BATTERY_DEPLETED BIT(0)
  92. #define CHG_STATUS_OVERCHARGE BIT(1)
  93. #define CHG_STATUS_OVERCHARGE_CURRENT BIT(2)
  94. #define CHG_STATUS_OVERCHARGE_VOLTAGE BIT(3)
  95. #define CHG_STATUS_CELL_BALANCING BIT(6)
  96. #define CHG_STATUS_HOT_TEMP_CHARGING BIT(8)
  97. #define CHG_STATUS_STD1_TEMP_CHARGING BIT(9)
  98. #define CHG_STATUS_STD2_TEMP_CHARGING BIT(10)
  99. #define CHG_STATUS_LOW_TEMP_CHARGING BIT(11)
  100. #define CHG_STATUS_PRECHARGING_EXIT BIT(13)
  101. #define CHG_STATUS_SUSPENDED BIT(14)
  102. #define CHG_STATUS_DISABLED BIT(15)
  103. /* SBS_FET_STATUS */
  104. #define FET_STATUS_DISCHARGE BIT(1)
  105. #define FET_STATUS_CHARGE BIT(2)
  106. #define FET_STATUS_PRECHARGE BIT(3)
  107. /* SBS_BATTERY_STATUS */
  108. #define BAT_STATUS_SBS_ERROR 0x0F
  109. #define BAT_STATUS_EMPTY BIT(4)
  110. #define BAT_STATUS_FULL BIT(5)
  111. #define BAT_STATUS_DISCHARGING BIT(6)
  112. #define BAT_STATUS_OVER_TEMPERATURE BIT(12)
  113. #define BAT_STATUS_OVER_CHARGED BIT(15)
  114. #define ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN (-2731)
  115. #define BQ_TERMINATION_CURRENT_MA 200
  116. #define BQ_MAX_STR_LEN 32
  117. struct bq28400_device {
  118. struct i2c_client *client;
  119. struct delayed_work periodic_user_space_update_work;
  120. struct dentry *dent;
  121. struct power_supply batt_psy;
  122. struct power_supply *dc_psy;
  123. bool is_charging_enabled;
  124. u32 temp_cold; /* in degree celsius */
  125. u32 temp_hot; /* in degree celsius */
  126. };
  127. static struct bq28400_device *bq28400_dev;
  128. static enum power_supply_property pm_power_props[] = {
  129. POWER_SUPPLY_PROP_STATUS,
  130. POWER_SUPPLY_PROP_CHARGE_TYPE,
  131. POWER_SUPPLY_PROP_PRESENT,
  132. POWER_SUPPLY_PROP_TECHNOLOGY,
  133. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  134. POWER_SUPPLY_PROP_CAPACITY,
  135. POWER_SUPPLY_PROP_CURRENT_NOW,
  136. POWER_SUPPLY_PROP_CURRENT_AVG,
  137. POWER_SUPPLY_PROP_TEMP,
  138. POWER_SUPPLY_PROP_CHARGE_FULL,
  139. POWER_SUPPLY_PROP_CHARGE_NOW,
  140. POWER_SUPPLY_PROP_MODEL_NAME,
  141. POWER_SUPPLY_PROP_MANUFACTURER,
  142. };
  143. struct debug_reg {
  144. char *name;
  145. u8 reg;
  146. u16 subcmd;
  147. };
  148. static int fake_battery = -EINVAL;
  149. module_param(fake_battery, int, 0644);
  150. #define BQ28400_DEBUG_REG(x) {#x, SBS_##x, 0}
  151. #define BQ28400_DEBUG_SUBREG(x, y) {#y, SBS_##x, SUBCMD_##y}
  152. /* Note: Some register can be read only in Unsealed mode */
  153. static struct debug_reg bq28400_debug_regs[] = {
  154. BQ28400_DEBUG_REG(MANUFACTURER_ACCESS),
  155. BQ28400_DEBUG_REG(BATTERY_MODE),
  156. BQ28400_DEBUG_REG(TEMPERATURE),
  157. BQ28400_DEBUG_REG(VOLTAGE),
  158. BQ28400_DEBUG_REG(CURRENT),
  159. BQ28400_DEBUG_REG(AVG_CURRENT),
  160. BQ28400_DEBUG_REG(MAX_ERROR),
  161. BQ28400_DEBUG_REG(RSOC),
  162. BQ28400_DEBUG_REG(REMAIN_CAPACITY),
  163. BQ28400_DEBUG_REG(FULL_CAPACITY),
  164. BQ28400_DEBUG_REG(CHG_CURRENT),
  165. BQ28400_DEBUG_REG(CHG_VOLTAGE),
  166. BQ28400_DEBUG_REG(BATTERY_STATUS),
  167. BQ28400_DEBUG_REG(CYCLE_COUNT),
  168. BQ28400_DEBUG_REG(DESIGN_CAPACITY),
  169. BQ28400_DEBUG_REG(DESIGN_VOLTAGE),
  170. BQ28400_DEBUG_REG(SPEC_INFO),
  171. BQ28400_DEBUG_REG(MANUFACTURE_DATE),
  172. BQ28400_DEBUG_REG(SERIAL_NUMBER),
  173. BQ28400_DEBUG_REG(MANUFACTURER_NAME),
  174. BQ28400_DEBUG_REG(DEVICE_NAME),
  175. BQ28400_DEBUG_REG(DEVICE_CHEMISTRY),
  176. BQ28400_DEBUG_REG(MANUFACTURER_DATA),
  177. BQ28400_DEBUG_REG(AUTHENTICATE),
  178. BQ28400_DEBUG_REG(CELL_VOLTAGE1),
  179. BQ28400_DEBUG_REG(CELL_VOLTAGE2),
  180. BQ28400_DEBUG_REG(SAFETY_ALERT),
  181. BQ28400_DEBUG_REG(SAFETY_STATUS),
  182. BQ28400_DEBUG_REG(PE_ALERT),
  183. BQ28400_DEBUG_REG(PE_STATUS),
  184. BQ28400_DEBUG_REG(OPERATION_STATUS),
  185. BQ28400_DEBUG_REG(CHARGING_STATUS),
  186. BQ28400_DEBUG_REG(FET_STATUS),
  187. BQ28400_DEBUG_REG(FULL_ACCESS_KEY),
  188. BQ28400_DEBUG_REG(PF_KEY),
  189. BQ28400_DEBUG_REG(MANUFACTURER_INFO),
  190. BQ28400_DEBUG_REG(SENSE_RESISTOR),
  191. BQ28400_DEBUG_REG(TEMP_RANGE),
  192. BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, DEVICE_TYPE),
  193. BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, FIRMWARE_VERSION),
  194. BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, HARDWARE_VERSION),
  195. BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, DF_CHECKSUM),
  196. BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, EDV),
  197. BQ28400_DEBUG_SUBREG(MANUFACTURER_ACCESS, CHEMISTRY_ID),
  198. };
  199. static int bq28400_read_reg(struct i2c_client *client, u8 reg)
  200. {
  201. int val;
  202. val = i2c_smbus_read_word_data(client, reg);
  203. if (val < 0)
  204. pr_err("i2c read fail. reg = 0x%x.ret = %d.\n", reg, val);
  205. else
  206. pr_debug("reg = 0x%02X.val = 0x%04X.\n", reg , val);
  207. return val;
  208. }
  209. static int bq28400_write_reg(struct i2c_client *client, u8 reg, u16 val)
  210. {
  211. int ret;
  212. ret = i2c_smbus_write_word_data(client, reg, val);
  213. if (ret < 0)
  214. pr_err("i2c read fail. reg = 0x%x.val = 0x%x.ret = %d.\n",
  215. reg, val, ret);
  216. else
  217. pr_debug("reg = 0x%02X.val = 0x%02X.\n", reg , val);
  218. return ret;
  219. }
  220. static int bq28400_read_subcmd(struct i2c_client *client, u8 reg, u16 subcmd)
  221. {
  222. int ret;
  223. u8 buf[4];
  224. u16 val = 0;
  225. buf[0] = reg;
  226. buf[1] = subcmd & 0xFF;
  227. buf[2] = (subcmd >> 8) & 0xFF;
  228. /* Control sub-command */
  229. ret = i2c_master_send(client, buf, 3);
  230. if (ret < 0) {
  231. pr_err("i2c tx fail. reg = 0x%x.ret = %d.\n", reg, ret);
  232. return ret;
  233. }
  234. udelay(66);
  235. /* Read Result of subcmd */
  236. ret = i2c_master_send(client, buf, 1);
  237. memset(buf, 0xAA, sizeof(buf));
  238. ret = i2c_master_recv(client, buf, 2);
  239. if (ret < 0) {
  240. pr_err("i2c rx fail. reg = 0x%x.ret = %d.\n", reg, ret);
  241. return ret;
  242. }
  243. val = (buf[1] << 8) + buf[0];
  244. pr_debug("reg = 0x%02X.subcmd = 0x%x.val = 0x%04X.\n",
  245. reg , subcmd, val);
  246. return val;
  247. }
  248. static int bq28400_read_block(struct i2c_client *client, u8 reg,
  249. u8 len, u8 *buf)
  250. {
  251. int ret;
  252. u32 val;
  253. ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
  254. val = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
  255. if (ret < 0)
  256. pr_err("i2c read fail. reg = 0x%x.ret = %d.\n", reg, ret);
  257. else
  258. pr_debug("reg = 0x%02X.val = 0x%04X.\n", reg , val);
  259. return val;
  260. }
  261. /*
  262. * Read a string from a device.
  263. * Returns string length on success or error on failure (negative value).
  264. */
  265. static int bq28400_read_string(struct i2c_client *client, u8 reg, char *str,
  266. u8 max_len)
  267. {
  268. int ret;
  269. int len;
  270. ret = bq28400_read_block(client, reg, max_len, str);
  271. if (ret < 0)
  272. return ret;
  273. len = str[0]; /* Actual length */
  274. if (len > max_len - 2) { /* reduce len byte and null */
  275. pr_err("len = %d invalid.\n", len);
  276. return -EINVAL;
  277. }
  278. memcpy(&str[0], &str[1], len); /* Move sting to the start */
  279. str[len] = 0; /* put NULL after actual size */
  280. pr_debug("len = %d.str = %s.\n", len, str);
  281. return len;
  282. }
  283. #define BQ28400_INVALID_TEMPERATURE -999
  284. /*
  285. * Return the battery temperature in tenths of degree Celsius
  286. * Or -99.9 C if something fails.
  287. */
  288. static int bq28400_read_temperature(struct i2c_client *client)
  289. {
  290. int temp;
  291. /* temperature resolution 0.1 Kelvin */
  292. temp = bq28400_read_reg(client, SBS_TEMPERATURE);
  293. if (temp < 0)
  294. return BQ28400_INVALID_TEMPERATURE;
  295. temp = temp + ZERO_DEGREE_CELSIUS_IN_TENTH_KELVIN;
  296. pr_debug("temp = %d C\n", temp/10);
  297. return temp;
  298. }
  299. /*
  300. * Return the battery Voltage in milivolts 0..20 V
  301. * Or < 0 if something fails.
  302. */
  303. static int bq28400_read_voltage(struct i2c_client *client)
  304. {
  305. int mvolt = 0;
  306. mvolt = bq28400_read_reg(client, SBS_VOLTAGE);
  307. if (mvolt < 0)
  308. return mvolt;
  309. pr_debug("volt = %d mV.\n", mvolt);
  310. return mvolt;
  311. }
  312. /*
  313. * Return the battery Current in miliamps
  314. * Or 0 if something fails.
  315. * Positive current indicates charging
  316. * Negative current indicates discharging.
  317. * Current-now is calculated every second.
  318. */
  319. static int bq28400_read_current(struct i2c_client *client)
  320. {
  321. s16 current_ma = 0;
  322. current_ma = bq28400_read_reg(client, SBS_CURRENT);
  323. pr_debug("current = %d mA.\n", current_ma);
  324. return current_ma;
  325. }
  326. /*
  327. * Return the Average battery Current in miliamps
  328. * Or 0 if something fails.
  329. * Positive current indicates charging
  330. * Negative current indicates discharging.
  331. * Average Current is the rolling 1 minute average current.
  332. */
  333. static int bq28400_read_avg_current(struct i2c_client *client)
  334. {
  335. s16 current_ma = 0;
  336. current_ma = bq28400_read_reg(client, SBS_AVG_CURRENT);
  337. pr_debug("avg_current=%d mA.\n", current_ma);
  338. return current_ma;
  339. }
  340. /*
  341. * Return the battery Relative-State-Of-Charge 0..100 %
  342. * Or negative value if something fails.
  343. */
  344. static int bq28400_read_rsoc(struct i2c_client *client)
  345. {
  346. int percentage = 0;
  347. if (fake_battery != -EINVAL) {
  348. pr_debug("Reporting Fake SOC = %d\n", fake_battery);
  349. return fake_battery;
  350. }
  351. /* This register is only 1 byte */
  352. percentage = i2c_smbus_read_byte_data(client, SBS_RSOC);
  353. if (percentage < 0) {
  354. pr_err("I2C failure when reading rsoc.\n");
  355. return percentage;
  356. }
  357. pr_debug("percentage = %d.\n", percentage);
  358. return percentage;
  359. }
  360. /*
  361. * Return the battery Capacity in mAh.
  362. * Or 0 if something fails.
  363. */
  364. static int bq28400_read_full_capacity(struct i2c_client *client)
  365. {
  366. int capacity = 0;
  367. capacity = bq28400_read_reg(client, SBS_FULL_CAPACITY);
  368. if (capacity < 0)
  369. return 0;
  370. pr_debug("full-capacity = %d mAh.\n", capacity);
  371. return capacity;
  372. }
  373. /*
  374. * Return the battery Capacity in mAh.
  375. * Or 0 if something fails.
  376. */
  377. static int bq28400_read_remain_capacity(struct i2c_client *client)
  378. {
  379. int capacity = 0;
  380. capacity = bq28400_read_reg(client, SBS_REMAIN_CAPACITY);
  381. if (capacity < 0)
  382. return 0;
  383. pr_debug("remain-capacity = %d mAh.\n", capacity);
  384. return capacity;
  385. }
  386. static int bq28400_enable_charging(struct bq28400_device *bq28400_dev,
  387. bool enable)
  388. {
  389. int ret;
  390. static bool is_charging_enabled;
  391. if (bq28400_dev->dc_psy == NULL) {
  392. bq28400_dev->dc_psy = power_supply_get_by_name("dc");
  393. if (bq28400_dev->dc_psy == NULL) {
  394. pr_err("fail to get dc-psy.\n");
  395. return -ENODEV;
  396. }
  397. }
  398. if (is_charging_enabled == enable) {
  399. pr_debug("Charging enable already = %d.\n", enable);
  400. return 0;
  401. }
  402. ret = power_supply_set_online(bq28400_dev->dc_psy, enable);
  403. if (ret < 0) {
  404. pr_err("fail to set dc-psy online to %d.\n", enable);
  405. return ret;
  406. }
  407. is_charging_enabled = enable;
  408. pr_debug("Charging enable = %d.\n", enable);
  409. return 0;
  410. }
  411. static int bq28400_get_prop_status(struct i2c_client *client)
  412. {
  413. int status = POWER_SUPPLY_STATUS_UNKNOWN;
  414. int rsoc;
  415. s16 current_ma = 0;
  416. u16 battery_status;
  417. int temperature;
  418. struct bq28400_device *dev = i2c_get_clientdata(client);
  419. battery_status = bq28400_read_reg(client, SBS_BATTERY_STATUS);
  420. rsoc = bq28400_read_rsoc(client);
  421. current_ma = bq28400_read_current(client);
  422. temperature = bq28400_read_temperature(client);
  423. temperature = temperature / 10; /* in degree celsius */
  424. if (battery_status & BAT_STATUS_EMPTY)
  425. pr_debug("Battery report Empty.\n");
  426. /* Battery may report FULL before rsoc is 100%
  427. * for protection and cell-balancing.
  428. * The FULL report may remain when rsoc drops from 100%.
  429. * If battery is full but DC-Jack is removed then report discahrging.
  430. */
  431. if (battery_status & BAT_STATUS_FULL) {
  432. pr_debug("Battery report Full.\n");
  433. bq28400_enable_charging(bq28400_dev, false);
  434. if (current_ma < 0)
  435. return POWER_SUPPLY_STATUS_DISCHARGING;
  436. return POWER_SUPPLY_STATUS_FULL;
  437. }
  438. if (rsoc == 100) {
  439. bq28400_enable_charging(bq28400_dev, false);
  440. pr_debug("Full.\n");
  441. return POWER_SUPPLY_STATUS_FULL;
  442. }
  443. /* Enable charging when battery is not full and temperature is ok */
  444. if ((temperature > dev->temp_cold) && (temperature < dev->temp_hot))
  445. bq28400_enable_charging(bq28400_dev, true);
  446. else
  447. bq28400_enable_charging(bq28400_dev, false);
  448. /*
  449. * Positive current indicates charging
  450. * Negative current indicates discharging.
  451. * Charging is stopped at termination-current.
  452. */
  453. if (current_ma < 0) {
  454. pr_debug("Discharging.\n");
  455. status = POWER_SUPPLY_STATUS_DISCHARGING;
  456. } else if (current_ma > BQ_TERMINATION_CURRENT_MA) {
  457. pr_debug("Charging.\n");
  458. status = POWER_SUPPLY_STATUS_CHARGING;
  459. } else {
  460. pr_debug("Not Charging.\n");
  461. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  462. }
  463. return status;
  464. }
  465. static int bq28400_get_prop_charge_type(struct i2c_client *client)
  466. {
  467. u16 battery_status;
  468. u16 chg_status;
  469. u16 fet_status;
  470. battery_status = bq28400_read_reg(client, SBS_BATTERY_STATUS);
  471. chg_status = bq28400_read_reg(client, SBS_CHARGING_STATUS);
  472. fet_status = bq28400_read_reg(client, SBS_FET_STATUS);
  473. if (battery_status & BAT_STATUS_DISCHARGING) {
  474. pr_debug("Discharging.\n");
  475. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  476. }
  477. if (fet_status & FET_STATUS_PRECHARGE) {
  478. pr_debug("Pre-Charging.\n");
  479. return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  480. }
  481. if (chg_status & CHG_STATUS_HOT_TEMP_CHARGING) {
  482. pr_debug("Hot-Temp-Charging.\n");
  483. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  484. }
  485. if (chg_status & CHG_STATUS_LOW_TEMP_CHARGING) {
  486. pr_debug("Low-Temp-Charging.\n");
  487. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  488. }
  489. if (chg_status & CHG_STATUS_STD1_TEMP_CHARGING) {
  490. pr_debug("STD1-Temp-Charging.\n");
  491. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  492. }
  493. if (chg_status & CHG_STATUS_STD2_TEMP_CHARGING) {
  494. pr_debug("STD2-Temp-Charging.\n");
  495. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  496. }
  497. if (chg_status & CHG_STATUS_BATTERY_DEPLETED)
  498. pr_debug("battery_depleted.\n");
  499. if (chg_status & CHG_STATUS_CELL_BALANCING)
  500. pr_debug("cell_balancing.\n");
  501. if (chg_status & CHG_STATUS_OVERCHARGE) {
  502. pr_err("overcharge fault.\n");
  503. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  504. }
  505. if (chg_status & CHG_STATUS_SUSPENDED) {
  506. pr_info("Suspended.\n");
  507. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  508. }
  509. if (chg_status & CHG_STATUS_DISABLED) {
  510. pr_info("Disabled.\n");
  511. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  512. }
  513. return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  514. }
  515. static bool bq28400_get_prop_present(struct i2c_client *client)
  516. {
  517. int val;
  518. val = bq28400_read_reg(client, SBS_BATTERY_STATUS);
  519. /* If the bq28400 is inside the battery pack
  520. * then when battery is removed the i2c transfer will fail.
  521. */
  522. if (val < 0)
  523. return false;
  524. /* TODO - support when bq28400 is not embedded in battery pack */
  525. return true;
  526. }
  527. /*
  528. * User sapce read the battery info.
  529. * Get data online via I2C from the battery gauge.
  530. */
  531. static int bq28400_get_property(struct power_supply *psy,
  532. enum power_supply_property psp,
  533. union power_supply_propval *val)
  534. {
  535. int ret = 0;
  536. struct bq28400_device *dev = container_of(psy,
  537. struct bq28400_device,
  538. batt_psy);
  539. struct i2c_client *client = dev->client;
  540. static char str[BQ_MAX_STR_LEN];
  541. switch (psp) {
  542. case POWER_SUPPLY_PROP_STATUS:
  543. val->intval = bq28400_get_prop_status(client);
  544. break;
  545. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  546. val->intval = bq28400_get_prop_charge_type(client);
  547. break;
  548. case POWER_SUPPLY_PROP_PRESENT:
  549. val->intval = bq28400_get_prop_present(client);
  550. break;
  551. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  552. val->intval = bq28400_read_voltage(client);
  553. val->intval *= 1000; /* mV to uV */
  554. break;
  555. case POWER_SUPPLY_PROP_CAPACITY:
  556. val->intval = bq28400_read_rsoc(client);
  557. if (val->intval < 0)
  558. ret = -EINVAL;
  559. break;
  560. case POWER_SUPPLY_PROP_CURRENT_NOW:
  561. /* Positive current indicates drawing */
  562. val->intval = -bq28400_read_current(client);
  563. val->intval *= 1000; /* mA to uA */
  564. break;
  565. case POWER_SUPPLY_PROP_CURRENT_AVG:
  566. /* Positive current indicates drawing */
  567. val->intval = -bq28400_read_avg_current(client);
  568. val->intval *= 1000; /* mA to uA */
  569. break;
  570. case POWER_SUPPLY_PROP_TEMP:
  571. val->intval = bq28400_read_temperature(client);
  572. break;
  573. case POWER_SUPPLY_PROP_CHARGE_FULL:
  574. val->intval = bq28400_read_full_capacity(client);
  575. break;
  576. case POWER_SUPPLY_PROP_CHARGE_NOW:
  577. val->intval = bq28400_read_remain_capacity(client);
  578. break;
  579. case POWER_SUPPLY_PROP_TECHNOLOGY:
  580. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  581. break;
  582. case POWER_SUPPLY_PROP_MODEL_NAME:
  583. bq28400_read_string(client, SBS_DEVICE_NAME, str, 20);
  584. val->strval = str;
  585. break;
  586. case POWER_SUPPLY_PROP_MANUFACTURER:
  587. bq28400_read_string(client, SBS_MANUFACTURER_NAME, str, 20);
  588. val->strval = str;
  589. break;
  590. default:
  591. pr_err(" psp %d Not supoprted.\n", psp);
  592. ret = -EINVAL;
  593. break;
  594. }
  595. return ret;
  596. }
  597. static int bq28400_set_reg(void *data, u64 val)
  598. {
  599. struct debug_reg *dbg = data;
  600. u8 reg = dbg->reg;
  601. int ret;
  602. struct i2c_client *client = bq28400_dev->client;
  603. ret = bq28400_write_reg(client, reg, val);
  604. return ret;
  605. }
  606. static int bq28400_get_reg(void *data, u64 *val)
  607. {
  608. struct debug_reg *dbg = data;
  609. u8 reg = dbg->reg;
  610. u16 subcmd = dbg->subcmd;
  611. int ret;
  612. struct i2c_client *client = bq28400_dev->client;
  613. if (subcmd)
  614. ret = bq28400_read_subcmd(client, reg, subcmd);
  615. else
  616. ret = bq28400_read_reg(client, reg);
  617. if (ret < 0)
  618. return ret;
  619. *val = ret;
  620. return 0;
  621. }
  622. DEFINE_SIMPLE_ATTRIBUTE(reg_fops, bq28400_get_reg, bq28400_set_reg,
  623. "0x%04llx\n");
  624. static int bq28400_create_debugfs_entries(struct bq28400_device *bq28400_dev)
  625. {
  626. int i;
  627. bq28400_dev->dent = debugfs_create_dir(BQ28400_NAME, NULL);
  628. if (IS_ERR(bq28400_dev->dent)) {
  629. pr_err("bq28400 driver couldn't create debugfs dir\n");
  630. return -EFAULT;
  631. }
  632. for (i = 0 ; i < ARRAY_SIZE(bq28400_debug_regs) ; i++) {
  633. char *name = bq28400_debug_regs[i].name;
  634. struct dentry *file;
  635. void *data = &bq28400_debug_regs[i];
  636. file = debugfs_create_file(name, 0644, bq28400_dev->dent,
  637. data, &reg_fops);
  638. if (IS_ERR(file)) {
  639. pr_err("debugfs_create_file %s failed.\n", name);
  640. return -EFAULT;
  641. }
  642. }
  643. return 0;
  644. }
  645. static int bq28400_set_property(struct power_supply *psy,
  646. enum power_supply_property psp,
  647. const union power_supply_propval *val)
  648. {
  649. pr_debug("psp = %d.val = %d.\n", psp, val->intval);
  650. return -EINVAL;
  651. }
  652. static void bq28400_external_power_changed(struct power_supply *psy)
  653. {
  654. pr_debug("Notify power_supply_changed.\n");
  655. /* The battery gauge monitors the current and voltage every 1 second.
  656. * Therefore a delay from the time that the charger start/stop charging
  657. * until the battery gauge detects it.
  658. */
  659. msleep(1000);
  660. /* Update LEDs and notify uevents */
  661. power_supply_changed(&bq28400_dev->batt_psy);
  662. }
  663. static int __devinit bq28400_register_psy(struct bq28400_device *bq28400_dev)
  664. {
  665. int ret;
  666. bq28400_dev->batt_psy.name = "battery";
  667. bq28400_dev->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  668. bq28400_dev->batt_psy.num_supplicants = 0;
  669. bq28400_dev->batt_psy.properties = pm_power_props;
  670. bq28400_dev->batt_psy.num_properties = ARRAY_SIZE(pm_power_props);
  671. bq28400_dev->batt_psy.get_property = bq28400_get_property;
  672. bq28400_dev->batt_psy.set_property = bq28400_set_property;
  673. bq28400_dev->batt_psy.external_power_changed =
  674. bq28400_external_power_changed;
  675. ret = power_supply_register(&bq28400_dev->client->dev,
  676. &bq28400_dev->batt_psy);
  677. if (ret) {
  678. pr_err("failed to register power_supply. ret=%d.\n", ret);
  679. return ret;
  680. }
  681. return 0;
  682. }
  683. /**
  684. * Update userspace every 1 minute.
  685. * Normally it takes more than 120 minutes (two hours) to
  686. * charge/discahrge the battery,
  687. * so updating every 1 minute should be enough for 1% change
  688. * detection.
  689. * Any immidiate change detected by the DC charger is notified
  690. * by the bq28400_external_power_changed callback, which notify
  691. * the user space.
  692. */
  693. static void bq28400_periodic_user_space_update_worker(struct work_struct *work)
  694. {
  695. u32 delay_msec = 60*1000;
  696. pr_debug("Notify user space.\n");
  697. /* Notify user space via kobject_uevent change notification */
  698. power_supply_changed(&bq28400_dev->batt_psy);
  699. schedule_delayed_work(&bq28400_dev->periodic_user_space_update_work,
  700. round_jiffies_relative(msecs_to_jiffies
  701. (delay_msec)));
  702. }
  703. static int __devinit bq28400_probe(struct i2c_client *client,
  704. const struct i2c_device_id *id)
  705. {
  706. int ret = 0;
  707. struct device_node *dev_node = client->dev.of_node;
  708. if (dev_node == NULL) {
  709. pr_err("Device Tree node doesn't exist.\n");
  710. return -ENODEV;
  711. }
  712. if (!i2c_check_functionality(client->adapter,
  713. I2C_FUNC_SMBUS_BYTE_DATA)) {
  714. pr_err(" i2c func fail.\n");
  715. return -EIO;
  716. }
  717. if (bq28400_read_reg(client, SBS_BATTERY_STATUS) < 0) {
  718. pr_err("Device doesn't exist.\n");
  719. return -ENODEV;
  720. }
  721. bq28400_dev = kzalloc(sizeof(*bq28400_dev), GFP_KERNEL);
  722. if (!bq28400_dev) {
  723. pr_err(" alloc fail.\n");
  724. return -ENOMEM;
  725. }
  726. /* Note: Lithium-ion battery normal temperature range 0..40 C */
  727. ret = of_property_read_u32(dev_node, "ti,temp-cold",
  728. &(bq28400_dev->temp_cold));
  729. if (ret) {
  730. pr_err("Unable to read cold temperature. ret=%d.\n", ret);
  731. goto err_dev_node;
  732. }
  733. pr_debug("cold temperature limit = %d C.\n", bq28400_dev->temp_cold);
  734. ret = of_property_read_u32(dev_node, "ti,temp-hot",
  735. &(bq28400_dev->temp_hot));
  736. if (ret) {
  737. pr_err("Unable to read hot temperature. ret=%d.\n", ret);
  738. goto err_dev_node;
  739. }
  740. pr_debug("hot temperature limit = %d C.\n", bq28400_dev->temp_hot);
  741. bq28400_dev->client = client;
  742. i2c_set_clientdata(client, bq28400_dev);
  743. ret = bq28400_register_psy(bq28400_dev);
  744. if (ret) {
  745. pr_err(" bq28400_register_psy fail.\n");
  746. goto err_register_psy;
  747. }
  748. ret = bq28400_create_debugfs_entries(bq28400_dev);
  749. if (ret) {
  750. pr_err(" bq28400_create_debugfs_entries fail.\n");
  751. goto err_debugfs;
  752. }
  753. INIT_DELAYED_WORK(&bq28400_dev->periodic_user_space_update_work,
  754. bq28400_periodic_user_space_update_worker);
  755. schedule_delayed_work(&bq28400_dev->periodic_user_space_update_work,
  756. msecs_to_jiffies(1000));
  757. pr_debug("Device is ready.\n");
  758. return 0;
  759. err_debugfs:
  760. if (bq28400_dev->dent)
  761. debugfs_remove_recursive(bq28400_dev->dent);
  762. power_supply_unregister(&bq28400_dev->batt_psy);
  763. err_register_psy:
  764. err_dev_node:
  765. kfree(bq28400_dev);
  766. bq28400_dev = NULL;
  767. pr_info("FAIL.\n");
  768. return ret;
  769. }
  770. static int __devexit bq28400_remove(struct i2c_client *client)
  771. {
  772. struct bq28400_device *bq28400_dev = i2c_get_clientdata(client);
  773. power_supply_unregister(&bq28400_dev->batt_psy);
  774. if (bq28400_dev->dent)
  775. debugfs_remove_recursive(bq28400_dev->dent);
  776. kfree(bq28400_dev);
  777. bq28400_dev = NULL;
  778. return 0;
  779. }
  780. static const struct of_device_id bq28400_match[] = {
  781. { .compatible = "ti,bq28400-battery" },
  782. { .compatible = "ti,bq30z55-battery" },
  783. { },
  784. };
  785. static const struct i2c_device_id bq28400_id[] = {
  786. {BQ28400_NAME, 0},
  787. {},
  788. };
  789. MODULE_DEVICE_TABLE(i2c, bq28400_id);
  790. static struct i2c_driver bq28400_driver = {
  791. .driver = {
  792. .name = BQ28400_NAME,
  793. .owner = THIS_MODULE,
  794. .of_match_table = of_match_ptr(bq28400_match),
  795. },
  796. .probe = bq28400_probe,
  797. .remove = __devexit_p(bq28400_remove),
  798. .id_table = bq28400_id,
  799. };
  800. static int __init bq28400_init(void)
  801. {
  802. pr_info(" bq28400 driver rev %s.\n", BQ28400_REV);
  803. return i2c_add_driver(&bq28400_driver);
  804. }
  805. module_init(bq28400_init);
  806. static void __exit bq28400_exit(void)
  807. {
  808. return i2c_del_driver(&bq28400_driver);
  809. }
  810. module_exit(bq28400_exit);
  811. MODULE_DESCRIPTION("Driver for BQ28400 charger chip");
  812. MODULE_LICENSE("GPL v2");
  813. MODULE_ALIAS("i2c:" BQ28400_NAME);