max17040_battery.c 15 KB

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