max17040_battery.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 = container_of(psy,
  55. struct max17040_chip, battery);
  56. switch (psp) {
  57. case POWER_SUPPLY_PROP_STATUS:
  58. val->intval = chip->status;
  59. break;
  60. case POWER_SUPPLY_PROP_ONLINE:
  61. val->intval = chip->online;
  62. break;
  63. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  64. val->intval = chip->vcell;
  65. break;
  66. case POWER_SUPPLY_PROP_CAPACITY:
  67. val->intval = chip->soc;
  68. break;
  69. default:
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. static int max17040_write_reg(struct i2c_client *client, int reg, u8 value)
  75. {
  76. int ret;
  77. ret = i2c_smbus_write_byte_data(client, reg, value);
  78. if (ret < 0)
  79. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  80. return ret;
  81. }
  82. static int max17040_read_reg(struct i2c_client *client, int reg)
  83. {
  84. int ret;
  85. ret = i2c_smbus_read_byte_data(client, reg);
  86. if (ret < 0)
  87. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  88. return ret;
  89. }
  90. static void max17040_reset(struct i2c_client *client)
  91. {
  92. max17040_write_reg(client, MAX17040_CMD_MSB, 0x54);
  93. max17040_write_reg(client, MAX17040_CMD_LSB, 0x00);
  94. }
  95. static void max17040_get_vcell(struct i2c_client *client)
  96. {
  97. struct max17040_chip *chip = i2c_get_clientdata(client);
  98. u8 msb;
  99. u8 lsb;
  100. msb = max17040_read_reg(client, MAX17040_VCELL_MSB);
  101. lsb = max17040_read_reg(client, MAX17040_VCELL_LSB);
  102. chip->vcell = (msb << 4) + (lsb >> 4);
  103. }
  104. static void max17040_get_soc(struct i2c_client *client)
  105. {
  106. struct max17040_chip *chip = i2c_get_clientdata(client);
  107. u8 msb;
  108. u8 lsb;
  109. msb = max17040_read_reg(client, MAX17040_SOC_MSB);
  110. lsb = max17040_read_reg(client, MAX17040_SOC_LSB);
  111. chip->soc = msb;
  112. }
  113. static void max17040_get_version(struct i2c_client *client)
  114. {
  115. u8 msb;
  116. u8 lsb;
  117. msb = max17040_read_reg(client, MAX17040_VER_MSB);
  118. lsb = max17040_read_reg(client, MAX17040_VER_LSB);
  119. dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver %d%d\n", msb, lsb);
  120. }
  121. static void max17040_get_online(struct i2c_client *client)
  122. {
  123. struct max17040_chip *chip = i2c_get_clientdata(client);
  124. if (chip->pdata && chip->pdata->battery_online)
  125. chip->online = chip->pdata->battery_online();
  126. else
  127. chip->online = 1;
  128. }
  129. static void max17040_get_status(struct i2c_client *client)
  130. {
  131. struct max17040_chip *chip = i2c_get_clientdata(client);
  132. if (!chip->pdata || !chip->pdata->charger_online
  133. || !chip->pdata->charger_enable) {
  134. chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
  135. return;
  136. }
  137. if (chip->pdata->charger_online()) {
  138. if (chip->pdata->charger_enable())
  139. chip->status = POWER_SUPPLY_STATUS_CHARGING;
  140. else
  141. chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  142. } else {
  143. chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
  144. }
  145. if (chip->soc > MAX17040_BATTERY_FULL)
  146. chip->status = POWER_SUPPLY_STATUS_FULL;
  147. }
  148. static void max17040_work(struct work_struct *work)
  149. {
  150. struct max17040_chip *chip;
  151. chip = container_of(work, struct max17040_chip, work.work);
  152. max17040_get_vcell(chip->client);
  153. max17040_get_soc(chip->client);
  154. max17040_get_online(chip->client);
  155. max17040_get_status(chip->client);
  156. schedule_delayed_work(&chip->work, 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 int __devinit max17040_probe(struct i2c_client *client,
  165. const struct i2c_device_id *id)
  166. {
  167. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  168. struct max17040_chip *chip;
  169. int ret;
  170. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  171. return -EIO;
  172. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  173. if (!chip)
  174. return -ENOMEM;
  175. chip->client = client;
  176. chip->pdata = client->dev.platform_data;
  177. i2c_set_clientdata(client, chip);
  178. chip->battery.name = "battery";
  179. chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  180. chip->battery.get_property = max17040_get_property;
  181. chip->battery.properties = max17040_battery_props;
  182. chip->battery.num_properties = ARRAY_SIZE(max17040_battery_props);
  183. ret = power_supply_register(&client->dev, &chip->battery);
  184. if (ret) {
  185. dev_err(&client->dev, "failed: power supply register\n");
  186. kfree(chip);
  187. return ret;
  188. }
  189. max17040_reset(client);
  190. max17040_get_version(client);
  191. INIT_DELAYED_WORK_DEFERRABLE(&chip->work, max17040_work);
  192. schedule_delayed_work(&chip->work, MAX17040_DELAY);
  193. return 0;
  194. }
  195. static int __devexit max17040_remove(struct i2c_client *client)
  196. {
  197. struct max17040_chip *chip = i2c_get_clientdata(client);
  198. power_supply_unregister(&chip->battery);
  199. cancel_delayed_work(&chip->work);
  200. kfree(chip);
  201. return 0;
  202. }
  203. #ifdef CONFIG_PM
  204. static int max17040_suspend(struct i2c_client *client,
  205. pm_message_t state)
  206. {
  207. struct max17040_chip *chip = i2c_get_clientdata(client);
  208. cancel_delayed_work(&chip->work);
  209. return 0;
  210. }
  211. static int max17040_resume(struct i2c_client *client)
  212. {
  213. struct max17040_chip *chip = i2c_get_clientdata(client);
  214. schedule_delayed_work(&chip->work, MAX17040_DELAY);
  215. return 0;
  216. }
  217. #else
  218. #define max17040_suspend NULL
  219. #define max17040_resume NULL
  220. #endif /* CONFIG_PM */
  221. static const struct i2c_device_id max17040_id[] = {
  222. { "max17040", 0 },
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(i2c, max17040_id);
  226. static struct i2c_driver max17040_i2c_driver = {
  227. .driver = {
  228. .name = "max17040",
  229. },
  230. .probe = max17040_probe,
  231. .remove = __devexit_p(max17040_remove),
  232. .suspend = max17040_suspend,
  233. .resume = max17040_resume,
  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");