max17040_battery.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // max17040_battery.c
  4. // fuel-gauge systems for lithium-ion (Li+) batteries
  5. //
  6. // Copyright (C) 2009 Samsung Electronics
  7. // Minkyu Kang <[email protected]>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/mutex.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #define MAX17040_VCELL 0x02
  21. #define MAX17040_SOC 0x04
  22. #define MAX17040_MODE 0x06
  23. #define MAX17040_VER 0x08
  24. #define MAX17040_CONFIG 0x0C
  25. #define MAX17040_STATUS 0x1A
  26. #define MAX17040_CMD 0xFE
  27. #define MAX17040_DELAY 1000
  28. #define MAX17040_BATTERY_FULL 95
  29. #define MAX17040_RCOMP_DEFAULT 0x9700
  30. #define MAX17040_ATHD_MASK 0x3f
  31. #define MAX17040_ALSC_MASK 0x40
  32. #define MAX17040_ATHD_DEFAULT_POWER_UP 4
  33. #define MAX17040_STATUS_HD_MASK 0x1000
  34. #define MAX17040_STATUS_SC_MASK 0x2000
  35. #define MAX17040_CFG_RCOMP_MASK 0xff00
  36. enum chip_id {
  37. ID_MAX17040,
  38. ID_MAX17041,
  39. ID_MAX17043,
  40. ID_MAX17044,
  41. ID_MAX17048,
  42. ID_MAX17049,
  43. ID_MAX17058,
  44. ID_MAX17059,
  45. };
  46. /* values that differ by chip_id */
  47. struct chip_data {
  48. u16 reset_val;
  49. u16 vcell_shift;
  50. u16 vcell_mul;
  51. u16 vcell_div;
  52. u8 has_low_soc_alert;
  53. u8 rcomp_bytes;
  54. u8 has_soc_alert;
  55. };
  56. static struct chip_data max17040_family[] = {
  57. [ID_MAX17040] = {
  58. .reset_val = 0x0054,
  59. .vcell_shift = 4,
  60. .vcell_mul = 1250,
  61. .vcell_div = 1,
  62. .has_low_soc_alert = 0,
  63. .rcomp_bytes = 2,
  64. .has_soc_alert = 0,
  65. },
  66. [ID_MAX17041] = {
  67. .reset_val = 0x0054,
  68. .vcell_shift = 4,
  69. .vcell_mul = 2500,
  70. .vcell_div = 1,
  71. .has_low_soc_alert = 0,
  72. .rcomp_bytes = 2,
  73. .has_soc_alert = 0,
  74. },
  75. [ID_MAX17043] = {
  76. .reset_val = 0x0054,
  77. .vcell_shift = 4,
  78. .vcell_mul = 1250,
  79. .vcell_div = 1,
  80. .has_low_soc_alert = 1,
  81. .rcomp_bytes = 1,
  82. .has_soc_alert = 0,
  83. },
  84. [ID_MAX17044] = {
  85. .reset_val = 0x0054,
  86. .vcell_shift = 4,
  87. .vcell_mul = 2500,
  88. .vcell_div = 1,
  89. .has_low_soc_alert = 1,
  90. .rcomp_bytes = 1,
  91. .has_soc_alert = 0,
  92. },
  93. [ID_MAX17048] = {
  94. .reset_val = 0x5400,
  95. .vcell_shift = 0,
  96. .vcell_mul = 625,
  97. .vcell_div = 8,
  98. .has_low_soc_alert = 1,
  99. .rcomp_bytes = 1,
  100. .has_soc_alert = 1,
  101. },
  102. [ID_MAX17049] = {
  103. .reset_val = 0x5400,
  104. .vcell_shift = 0,
  105. .vcell_mul = 625,
  106. .vcell_div = 4,
  107. .has_low_soc_alert = 1,
  108. .rcomp_bytes = 1,
  109. .has_soc_alert = 1,
  110. },
  111. [ID_MAX17058] = {
  112. .reset_val = 0x5400,
  113. .vcell_shift = 0,
  114. .vcell_mul = 625,
  115. .vcell_div = 8,
  116. .has_low_soc_alert = 1,
  117. .rcomp_bytes = 1,
  118. .has_soc_alert = 0,
  119. },
  120. [ID_MAX17059] = {
  121. .reset_val = 0x5400,
  122. .vcell_shift = 0,
  123. .vcell_mul = 625,
  124. .vcell_div = 4,
  125. .has_low_soc_alert = 1,
  126. .rcomp_bytes = 1,
  127. .has_soc_alert = 0,
  128. },
  129. };
  130. struct max17040_chip {
  131. struct i2c_client *client;
  132. struct regmap *regmap;
  133. struct delayed_work work;
  134. struct power_supply *battery;
  135. struct chip_data data;
  136. /* battery capacity */
  137. int soc;
  138. /* Low alert threshold from 32% to 1% of the State of Charge */
  139. u32 low_soc_alert;
  140. /* some devices return twice the capacity */
  141. bool quirk_double_soc;
  142. /* higher 8 bits for 17043+, 16 bits for 17040,41 */
  143. u16 rcomp;
  144. };
  145. static int max17040_reset(struct max17040_chip *chip)
  146. {
  147. return regmap_write(chip->regmap, MAX17040_CMD, chip->data.reset_val);
  148. }
  149. static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level)
  150. {
  151. level = 32 - level * (chip->quirk_double_soc ? 2 : 1);
  152. return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
  153. MAX17040_ATHD_MASK, level);
  154. }
  155. static int max17040_set_soc_alert(struct max17040_chip *chip, bool enable)
  156. {
  157. return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
  158. MAX17040_ALSC_MASK, enable ? MAX17040_ALSC_MASK : 0);
  159. }
  160. static int max17040_set_rcomp(struct max17040_chip *chip, u16 rcomp)
  161. {
  162. u16 mask = chip->data.rcomp_bytes == 2 ?
  163. 0xffff : MAX17040_CFG_RCOMP_MASK;
  164. return regmap_update_bits(chip->regmap, MAX17040_CONFIG, mask, rcomp);
  165. }
  166. static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
  167. {
  168. struct chip_data *d = &chip->data;
  169. return (vcell >> d->vcell_shift) * d->vcell_mul / d->vcell_div;
  170. }
  171. static int max17040_get_vcell(struct max17040_chip *chip)
  172. {
  173. u32 vcell;
  174. regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
  175. return max17040_raw_vcell_to_uvolts(chip, vcell);
  176. }
  177. static int max17040_get_soc(struct max17040_chip *chip)
  178. {
  179. u32 soc;
  180. regmap_read(chip->regmap, MAX17040_SOC, &soc);
  181. return soc >> (chip->quirk_double_soc ? 9 : 8);
  182. }
  183. static int max17040_get_version(struct max17040_chip *chip)
  184. {
  185. int ret;
  186. u32 version;
  187. ret = regmap_read(chip->regmap, MAX17040_VER, &version);
  188. return ret ? ret : version;
  189. }
  190. static int max17040_get_online(struct max17040_chip *chip)
  191. {
  192. return 1;
  193. }
  194. static int max17040_get_of_data(struct max17040_chip *chip)
  195. {
  196. struct device *dev = &chip->client->dev;
  197. struct chip_data *data = &max17040_family[
  198. (uintptr_t) of_device_get_match_data(dev)];
  199. int rcomp_len;
  200. u8 rcomp[2];
  201. chip->quirk_double_soc = device_property_read_bool(dev,
  202. "maxim,double-soc");
  203. chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
  204. device_property_read_u32(dev,
  205. "maxim,alert-low-soc-level",
  206. &chip->low_soc_alert);
  207. if (chip->low_soc_alert <= 0 ||
  208. chip->low_soc_alert > (chip->quirk_double_soc ? 16 : 32)) {
  209. dev_err(dev, "maxim,alert-low-soc-level out of bounds\n");
  210. return -EINVAL;
  211. }
  212. rcomp_len = device_property_count_u8(dev, "maxim,rcomp");
  213. chip->rcomp = MAX17040_RCOMP_DEFAULT;
  214. if (rcomp_len == data->rcomp_bytes) {
  215. if (!device_property_read_u8_array(dev, "maxim,rcomp",
  216. rcomp, rcomp_len))
  217. chip->rcomp = rcomp_len == 2 ? rcomp[0] << 8 | rcomp[1] :
  218. rcomp[0] << 8;
  219. } else if (rcomp_len > 0) {
  220. dev_err(dev, "maxim,rcomp has incorrect length\n");
  221. return -EINVAL;
  222. }
  223. return 0;
  224. }
  225. static void max17040_check_changes(struct max17040_chip *chip)
  226. {
  227. chip->soc = max17040_get_soc(chip);
  228. }
  229. static void max17040_queue_work(struct max17040_chip *chip)
  230. {
  231. queue_delayed_work(system_power_efficient_wq, &chip->work,
  232. MAX17040_DELAY);
  233. }
  234. static void max17040_stop_work(void *data)
  235. {
  236. struct max17040_chip *chip = data;
  237. cancel_delayed_work_sync(&chip->work);
  238. }
  239. static void max17040_work(struct work_struct *work)
  240. {
  241. struct max17040_chip *chip;
  242. int last_soc;
  243. chip = container_of(work, struct max17040_chip, work.work);
  244. /* store SOC to check changes */
  245. last_soc = chip->soc;
  246. max17040_check_changes(chip);
  247. /* check changes and send uevent */
  248. if (last_soc != chip->soc)
  249. power_supply_changed(chip->battery);
  250. max17040_queue_work(chip);
  251. }
  252. /* Returns true if alert cause was SOC change, not low SOC */
  253. static bool max17040_handle_soc_alert(struct max17040_chip *chip)
  254. {
  255. bool ret = true;
  256. u32 data;
  257. regmap_read(chip->regmap, MAX17040_STATUS, &data);
  258. if (data & MAX17040_STATUS_HD_MASK) {
  259. // this alert was caused by low soc
  260. ret = false;
  261. }
  262. if (data & MAX17040_STATUS_SC_MASK) {
  263. // soc change bit -- deassert to mark as handled
  264. regmap_write(chip->regmap, MAX17040_STATUS,
  265. data & ~MAX17040_STATUS_SC_MASK);
  266. }
  267. return ret;
  268. }
  269. static irqreturn_t max17040_thread_handler(int id, void *dev)
  270. {
  271. struct max17040_chip *chip = dev;
  272. if (!(chip->data.has_soc_alert && max17040_handle_soc_alert(chip)))
  273. dev_warn(&chip->client->dev, "IRQ: Alert battery low level\n");
  274. /* read registers */
  275. max17040_check_changes(chip);
  276. /* send uevent */
  277. power_supply_changed(chip->battery);
  278. /* reset alert bit */
  279. max17040_set_low_soc_alert(chip, chip->low_soc_alert);
  280. return IRQ_HANDLED;
  281. }
  282. static int max17040_enable_alert_irq(struct max17040_chip *chip)
  283. {
  284. struct i2c_client *client = chip->client;
  285. int ret;
  286. ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  287. max17040_thread_handler, IRQF_ONESHOT,
  288. chip->battery->desc->name, chip);
  289. return ret;
  290. }
  291. static int max17040_prop_writeable(struct power_supply *psy,
  292. enum power_supply_property psp)
  293. {
  294. switch (psp) {
  295. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  296. return 1;
  297. default:
  298. return 0;
  299. }
  300. }
  301. static int max17040_set_property(struct power_supply *psy,
  302. enum power_supply_property psp,
  303. const union power_supply_propval *val)
  304. {
  305. struct max17040_chip *chip = power_supply_get_drvdata(psy);
  306. int ret;
  307. switch (psp) {
  308. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  309. /* alert threshold can be programmed from 1% up to 16/32% */
  310. if ((val->intval < 1) ||
  311. (val->intval > (chip->quirk_double_soc ? 16 : 32))) {
  312. ret = -EINVAL;
  313. break;
  314. }
  315. ret = max17040_set_low_soc_alert(chip, val->intval);
  316. chip->low_soc_alert = val->intval;
  317. break;
  318. default:
  319. ret = -EINVAL;
  320. }
  321. return ret;
  322. }
  323. static int max17040_get_property(struct power_supply *psy,
  324. enum power_supply_property psp,
  325. union power_supply_propval *val)
  326. {
  327. struct max17040_chip *chip = power_supply_get_drvdata(psy);
  328. switch (psp) {
  329. case POWER_SUPPLY_PROP_ONLINE:
  330. val->intval = max17040_get_online(chip);
  331. break;
  332. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  333. val->intval = max17040_get_vcell(chip);
  334. break;
  335. case POWER_SUPPLY_PROP_CAPACITY:
  336. val->intval = max17040_get_soc(chip);
  337. break;
  338. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  339. val->intval = chip->low_soc_alert;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. return 0;
  345. }
  346. static const struct regmap_config max17040_regmap = {
  347. .reg_bits = 8,
  348. .reg_stride = 2,
  349. .val_bits = 16,
  350. .val_format_endian = REGMAP_ENDIAN_BIG,
  351. };
  352. static enum power_supply_property max17040_battery_props[] = {
  353. POWER_SUPPLY_PROP_ONLINE,
  354. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  355. POWER_SUPPLY_PROP_CAPACITY,
  356. POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
  357. };
  358. static const struct power_supply_desc max17040_battery_desc = {
  359. .name = "battery",
  360. .type = POWER_SUPPLY_TYPE_BATTERY,
  361. .get_property = max17040_get_property,
  362. .set_property = max17040_set_property,
  363. .property_is_writeable = max17040_prop_writeable,
  364. .properties = max17040_battery_props,
  365. .num_properties = ARRAY_SIZE(max17040_battery_props),
  366. };
  367. static int max17040_probe(struct i2c_client *client,
  368. const struct i2c_device_id *id)
  369. {
  370. struct i2c_adapter *adapter = client->adapter;
  371. struct power_supply_config psy_cfg = {};
  372. struct max17040_chip *chip;
  373. enum chip_id chip_id;
  374. bool enable_irq = false;
  375. int ret;
  376. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  377. return -EIO;
  378. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  379. if (!chip)
  380. return -ENOMEM;
  381. chip->client = client;
  382. chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap);
  383. if (IS_ERR(chip->regmap))
  384. return PTR_ERR(chip->regmap);
  385. chip_id = (enum chip_id) id->driver_data;
  386. if (client->dev.of_node) {
  387. ret = max17040_get_of_data(chip);
  388. if (ret)
  389. return ret;
  390. chip_id = (uintptr_t)of_device_get_match_data(&client->dev);
  391. }
  392. chip->data = max17040_family[chip_id];
  393. i2c_set_clientdata(client, chip);
  394. psy_cfg.drv_data = chip;
  395. chip->battery = devm_power_supply_register(&client->dev,
  396. &max17040_battery_desc, &psy_cfg);
  397. if (IS_ERR(chip->battery)) {
  398. dev_err(&client->dev, "failed: power supply register\n");
  399. return PTR_ERR(chip->battery);
  400. }
  401. ret = max17040_get_version(chip);
  402. if (ret < 0)
  403. return ret;
  404. dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret);
  405. if (chip_id == ID_MAX17040 || chip_id == ID_MAX17041)
  406. max17040_reset(chip);
  407. max17040_set_rcomp(chip, chip->rcomp);
  408. /* check interrupt */
  409. if (client->irq && chip->data.has_low_soc_alert) {
  410. ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert);
  411. if (ret) {
  412. dev_err(&client->dev,
  413. "Failed to set low SOC alert: err %d\n", ret);
  414. return ret;
  415. }
  416. enable_irq = true;
  417. }
  418. if (client->irq && chip->data.has_soc_alert) {
  419. ret = max17040_set_soc_alert(chip, 1);
  420. if (ret) {
  421. dev_err(&client->dev,
  422. "Failed to set SOC alert: err %d\n", ret);
  423. return ret;
  424. }
  425. enable_irq = true;
  426. } else {
  427. /* soc alerts negate the need for polling */
  428. INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
  429. ret = devm_add_action(&client->dev, max17040_stop_work, chip);
  430. if (ret)
  431. return ret;
  432. max17040_queue_work(chip);
  433. }
  434. if (enable_irq) {
  435. ret = max17040_enable_alert_irq(chip);
  436. if (ret) {
  437. client->irq = 0;
  438. dev_warn(&client->dev,
  439. "Failed to get IRQ err %d\n", ret);
  440. }
  441. }
  442. return 0;
  443. }
  444. #ifdef CONFIG_PM_SLEEP
  445. static int max17040_suspend(struct device *dev)
  446. {
  447. struct i2c_client *client = to_i2c_client(dev);
  448. struct max17040_chip *chip = i2c_get_clientdata(client);
  449. if (client->irq && chip->data.has_soc_alert)
  450. // disable soc alert to prevent wakeup
  451. max17040_set_soc_alert(chip, 0);
  452. else
  453. cancel_delayed_work(&chip->work);
  454. if (client->irq && device_may_wakeup(dev))
  455. enable_irq_wake(client->irq);
  456. return 0;
  457. }
  458. static int max17040_resume(struct device *dev)
  459. {
  460. struct i2c_client *client = to_i2c_client(dev);
  461. struct max17040_chip *chip = i2c_get_clientdata(client);
  462. if (client->irq && device_may_wakeup(dev))
  463. disable_irq_wake(client->irq);
  464. if (client->irq && chip->data.has_soc_alert)
  465. max17040_set_soc_alert(chip, 1);
  466. else
  467. max17040_queue_work(chip);
  468. return 0;
  469. }
  470. static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
  471. #define MAX17040_PM_OPS (&max17040_pm_ops)
  472. #else
  473. #define MAX17040_PM_OPS NULL
  474. #endif /* CONFIG_PM_SLEEP */
  475. static const struct i2c_device_id max17040_id[] = {
  476. { "max17040", ID_MAX17040 },
  477. { "max17041", ID_MAX17041 },
  478. { "max17043", ID_MAX17043 },
  479. { "max77836-battery", ID_MAX17043 },
  480. { "max17044", ID_MAX17044 },
  481. { "max17048", ID_MAX17048 },
  482. { "max17049", ID_MAX17049 },
  483. { "max17058", ID_MAX17058 },
  484. { "max17059", ID_MAX17059 },
  485. { /* sentinel */ }
  486. };
  487. MODULE_DEVICE_TABLE(i2c, max17040_id);
  488. static const struct of_device_id max17040_of_match[] = {
  489. { .compatible = "maxim,max17040", .data = (void *) ID_MAX17040 },
  490. { .compatible = "maxim,max17041", .data = (void *) ID_MAX17041 },
  491. { .compatible = "maxim,max17043", .data = (void *) ID_MAX17043 },
  492. { .compatible = "maxim,max77836-battery", .data = (void *) ID_MAX17043 },
  493. { .compatible = "maxim,max17044", .data = (void *) ID_MAX17044 },
  494. { .compatible = "maxim,max17048", .data = (void *) ID_MAX17048 },
  495. { .compatible = "maxim,max17049", .data = (void *) ID_MAX17049 },
  496. { .compatible = "maxim,max17058", .data = (void *) ID_MAX17058 },
  497. { .compatible = "maxim,max17059", .data = (void *) ID_MAX17059 },
  498. { /* sentinel */ },
  499. };
  500. MODULE_DEVICE_TABLE(of, max17040_of_match);
  501. static struct i2c_driver max17040_i2c_driver = {
  502. .driver = {
  503. .name = "max17040",
  504. .of_match_table = max17040_of_match,
  505. .pm = MAX17040_PM_OPS,
  506. },
  507. .probe = max17040_probe,
  508. .id_table = max17040_id,
  509. };
  510. module_i2c_driver(max17040_i2c_driver);
  511. MODULE_AUTHOR("Minkyu Kang <[email protected]>");
  512. MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
  513. MODULE_LICENSE("GPL");