max17040_battery.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_MSB 0x02
  23. #define MAX17040_VCELL_LSB 0x03
  24. #define MAX17040_SOC_MSB 0x04
  25. #define MAX17040_SOC_LSB 0x05
  26. #define MAX17040_MODE_MSB 0x06
  27. #define MAX17040_MODE_LSB 0x07
  28. #define MAX17040_VER_MSB 0x08
  29. #define MAX17040_VER_LSB 0x09
  30. #define MAX17040_RCOMP_MSB 0x0C
  31. #define MAX17040_RCOMP_LSB 0x0D
  32. #define MAX17040_CMD_MSB 0xFE
  33. #define MAX17040_CMD_LSB 0xFF
  34. #define MAX17040_DELAY 1000
  35. #define MAX17040_BATTERY_FULL 95
  36. struct max17040_chip {
  37. struct i2c_client *client;
  38. struct delayed_work work;
  39. struct power_supply *battery;
  40. struct max17040_platform_data *pdata;
  41. /* State Of Connect */
  42. int online;
  43. /* battery voltage */
  44. int vcell;
  45. /* battery capacity */
  46. int soc;
  47. /* State Of Charge */
  48. int status;
  49. };
  50. static int max17040_get_property(struct power_supply *psy,
  51. enum power_supply_property psp,
  52. union power_supply_propval *val)
  53. {
  54. struct max17040_chip *chip = power_supply_get_drvdata(psy);
  55. switch (psp) {
  56. case POWER_SUPPLY_PROP_STATUS:
  57. val->intval = chip->status;
  58. break;
  59. case POWER_SUPPLY_PROP_ONLINE:
  60. val->intval = chip->online;
  61. break;
  62. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  63. val->intval = chip->vcell;
  64. break;
  65. case POWER_SUPPLY_PROP_CAPACITY:
  66. val->intval = chip->soc;
  67. break;
  68. default:
  69. return -EINVAL;
  70. }
  71. return 0;
  72. }
  73. static int max17040_write_reg(struct i2c_client *client, int reg, u8 value)
  74. {
  75. int ret;
  76. ret = i2c_smbus_write_byte_data(client, reg, value);
  77. if (ret < 0)
  78. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  79. return ret;
  80. }
  81. static int max17040_read_reg(struct i2c_client *client, int reg)
  82. {
  83. int ret;
  84. ret = i2c_smbus_read_byte_data(client, reg);
  85. if (ret < 0)
  86. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  87. return ret;
  88. }
  89. static void max17040_reset(struct i2c_client *client)
  90. {
  91. max17040_write_reg(client, MAX17040_CMD_MSB, 0x54);
  92. max17040_write_reg(client, MAX17040_CMD_LSB, 0x00);
  93. }
  94. static void max17040_get_vcell(struct i2c_client *client)
  95. {
  96. struct max17040_chip *chip = i2c_get_clientdata(client);
  97. u8 msb;
  98. u8 lsb;
  99. msb = max17040_read_reg(client, MAX17040_VCELL_MSB);
  100. lsb = max17040_read_reg(client, MAX17040_VCELL_LSB);
  101. chip->vcell = (msb << 4) + (lsb >> 4);
  102. }
  103. static void max17040_get_soc(struct i2c_client *client)
  104. {
  105. struct max17040_chip *chip = i2c_get_clientdata(client);
  106. u8 msb;
  107. u8 lsb;
  108. msb = max17040_read_reg(client, MAX17040_SOC_MSB);
  109. lsb = max17040_read_reg(client, MAX17040_SOC_LSB);
  110. chip->soc = msb;
  111. }
  112. static void max17040_get_version(struct i2c_client *client)
  113. {
  114. u8 msb;
  115. u8 lsb;
  116. msb = max17040_read_reg(client, MAX17040_VER_MSB);
  117. lsb = max17040_read_reg(client, MAX17040_VER_LSB);
  118. dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver %d%d\n", msb, lsb);
  119. }
  120. static void max17040_get_online(struct i2c_client *client)
  121. {
  122. struct max17040_chip *chip = i2c_get_clientdata(client);
  123. if (chip->pdata && chip->pdata->battery_online)
  124. chip->online = chip->pdata->battery_online();
  125. else
  126. chip->online = 1;
  127. }
  128. static void max17040_get_status(struct i2c_client *client)
  129. {
  130. struct max17040_chip *chip = i2c_get_clientdata(client);
  131. if (!chip->pdata || !chip->pdata->charger_online
  132. || !chip->pdata->charger_enable) {
  133. chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
  134. return;
  135. }
  136. if (chip->pdata->charger_online()) {
  137. if (chip->pdata->charger_enable())
  138. chip->status = POWER_SUPPLY_STATUS_CHARGING;
  139. else
  140. chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  141. } else {
  142. chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
  143. }
  144. if (chip->soc > MAX17040_BATTERY_FULL)
  145. chip->status = POWER_SUPPLY_STATUS_FULL;
  146. }
  147. static void max17040_work(struct work_struct *work)
  148. {
  149. struct max17040_chip *chip;
  150. chip = container_of(work, struct max17040_chip, work.work);
  151. max17040_get_vcell(chip->client);
  152. max17040_get_soc(chip->client);
  153. max17040_get_online(chip->client);
  154. max17040_get_status(chip->client);
  155. queue_delayed_work(system_power_efficient_wq, &chip->work,
  156. MAX17040_DELAY);
  157. }
  158. static enum power_supply_property max17040_battery_props[] = {
  159. POWER_SUPPLY_PROP_STATUS,
  160. POWER_SUPPLY_PROP_ONLINE,
  161. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  162. POWER_SUPPLY_PROP_CAPACITY,
  163. };
  164. static const struct power_supply_desc max17040_battery_desc = {
  165. .name = "battery",
  166. .type = POWER_SUPPLY_TYPE_BATTERY,
  167. .get_property = max17040_get_property,
  168. .properties = max17040_battery_props,
  169. .num_properties = ARRAY_SIZE(max17040_battery_props),
  170. };
  171. static int max17040_probe(struct i2c_client *client,
  172. const struct i2c_device_id *id)
  173. {
  174. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  175. struct power_supply_config psy_cfg = {};
  176. struct max17040_chip *chip;
  177. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  178. return -EIO;
  179. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  180. if (!chip)
  181. return -ENOMEM;
  182. chip->client = client;
  183. chip->pdata = client->dev.platform_data;
  184. i2c_set_clientdata(client, chip);
  185. psy_cfg.drv_data = chip;
  186. chip->battery = power_supply_register(&client->dev,
  187. &max17040_battery_desc, &psy_cfg);
  188. if (IS_ERR(chip->battery)) {
  189. dev_err(&client->dev, "failed: power supply register\n");
  190. return PTR_ERR(chip->battery);
  191. }
  192. max17040_reset(client);
  193. max17040_get_version(client);
  194. INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
  195. queue_delayed_work(system_power_efficient_wq, &chip->work,
  196. MAX17040_DELAY);
  197. return 0;
  198. }
  199. static int max17040_remove(struct i2c_client *client)
  200. {
  201. struct max17040_chip *chip = i2c_get_clientdata(client);
  202. power_supply_unregister(chip->battery);
  203. cancel_delayed_work(&chip->work);
  204. return 0;
  205. }
  206. #ifdef CONFIG_PM_SLEEP
  207. static int max17040_suspend(struct device *dev)
  208. {
  209. struct i2c_client *client = to_i2c_client(dev);
  210. struct max17040_chip *chip = i2c_get_clientdata(client);
  211. cancel_delayed_work(&chip->work);
  212. return 0;
  213. }
  214. static int max17040_resume(struct device *dev)
  215. {
  216. struct i2c_client *client = to_i2c_client(dev);
  217. struct max17040_chip *chip = i2c_get_clientdata(client);
  218. queue_delayed_work(system_power_efficient_wq, &chip->work,
  219. MAX17040_DELAY);
  220. return 0;
  221. }
  222. static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
  223. #define MAX17040_PM_OPS (&max17040_pm_ops)
  224. #else
  225. #define MAX17040_PM_OPS NULL
  226. #endif /* CONFIG_PM_SLEEP */
  227. static const struct i2c_device_id max17040_id[] = {
  228. { "max17040" },
  229. { "max77836-battery" },
  230. { }
  231. };
  232. MODULE_DEVICE_TABLE(i2c, max17040_id);
  233. static struct i2c_driver max17040_i2c_driver = {
  234. .driver = {
  235. .name = "max17040",
  236. .pm = MAX17040_PM_OPS,
  237. },
  238. .probe = max17040_probe,
  239. .remove = max17040_remove,
  240. .id_table = max17040_id,
  241. };
  242. module_i2c_driver(max17040_i2c_driver);
  243. MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
  244. MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
  245. MODULE_LICENSE("GPL");