max17042_battery.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Fuel gauge driver for Maxim 17042 / 8966 / 8997
  3. * Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
  4. *
  5. * Copyright (C) 2011 Samsung Electronics
  6. * MyungJoo Ham <myungjoo.ham@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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * This driver is based on max17040_battery.c
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/power_supply.h>
  30. #include <linux/power/max17042_battery.h>
  31. struct max17042_chip {
  32. struct i2c_client *client;
  33. struct power_supply battery;
  34. struct max17042_platform_data *pdata;
  35. };
  36. static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
  37. {
  38. int ret = i2c_smbus_write_word_data(client, reg, value);
  39. if (ret < 0)
  40. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  41. return ret;
  42. }
  43. static int max17042_read_reg(struct i2c_client *client, u8 reg)
  44. {
  45. int ret = i2c_smbus_read_word_data(client, reg);
  46. if (ret < 0)
  47. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  48. return ret;
  49. }
  50. static void max17042_set_reg(struct i2c_client *client,
  51. struct max17042_reg_data *data, int size)
  52. {
  53. int i;
  54. for (i = 0; i < size; i++)
  55. max17042_write_reg(client, data[i].addr, data[i].data);
  56. }
  57. static enum power_supply_property max17042_battery_props[] = {
  58. POWER_SUPPLY_PROP_PRESENT,
  59. POWER_SUPPLY_PROP_CYCLE_COUNT,
  60. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  61. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  62. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  63. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  64. POWER_SUPPLY_PROP_CAPACITY,
  65. POWER_SUPPLY_PROP_CHARGE_FULL,
  66. POWER_SUPPLY_PROP_TEMP,
  67. POWER_SUPPLY_PROP_CURRENT_NOW,
  68. POWER_SUPPLY_PROP_CURRENT_AVG,
  69. };
  70. static int max17042_get_property(struct power_supply *psy,
  71. enum power_supply_property psp,
  72. union power_supply_propval *val)
  73. {
  74. struct max17042_chip *chip = container_of(psy,
  75. struct max17042_chip, battery);
  76. int ret;
  77. switch (psp) {
  78. case POWER_SUPPLY_PROP_PRESENT:
  79. ret = max17042_read_reg(chip->client, MAX17042_STATUS);
  80. if (ret < 0)
  81. return ret;
  82. if (ret & MAX17042_STATUS_BattAbsent)
  83. val->intval = 0;
  84. else
  85. val->intval = 1;
  86. break;
  87. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  88. ret = max17042_read_reg(chip->client, MAX17042_Cycles);
  89. if (ret < 0)
  90. return ret;
  91. val->intval = ret;
  92. break;
  93. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  94. ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
  95. if (ret < 0)
  96. return ret;
  97. val->intval = ret >> 8;
  98. val->intval *= 20000; /* Units of LSB = 20mV */
  99. break;
  100. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  101. ret = max17042_read_reg(chip->client, MAX17042_V_empty);
  102. if (ret < 0)
  103. return ret;
  104. val->intval = ret >> 7;
  105. val->intval *= 10000; /* Units of LSB = 10mV */
  106. break;
  107. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  108. ret = max17042_read_reg(chip->client, MAX17042_VCELL);
  109. if (ret < 0)
  110. return ret;
  111. val->intval = ret * 625 / 8;
  112. break;
  113. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  114. ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
  115. if (ret < 0)
  116. return ret;
  117. val->intval = ret * 625 / 8;
  118. break;
  119. case POWER_SUPPLY_PROP_CAPACITY:
  120. ret = max17042_read_reg(chip->client, MAX17042_SOC);
  121. if (ret < 0)
  122. return ret;
  123. val->intval = ret >> 8;
  124. break;
  125. case POWER_SUPPLY_PROP_CHARGE_FULL:
  126. ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
  127. if (ret < 0)
  128. return ret;
  129. if ((ret >> 8) >= MAX17042_BATTERY_FULL)
  130. val->intval = 1;
  131. else if (ret >= 0)
  132. val->intval = 0;
  133. break;
  134. case POWER_SUPPLY_PROP_TEMP:
  135. ret = max17042_read_reg(chip->client, MAX17042_TEMP);
  136. if (ret < 0)
  137. return ret;
  138. val->intval = ret;
  139. /* The value is signed. */
  140. if (val->intval & 0x8000) {
  141. val->intval = (0x7fff & ~val->intval) + 1;
  142. val->intval *= -1;
  143. }
  144. /* The value is converted into deci-centigrade scale */
  145. /* Units of LSB = 1 / 256 degree Celsius */
  146. val->intval = val->intval * 10 / 256;
  147. break;
  148. case POWER_SUPPLY_PROP_CURRENT_NOW:
  149. if (chip->pdata->enable_current_sense) {
  150. ret = max17042_read_reg(chip->client, MAX17042_Current);
  151. if (ret < 0)
  152. return ret;
  153. val->intval = ret;
  154. if (val->intval & 0x8000) {
  155. /* Negative */
  156. val->intval = ~val->intval & 0x7fff;
  157. val->intval++;
  158. val->intval *= -1;
  159. }
  160. val->intval *= 1562500 / chip->pdata->r_sns;
  161. } else {
  162. return -EINVAL;
  163. }
  164. break;
  165. case POWER_SUPPLY_PROP_CURRENT_AVG:
  166. if (chip->pdata->enable_current_sense) {
  167. ret = max17042_read_reg(chip->client,
  168. MAX17042_AvgCurrent);
  169. if (ret < 0)
  170. return ret;
  171. val->intval = ret;
  172. if (val->intval & 0x8000) {
  173. /* Negative */
  174. val->intval = ~val->intval & 0x7fff;
  175. val->intval++;
  176. val->intval *= -1;
  177. }
  178. val->intval *= 1562500 / chip->pdata->r_sns;
  179. } else {
  180. return -EINVAL;
  181. }
  182. break;
  183. default:
  184. return -EINVAL;
  185. }
  186. return 0;
  187. }
  188. static int __devinit max17042_probe(struct i2c_client *client,
  189. const struct i2c_device_id *id)
  190. {
  191. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  192. struct max17042_chip *chip;
  193. int ret;
  194. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  195. return -EIO;
  196. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  197. if (!chip)
  198. return -ENOMEM;
  199. chip->client = client;
  200. chip->pdata = client->dev.platform_data;
  201. i2c_set_clientdata(client, chip);
  202. chip->battery.name = "max17042_battery";
  203. chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  204. chip->battery.get_property = max17042_get_property;
  205. chip->battery.properties = max17042_battery_props;
  206. chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
  207. /* When current is not measured,
  208. * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
  209. if (!chip->pdata->enable_current_sense)
  210. chip->battery.num_properties -= 2;
  211. if (chip->pdata->r_sns == 0)
  212. chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
  213. ret = power_supply_register(&client->dev, &chip->battery);
  214. if (ret) {
  215. dev_err(&client->dev, "failed: power supply register\n");
  216. kfree(chip);
  217. return ret;
  218. }
  219. /* Initialize registers according to values from the platform data */
  220. if (chip->pdata->init_data)
  221. max17042_set_reg(client, chip->pdata->init_data,
  222. chip->pdata->num_init_data);
  223. if (!chip->pdata->enable_current_sense) {
  224. max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
  225. max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
  226. max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
  227. }
  228. return 0;
  229. }
  230. static int __devexit max17042_remove(struct i2c_client *client)
  231. {
  232. struct max17042_chip *chip = i2c_get_clientdata(client);
  233. power_supply_unregister(&chip->battery);
  234. kfree(chip);
  235. return 0;
  236. }
  237. static const struct i2c_device_id max17042_id[] = {
  238. { "max17042", 0 },
  239. { }
  240. };
  241. MODULE_DEVICE_TABLE(i2c, max17042_id);
  242. static struct i2c_driver max17042_i2c_driver = {
  243. .driver = {
  244. .name = "max17042",
  245. },
  246. .probe = max17042_probe,
  247. .remove = __devexit_p(max17042_remove),
  248. .id_table = max17042_id,
  249. };
  250. static int __init max17042_init(void)
  251. {
  252. return i2c_add_driver(&max17042_i2c_driver);
  253. }
  254. module_init(max17042_init);
  255. static void __exit max17042_exit(void)
  256. {
  257. i2c_del_driver(&max17042_i2c_driver);
  258. }
  259. module_exit(max17042_exit);
  260. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  261. MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
  262. MODULE_LICENSE("GPL");