max17040_battery.c 15 KB

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