ltc2941-battery-gauge.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * I2C client/driver for the Linear Technology LTC2941, LTC2942, LTC2943
  3. * and LTC2944 Battery Gas Gauge IC
  4. *
  5. * Copyright (C) 2014 Topic Embedded Systems
  6. *
  7. * Author: Auryn Verwegen
  8. * Author: Mike Looijmans
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/swab.h>
  16. #include <linux/i2c.h>
  17. #include <linux/delay.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/slab.h>
  20. #define I16_MSB(x) ((x >> 8) & 0xFF)
  21. #define I16_LSB(x) (x & 0xFF)
  22. #define LTC294X_WORK_DELAY 10 /* Update delay in seconds */
  23. #define LTC294X_MAX_VALUE 0xFFFF
  24. #define LTC294X_MID_SUPPLY 0x7FFF
  25. #define LTC2941_MAX_PRESCALER_EXP 7
  26. #define LTC2943_MAX_PRESCALER_EXP 6
  27. enum ltc294x_reg {
  28. LTC294X_REG_STATUS = 0x00,
  29. LTC294X_REG_CONTROL = 0x01,
  30. LTC294X_REG_ACC_CHARGE_MSB = 0x02,
  31. LTC294X_REG_ACC_CHARGE_LSB = 0x03,
  32. LTC294X_REG_VOLTAGE_MSB = 0x08,
  33. LTC294X_REG_VOLTAGE_LSB = 0x09,
  34. LTC2942_REG_TEMPERATURE_MSB = 0x0C,
  35. LTC2942_REG_TEMPERATURE_LSB = 0x0D,
  36. LTC2943_REG_CURRENT_MSB = 0x0E,
  37. LTC2943_REG_CURRENT_LSB = 0x0F,
  38. LTC2943_REG_TEMPERATURE_MSB = 0x14,
  39. LTC2943_REG_TEMPERATURE_LSB = 0x15,
  40. };
  41. enum ltc294x_id {
  42. LTC2941_ID,
  43. LTC2942_ID,
  44. LTC2943_ID,
  45. LTC2944_ID,
  46. };
  47. #define LTC2941_REG_STATUS_CHIP_ID BIT(7)
  48. #define LTC2942_REG_CONTROL_MODE_SCAN (BIT(7) | BIT(6))
  49. #define LTC2943_REG_CONTROL_MODE_SCAN BIT(7)
  50. #define LTC294X_REG_CONTROL_PRESCALER_MASK (BIT(5) | BIT(4) | BIT(3))
  51. #define LTC294X_REG_CONTROL_SHUTDOWN_MASK (BIT(0))
  52. #define LTC294X_REG_CONTROL_PRESCALER_SET(x) \
  53. ((x << 3) & LTC294X_REG_CONTROL_PRESCALER_MASK)
  54. #define LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED 0
  55. struct ltc294x_info {
  56. struct i2c_client *client; /* I2C Client pointer */
  57. struct power_supply *supply; /* Supply pointer */
  58. struct power_supply_desc supply_desc; /* Supply description */
  59. struct delayed_work work; /* Work scheduler */
  60. enum ltc294x_id id; /* Chip type */
  61. int charge; /* Last charge register content */
  62. int r_sense; /* mOhm */
  63. int Qlsb; /* nAh */
  64. };
  65. static inline int convert_bin_to_uAh(
  66. const struct ltc294x_info *info, int Q)
  67. {
  68. return ((Q * (info->Qlsb / 10))) / 100;
  69. }
  70. static inline int convert_uAh_to_bin(
  71. const struct ltc294x_info *info, int uAh)
  72. {
  73. int Q;
  74. Q = (uAh * 100) / (info->Qlsb/10);
  75. return (Q < LTC294X_MAX_VALUE) ? Q : LTC294X_MAX_VALUE;
  76. }
  77. static int ltc294x_read_regs(struct i2c_client *client,
  78. enum ltc294x_reg reg, u8 *buf, int num_regs)
  79. {
  80. int ret;
  81. struct i2c_msg msgs[2] = { };
  82. u8 reg_start = reg;
  83. msgs[0].addr = client->addr;
  84. msgs[0].len = 1;
  85. msgs[0].buf = &reg_start;
  86. msgs[1].addr = client->addr;
  87. msgs[1].len = num_regs;
  88. msgs[1].buf = buf;
  89. msgs[1].flags = I2C_M_RD;
  90. ret = i2c_transfer(client->adapter, &msgs[0], 2);
  91. if (ret < 0) {
  92. dev_err(&client->dev, "ltc2941 read_reg failed!\n");
  93. return ret;
  94. }
  95. dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
  96. __func__, reg, num_regs, *buf);
  97. return 0;
  98. }
  99. static int ltc294x_write_regs(struct i2c_client *client,
  100. enum ltc294x_reg reg, const u8 *buf, int num_regs)
  101. {
  102. int ret;
  103. u8 reg_start = reg;
  104. ret = i2c_smbus_write_i2c_block_data(client, reg_start, num_regs, buf);
  105. if (ret < 0) {
  106. dev_err(&client->dev, "ltc2941 write_reg failed!\n");
  107. return ret;
  108. }
  109. dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
  110. __func__, reg, num_regs, *buf);
  111. return 0;
  112. }
  113. static int ltc294x_reset(const struct ltc294x_info *info, int prescaler_exp)
  114. {
  115. int ret;
  116. u8 value;
  117. u8 control;
  118. /* Read status and control registers */
  119. ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1);
  120. if (ret < 0) {
  121. dev_err(&info->client->dev,
  122. "Could not read registers from device\n");
  123. goto error_exit;
  124. }
  125. control = LTC294X_REG_CONTROL_PRESCALER_SET(prescaler_exp) |
  126. LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED;
  127. /* Put device into "monitor" mode */
  128. switch (info->id) {
  129. case LTC2942_ID: /* 2942 measures every 2 sec */
  130. control |= LTC2942_REG_CONTROL_MODE_SCAN;
  131. break;
  132. case LTC2943_ID:
  133. case LTC2944_ID: /* 2943 and 2944 measure every 10 sec */
  134. control |= LTC2943_REG_CONTROL_MODE_SCAN;
  135. break;
  136. default:
  137. break;
  138. }
  139. if (value != control) {
  140. ret = ltc294x_write_regs(info->client,
  141. LTC294X_REG_CONTROL, &control, 1);
  142. if (ret < 0) {
  143. dev_err(&info->client->dev,
  144. "Could not write register\n");
  145. goto error_exit;
  146. }
  147. }
  148. return 0;
  149. error_exit:
  150. return ret;
  151. }
  152. static int ltc294x_read_charge_register(const struct ltc294x_info *info)
  153. {
  154. int ret;
  155. u8 datar[2];
  156. ret = ltc294x_read_regs(info->client,
  157. LTC294X_REG_ACC_CHARGE_MSB, &datar[0], 2);
  158. if (ret < 0)
  159. return ret;
  160. return (datar[0] << 8) + datar[1];
  161. }
  162. static int ltc294x_get_charge_now(const struct ltc294x_info *info, int *val)
  163. {
  164. int value = ltc294x_read_charge_register(info);
  165. if (value < 0)
  166. return value;
  167. /* When r_sense < 0, this counts up when the battery discharges */
  168. if (info->Qlsb < 0)
  169. value -= 0xFFFF;
  170. *val = convert_bin_to_uAh(info, value);
  171. return 0;
  172. }
  173. static int ltc294x_set_charge_now(const struct ltc294x_info *info, int val)
  174. {
  175. int ret;
  176. u8 dataw[2];
  177. u8 ctrl_reg;
  178. s32 value;
  179. value = convert_uAh_to_bin(info, val);
  180. /* Direction depends on how sense+/- were connected */
  181. if (info->Qlsb < 0)
  182. value += 0xFFFF;
  183. if ((value < 0) || (value > 0xFFFF)) /* input validation */
  184. return -EINVAL;
  185. /* Read control register */
  186. ret = ltc294x_read_regs(info->client,
  187. LTC294X_REG_CONTROL, &ctrl_reg, 1);
  188. if (ret < 0)
  189. return ret;
  190. /* Disable analog section */
  191. ctrl_reg |= LTC294X_REG_CONTROL_SHUTDOWN_MASK;
  192. ret = ltc294x_write_regs(info->client,
  193. LTC294X_REG_CONTROL, &ctrl_reg, 1);
  194. if (ret < 0)
  195. return ret;
  196. /* Set new charge value */
  197. dataw[0] = I16_MSB(value);
  198. dataw[1] = I16_LSB(value);
  199. ret = ltc294x_write_regs(info->client,
  200. LTC294X_REG_ACC_CHARGE_MSB, &dataw[0], 2);
  201. if (ret < 0)
  202. goto error_exit;
  203. /* Enable analog section */
  204. error_exit:
  205. ctrl_reg &= ~LTC294X_REG_CONTROL_SHUTDOWN_MASK;
  206. ret = ltc294x_write_regs(info->client,
  207. LTC294X_REG_CONTROL, &ctrl_reg, 1);
  208. return ret < 0 ? ret : 0;
  209. }
  210. static int ltc294x_get_charge_counter(
  211. const struct ltc294x_info *info, int *val)
  212. {
  213. int value = ltc294x_read_charge_register(info);
  214. if (value < 0)
  215. return value;
  216. value -= LTC294X_MID_SUPPLY;
  217. *val = convert_bin_to_uAh(info, value);
  218. return 0;
  219. }
  220. static int ltc294x_get_voltage(const struct ltc294x_info *info, int *val)
  221. {
  222. int ret;
  223. u8 datar[2];
  224. u32 value;
  225. ret = ltc294x_read_regs(info->client,
  226. LTC294X_REG_VOLTAGE_MSB, &datar[0], 2);
  227. value = (datar[0] << 8) | datar[1];
  228. switch (info->id) {
  229. case LTC2943_ID:
  230. value *= 23600 * 2;
  231. value /= 0xFFFF;
  232. value *= 1000 / 2;
  233. break;
  234. case LTC2944_ID:
  235. value *= 70800 / 5*4;
  236. value /= 0xFFFF;
  237. value *= 1000 * 5/4;
  238. break;
  239. default:
  240. value *= 6000 * 10;
  241. value /= 0xFFFF;
  242. value *= 1000 / 10;
  243. break;
  244. }
  245. *val = value;
  246. return ret;
  247. }
  248. static int ltc294x_get_current(const struct ltc294x_info *info, int *val)
  249. {
  250. int ret;
  251. u8 datar[2];
  252. s32 value;
  253. ret = ltc294x_read_regs(info->client,
  254. LTC2943_REG_CURRENT_MSB, &datar[0], 2);
  255. value = (datar[0] << 8) | datar[1];
  256. value -= 0x7FFF;
  257. if (info->id == LTC2944_ID)
  258. value *= 64000;
  259. else
  260. value *= 60000;
  261. /* Value is in range -32k..+32k, r_sense is usually 10..50 mOhm,
  262. * the formula below keeps everything in s32 range while preserving
  263. * enough digits */
  264. *val = 1000 * (value / (info->r_sense * 0x7FFF)); /* in uA */
  265. return ret;
  266. }
  267. static int ltc294x_get_temperature(const struct ltc294x_info *info, int *val)
  268. {
  269. enum ltc294x_reg reg;
  270. int ret;
  271. u8 datar[2];
  272. u32 value;
  273. if (info->id == LTC2942_ID) {
  274. reg = LTC2942_REG_TEMPERATURE_MSB;
  275. value = 6000; /* Full-scale is 600 Kelvin */
  276. } else {
  277. reg = LTC2943_REG_TEMPERATURE_MSB;
  278. value = 5100; /* Full-scale is 510 Kelvin */
  279. }
  280. ret = ltc294x_read_regs(info->client, reg, &datar[0], 2);
  281. value *= (datar[0] << 8) | datar[1];
  282. /* Convert to tenths of degree Celsius */
  283. *val = value / 0xFFFF - 2722;
  284. return ret;
  285. }
  286. static int ltc294x_get_property(struct power_supply *psy,
  287. enum power_supply_property prop,
  288. union power_supply_propval *val)
  289. {
  290. struct ltc294x_info *info = power_supply_get_drvdata(psy);
  291. switch (prop) {
  292. case POWER_SUPPLY_PROP_CHARGE_NOW:
  293. return ltc294x_get_charge_now(info, &val->intval);
  294. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  295. return ltc294x_get_charge_counter(info, &val->intval);
  296. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  297. return ltc294x_get_voltage(info, &val->intval);
  298. case POWER_SUPPLY_PROP_CURRENT_NOW:
  299. return ltc294x_get_current(info, &val->intval);
  300. case POWER_SUPPLY_PROP_TEMP:
  301. return ltc294x_get_temperature(info, &val->intval);
  302. default:
  303. return -EINVAL;
  304. }
  305. }
  306. static int ltc294x_set_property(struct power_supply *psy,
  307. enum power_supply_property psp,
  308. const union power_supply_propval *val)
  309. {
  310. struct ltc294x_info *info = power_supply_get_drvdata(psy);
  311. switch (psp) {
  312. case POWER_SUPPLY_PROP_CHARGE_NOW:
  313. return ltc294x_set_charge_now(info, val->intval);
  314. default:
  315. return -EPERM;
  316. }
  317. }
  318. static int ltc294x_property_is_writeable(
  319. struct power_supply *psy, enum power_supply_property psp)
  320. {
  321. switch (psp) {
  322. case POWER_SUPPLY_PROP_CHARGE_NOW:
  323. return 1;
  324. default:
  325. return 0;
  326. }
  327. }
  328. static void ltc294x_update(struct ltc294x_info *info)
  329. {
  330. int charge = ltc294x_read_charge_register(info);
  331. if (charge != info->charge) {
  332. info->charge = charge;
  333. power_supply_changed(info->supply);
  334. }
  335. }
  336. static void ltc294x_work(struct work_struct *work)
  337. {
  338. struct ltc294x_info *info;
  339. info = container_of(work, struct ltc294x_info, work.work);
  340. ltc294x_update(info);
  341. schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
  342. }
  343. static enum power_supply_property ltc294x_properties[] = {
  344. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  345. POWER_SUPPLY_PROP_CHARGE_NOW,
  346. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  347. POWER_SUPPLY_PROP_TEMP,
  348. POWER_SUPPLY_PROP_CURRENT_NOW,
  349. };
  350. static int ltc294x_i2c_remove(struct i2c_client *client)
  351. {
  352. struct ltc294x_info *info = i2c_get_clientdata(client);
  353. cancel_delayed_work_sync(&info->work);
  354. power_supply_unregister(info->supply);
  355. return 0;
  356. }
  357. static int ltc294x_i2c_probe(struct i2c_client *client,
  358. const struct i2c_device_id *id)
  359. {
  360. struct power_supply_config psy_cfg = {};
  361. struct ltc294x_info *info;
  362. struct device_node *np;
  363. int ret;
  364. u32 prescaler_exp;
  365. s32 r_sense;
  366. u8 status;
  367. info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
  368. if (info == NULL)
  369. return -ENOMEM;
  370. i2c_set_clientdata(client, info);
  371. np = of_node_get(client->dev.of_node);
  372. info->id = (enum ltc294x_id)of_device_get_match_data(&client->dev);
  373. info->supply_desc.name = np->name;
  374. /* r_sense can be negative, when sense+ is connected to the battery
  375. * instead of the sense-. This results in reversed measurements. */
  376. ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense);
  377. if (ret < 0) {
  378. dev_err(&client->dev,
  379. "Could not find lltc,resistor-sense in devicetree\n");
  380. return ret;
  381. }
  382. info->r_sense = r_sense;
  383. ret = of_property_read_u32(np, "lltc,prescaler-exponent",
  384. &prescaler_exp);
  385. if (ret < 0) {
  386. dev_warn(&client->dev,
  387. "lltc,prescaler-exponent not in devicetree\n");
  388. prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
  389. }
  390. if (info->id == LTC2943_ID) {
  391. if (prescaler_exp > LTC2943_MAX_PRESCALER_EXP)
  392. prescaler_exp = LTC2943_MAX_PRESCALER_EXP;
  393. info->Qlsb = ((340 * 50000) / r_sense) /
  394. (4096 / (1 << (2*prescaler_exp)));
  395. } else {
  396. if (prescaler_exp > LTC2941_MAX_PRESCALER_EXP)
  397. prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
  398. info->Qlsb = ((85 * 50000) / r_sense) /
  399. (128 / (1 << prescaler_exp));
  400. }
  401. /* Read status register to check for LTC2942 */
  402. if (info->id == LTC2941_ID || info->id == LTC2942_ID) {
  403. ret = ltc294x_read_regs(client, LTC294X_REG_STATUS, &status, 1);
  404. if (ret < 0) {
  405. dev_err(&client->dev,
  406. "Could not read status register\n");
  407. return ret;
  408. }
  409. if (status & LTC2941_REG_STATUS_CHIP_ID)
  410. info->id = LTC2941_ID;
  411. else
  412. info->id = LTC2942_ID;
  413. }
  414. info->client = client;
  415. info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  416. info->supply_desc.properties = ltc294x_properties;
  417. switch (info->id) {
  418. case LTC2944_ID:
  419. case LTC2943_ID:
  420. info->supply_desc.num_properties =
  421. ARRAY_SIZE(ltc294x_properties);
  422. break;
  423. case LTC2942_ID:
  424. info->supply_desc.num_properties =
  425. ARRAY_SIZE(ltc294x_properties) - 1;
  426. break;
  427. case LTC2941_ID:
  428. default:
  429. info->supply_desc.num_properties =
  430. ARRAY_SIZE(ltc294x_properties) - 3;
  431. break;
  432. }
  433. info->supply_desc.get_property = ltc294x_get_property;
  434. info->supply_desc.set_property = ltc294x_set_property;
  435. info->supply_desc.property_is_writeable = ltc294x_property_is_writeable;
  436. info->supply_desc.external_power_changed = NULL;
  437. psy_cfg.drv_data = info;
  438. INIT_DELAYED_WORK(&info->work, ltc294x_work);
  439. ret = ltc294x_reset(info, prescaler_exp);
  440. if (ret < 0) {
  441. dev_err(&client->dev, "Communication with chip failed\n");
  442. return ret;
  443. }
  444. info->supply = power_supply_register(&client->dev, &info->supply_desc,
  445. &psy_cfg);
  446. if (IS_ERR(info->supply)) {
  447. dev_err(&client->dev, "failed to register ltc2941\n");
  448. return PTR_ERR(info->supply);
  449. } else {
  450. schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
  451. }
  452. return 0;
  453. }
  454. #ifdef CONFIG_PM_SLEEP
  455. static int ltc294x_suspend(struct device *dev)
  456. {
  457. struct i2c_client *client = to_i2c_client(dev);
  458. struct ltc294x_info *info = i2c_get_clientdata(client);
  459. cancel_delayed_work(&info->work);
  460. return 0;
  461. }
  462. static int ltc294x_resume(struct device *dev)
  463. {
  464. struct i2c_client *client = to_i2c_client(dev);
  465. struct ltc294x_info *info = i2c_get_clientdata(client);
  466. schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
  467. return 0;
  468. }
  469. static SIMPLE_DEV_PM_OPS(ltc294x_pm_ops, ltc294x_suspend, ltc294x_resume);
  470. #define LTC294X_PM_OPS (&ltc294x_pm_ops)
  471. #else
  472. #define LTC294X_PM_OPS NULL
  473. #endif /* CONFIG_PM_SLEEP */
  474. static const struct i2c_device_id ltc294x_i2c_id[] = {
  475. { "ltc2941", LTC2941_ID, },
  476. { "ltc2942", LTC2942_ID, },
  477. { "ltc2943", LTC2943_ID, },
  478. { "ltc2944", LTC2944_ID, },
  479. { },
  480. };
  481. MODULE_DEVICE_TABLE(i2c, ltc294x_i2c_id);
  482. static const struct of_device_id ltc294x_i2c_of_match[] = {
  483. {
  484. .compatible = "lltc,ltc2941",
  485. .data = (void *)LTC2941_ID,
  486. },
  487. {
  488. .compatible = "lltc,ltc2942",
  489. .data = (void *)LTC2942_ID,
  490. },
  491. {
  492. .compatible = "lltc,ltc2943",
  493. .data = (void *)LTC2943_ID,
  494. },
  495. {
  496. .compatible = "lltc,ltc2944",
  497. .data = (void *)LTC2944_ID,
  498. },
  499. { },
  500. };
  501. MODULE_DEVICE_TABLE(of, ltc294x_i2c_of_match);
  502. static struct i2c_driver ltc294x_driver = {
  503. .driver = {
  504. .name = "LTC2941",
  505. .of_match_table = ltc294x_i2c_of_match,
  506. .pm = LTC294X_PM_OPS,
  507. },
  508. .probe = ltc294x_i2c_probe,
  509. .remove = ltc294x_i2c_remove,
  510. .id_table = ltc294x_i2c_id,
  511. };
  512. module_i2c_driver(ltc294x_driver);
  513. MODULE_AUTHOR("Auryn Verwegen, Topic Embedded Systems");
  514. MODULE_AUTHOR("Mike Looijmans, Topic Embedded Products");
  515. MODULE_DESCRIPTION("LTC2941/LTC2942/LTC2943/LTC2944 Battery Gas Gauge IC driver");
  516. MODULE_LICENSE("GPL");