ltc2941-battery-gauge.c 13 KB

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