max17040_battery.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * max17040_battery.c
  3. * fuel-gauge systems for lithium-ion (Li+) batteries
  4. *
  5. * Copyright (C) 2009 Samsung Electronics
  6. * Minkyu Kang <mk7.kang@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mutex.h>
  16. #include <linux/err.h>
  17. #include <linux/i2c.h>
  18. #include <linux/delay.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/max17040_battery.h>
  21. #include <linux/slab.h>
  22. #define MAX17040_VCELL 0x02
  23. #define MAX17040_SOC 0x04
  24. #define MAX17040_MODE 0x06
  25. #define MAX17040_VER 0x08
  26. #define MAX17040_RCOMP 0x0C
  27. #define MAX17040_CMD 0xFE
  28. #define MAX17040_DELAY 1000
  29. #define MAX17040_BATTERY_FULL 95
  30. struct max17040_chip {
  31. struct i2c_client *client;
  32. struct delayed_work work;
  33. struct power_supply *battery;
  34. struct max17040_platform_data *pdata;
  35. /* State Of Connect */
  36. int online;
  37. /* battery voltage */
  38. int vcell;
  39. /* battery capacity */
  40. int soc;
  41. /* State Of Charge */
  42. int status;
  43. };
  44. static int max17040_get_property(struct power_supply *psy,
  45. enum power_supply_property psp,
  46. union power_supply_propval *val)
  47. {
  48. struct max17040_chip *chip = power_supply_get_drvdata(psy);
  49. switch (psp) {
  50. case POWER_SUPPLY_PROP_STATUS:
  51. val->intval = chip->status;
  52. break;
  53. case POWER_SUPPLY_PROP_ONLINE:
  54. val->intval = chip->online;
  55. break;
  56. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  57. val->intval = chip->vcell;
  58. break;
  59. case POWER_SUPPLY_PROP_CAPACITY:
  60. val->intval = chip->soc;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. return 0;
  66. }
  67. static int max17040_write_reg(struct i2c_client *client, int reg, u16 value)
  68. {
  69. int ret;
  70. ret = i2c_smbus_write_word_swapped(client, reg, value);
  71. if (ret < 0)
  72. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  73. return ret;
  74. }
  75. static int max17040_read_reg(struct i2c_client *client, int reg)
  76. {
  77. int ret;
  78. ret = i2c_smbus_read_word_swapped(client, reg);
  79. if (ret < 0)
  80. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  81. return ret;
  82. }
  83. static void max17040_reset(struct i2c_client *client)
  84. {
  85. max17040_write_reg(client, MAX17040_CMD, 0x0054);
  86. }
  87. static void max17040_get_vcell(struct i2c_client *client)
  88. {
  89. struct max17040_chip *chip = i2c_get_clientdata(client);
  90. u16 vcell;
  91. vcell = max17040_read_reg(client, MAX17040_VCELL);
  92. chip->vcell = vcell;
  93. }
  94. static void max17040_get_soc(struct i2c_client *client)
  95. {
  96. struct max17040_chip *chip = i2c_get_clientdata(client);
  97. u16 soc;
  98. soc = max17040_read_reg(client, MAX17040_SOC);
  99. chip->soc = (soc >> 8);
  100. }
  101. static void max17040_get_version(struct i2c_client *client)
  102. {
  103. u16 version;
  104. version = max17040_read_reg(client, MAX17040_VER);
  105. dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version);
  106. }
  107. static void max17040_get_online(struct i2c_client *client)
  108. {
  109. struct max17040_chip *chip = i2c_get_clientdata(client);
  110. if (chip->pdata && chip->pdata->battery_online)
  111. chip->online = chip->pdata->battery_online();
  112. else
  113. chip->online = 1;
  114. }
  115. static void max17040_get_status(struct i2c_client *client)
  116. {
  117. struct max17040_chip *chip = i2c_get_clientdata(client);
  118. if (!chip->pdata || !chip->pdata->charger_online
  119. || !chip->pdata->charger_enable) {
  120. chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
  121. return;
  122. }
  123. if (chip->pdata->charger_online()) {
  124. if (chip->pdata->charger_enable())
  125. chip->status = POWER_SUPPLY_STATUS_CHARGING;
  126. else
  127. chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  128. } else {
  129. chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
  130. }
  131. if (chip->soc > MAX17040_BATTERY_FULL)
  132. chip->status = POWER_SUPPLY_STATUS_FULL;
  133. }
  134. static void max17040_work(struct work_struct *work)
  135. {
  136. struct max17040_chip *chip;
  137. chip = container_of(work, struct max17040_chip, work.work);
  138. max17040_get_vcell(chip->client);
  139. max17040_get_soc(chip->client);
  140. max17040_get_online(chip->client);
  141. max17040_get_status(chip->client);
  142. queue_delayed_work(system_power_efficient_wq, &chip->work,
  143. MAX17040_DELAY);
  144. }
  145. static enum power_supply_property max17040_battery_props[] = {
  146. POWER_SUPPLY_PROP_STATUS,
  147. POWER_SUPPLY_PROP_ONLINE,
  148. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  149. POWER_SUPPLY_PROP_CAPACITY,
  150. };
  151. static const struct power_supply_desc max17040_battery_desc = {
  152. .name = "battery",
  153. .type = POWER_SUPPLY_TYPE_BATTERY,
  154. .get_property = max17040_get_property,
  155. .properties = max17040_battery_props,
  156. .num_properties = ARRAY_SIZE(max17040_battery_props),
  157. };
  158. static int max17040_probe(struct i2c_client *client,
  159. const struct i2c_device_id *id)
  160. {
  161. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  162. struct power_supply_config psy_cfg = {};
  163. struct max17040_chip *chip;
  164. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  165. return -EIO;
  166. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  167. if (!chip)
  168. return -ENOMEM;
  169. chip->client = client;
  170. chip->pdata = client->dev.platform_data;
  171. i2c_set_clientdata(client, chip);
  172. psy_cfg.drv_data = chip;
  173. chip->battery = power_supply_register(&client->dev,
  174. &max17040_battery_desc, &psy_cfg);
  175. if (IS_ERR(chip->battery)) {
  176. dev_err(&client->dev, "failed: power supply register\n");
  177. return PTR_ERR(chip->battery);
  178. }
  179. max17040_reset(client);
  180. max17040_get_version(client);
  181. INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
  182. queue_delayed_work(system_power_efficient_wq, &chip->work,
  183. MAX17040_DELAY);
  184. return 0;
  185. }
  186. static int max17040_remove(struct i2c_client *client)
  187. {
  188. struct max17040_chip *chip = i2c_get_clientdata(client);
  189. power_supply_unregister(chip->battery);
  190. cancel_delayed_work(&chip->work);
  191. return 0;
  192. }
  193. #ifdef CONFIG_PM_SLEEP
  194. static int max17040_suspend(struct device *dev)
  195. {
  196. struct i2c_client *client = to_i2c_client(dev);
  197. struct max17040_chip *chip = i2c_get_clientdata(client);
  198. cancel_delayed_work(&chip->work);
  199. return 0;
  200. }
  201. static int max17040_resume(struct device *dev)
  202. {
  203. struct i2c_client *client = to_i2c_client(dev);
  204. struct max17040_chip *chip = i2c_get_clientdata(client);
  205. queue_delayed_work(system_power_efficient_wq, &chip->work,
  206. MAX17040_DELAY);
  207. return 0;
  208. }
  209. static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
  210. #define MAX17040_PM_OPS (&max17040_pm_ops)
  211. #else
  212. #define MAX17040_PM_OPS NULL
  213. #endif /* CONFIG_PM_SLEEP */
  214. static const struct i2c_device_id max17040_id[] = {
  215. { "max17040" },
  216. { "max77836-battery" },
  217. { }
  218. };
  219. MODULE_DEVICE_TABLE(i2c, max17040_id);
  220. static const struct of_device_id max17040_of_match[] = {
  221. { .compatible = "maxim,max17040" },
  222. { .compatible = "maxim,max77836-battery" },
  223. { },
  224. };
  225. MODULE_DEVICE_TABLE(of, max17040_of_match);
  226. static struct i2c_driver max17040_i2c_driver = {
  227. .driver = {
  228. .name = "max17040",
  229. .of_match_table = max17040_of_match,
  230. .pm = MAX17040_PM_OPS,
  231. },
  232. .probe = max17040_probe,
  233. .remove = max17040_remove,
  234. .id_table = max17040_id,
  235. };
  236. module_i2c_driver(max17040_i2c_driver);
  237. MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
  238. MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
  239. MODULE_LICENSE("GPL");