max17042_battery.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. switch (psp) {
  77. case POWER_SUPPLY_PROP_PRESENT:
  78. val->intval = max17042_read_reg(chip->client,
  79. MAX17042_STATUS);
  80. if (val->intval & MAX17042_STATUS_BattAbsent)
  81. val->intval = 0;
  82. else
  83. val->intval = 1;
  84. break;
  85. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  86. val->intval = max17042_read_reg(chip->client,
  87. MAX17042_Cycles);
  88. break;
  89. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  90. val->intval = max17042_read_reg(chip->client,
  91. MAX17042_MinMaxVolt);
  92. val->intval >>= 8;
  93. val->intval *= 20000; /* Units of LSB = 20mV */
  94. break;
  95. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  96. val->intval = max17042_read_reg(chip->client,
  97. MAX17042_V_empty);
  98. val->intval >>= 7;
  99. val->intval *= 10000; /* Units of LSB = 10mV */
  100. break;
  101. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  102. val->intval = max17042_read_reg(chip->client,
  103. MAX17042_VCELL) * 83; /* 1000 / 12 = 83 */
  104. break;
  105. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  106. val->intval = max17042_read_reg(chip->client,
  107. MAX17042_AvgVCELL) * 83;
  108. break;
  109. case POWER_SUPPLY_PROP_CAPACITY:
  110. val->intval = max17042_read_reg(chip->client,
  111. MAX17042_SOC) / 256;
  112. break;
  113. case POWER_SUPPLY_PROP_CHARGE_FULL:
  114. val->intval = max17042_read_reg(chip->client,
  115. MAX17042_RepSOC);
  116. if ((val->intval / 256) >= MAX17042_BATTERY_FULL)
  117. val->intval = 1;
  118. else if (val->intval >= 0)
  119. val->intval = 0;
  120. break;
  121. case POWER_SUPPLY_PROP_TEMP:
  122. val->intval = max17042_read_reg(chip->client,
  123. MAX17042_TEMP);
  124. /* The value is signed. */
  125. if (val->intval & 0x8000) {
  126. val->intval = (0x7fff & ~val->intval) + 1;
  127. val->intval *= -1;
  128. }
  129. /* The value is converted into deci-centigrade scale */
  130. /* Units of LSB = 1 / 256 degree Celsius */
  131. val->intval = val->intval * 10 / 256;
  132. break;
  133. case POWER_SUPPLY_PROP_CURRENT_NOW:
  134. if (chip->pdata->enable_current_sense) {
  135. val->intval = max17042_read_reg(chip->client,
  136. MAX17042_Current);
  137. if (val->intval & 0x8000) {
  138. /* Negative */
  139. val->intval = ~val->intval & 0x7fff;
  140. val->intval++;
  141. val->intval *= -1;
  142. }
  143. val->intval >>= 4;
  144. val->intval *= 1000000 * 25 / chip->pdata->r_sns;
  145. } else {
  146. return -EINVAL;
  147. }
  148. break;
  149. case POWER_SUPPLY_PROP_CURRENT_AVG:
  150. if (chip->pdata->enable_current_sense) {
  151. val->intval = max17042_read_reg(chip->client,
  152. MAX17042_AvgCurrent);
  153. if (val->intval & 0x8000) {
  154. /* Negative */
  155. val->intval = ~val->intval & 0x7fff;
  156. val->intval++;
  157. val->intval *= -1;
  158. }
  159. val->intval *= 1562500 / chip->pdata->r_sns;
  160. } else {
  161. return -EINVAL;
  162. }
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. return 0;
  168. }
  169. static int __devinit max17042_probe(struct i2c_client *client,
  170. const struct i2c_device_id *id)
  171. {
  172. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  173. struct max17042_chip *chip;
  174. int ret;
  175. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  176. return -EIO;
  177. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  178. if (!chip)
  179. return -ENOMEM;
  180. chip->client = client;
  181. chip->pdata = client->dev.platform_data;
  182. i2c_set_clientdata(client, chip);
  183. chip->battery.name = "max17042_battery";
  184. chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  185. chip->battery.get_property = max17042_get_property;
  186. chip->battery.properties = max17042_battery_props;
  187. chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
  188. /* When current is not measured,
  189. * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
  190. if (!chip->pdata->enable_current_sense)
  191. chip->battery.num_properties -= 2;
  192. ret = power_supply_register(&client->dev, &chip->battery);
  193. if (ret) {
  194. dev_err(&client->dev, "failed: power supply register\n");
  195. kfree(chip);
  196. return ret;
  197. }
  198. /* Initialize registers according to values from the platform data */
  199. if (chip->pdata->init_data)
  200. max17042_set_reg(client, chip->pdata->init_data,
  201. chip->pdata->num_init_data);
  202. if (!chip->pdata->enable_current_sense) {
  203. max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
  204. max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
  205. max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
  206. } else {
  207. if (chip->pdata->r_sns == 0)
  208. chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
  209. }
  210. return 0;
  211. }
  212. static int __devexit max17042_remove(struct i2c_client *client)
  213. {
  214. struct max17042_chip *chip = i2c_get_clientdata(client);
  215. power_supply_unregister(&chip->battery);
  216. kfree(chip);
  217. return 0;
  218. }
  219. static const struct i2c_device_id max17042_id[] = {
  220. { "max17042", 0 },
  221. { }
  222. };
  223. MODULE_DEVICE_TABLE(i2c, max17042_id);
  224. static struct i2c_driver max17042_i2c_driver = {
  225. .driver = {
  226. .name = "max17042",
  227. },
  228. .probe = max17042_probe,
  229. .remove = __devexit_p(max17042_remove),
  230. .id_table = max17042_id,
  231. };
  232. static int __init max17042_init(void)
  233. {
  234. return i2c_add_driver(&max17042_i2c_driver);
  235. }
  236. module_init(max17042_init);
  237. static void __exit max17042_exit(void)
  238. {
  239. i2c_del_driver(&max17042_i2c_driver);
  240. }
  241. module_exit(max17042_exit);
  242. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  243. MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
  244. MODULE_LICENSE("GPL");