max17042_battery.c 7.4 KB

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