max17042_battery.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. enum max17042_register {
  31. MAX17042_STATUS = 0x00,
  32. MAX17042_VALRT_Th = 0x01,
  33. MAX17042_TALRT_Th = 0x02,
  34. MAX17042_SALRT_Th = 0x03,
  35. MAX17042_AtRate = 0x04,
  36. MAX17042_RepCap = 0x05,
  37. MAX17042_RepSOC = 0x06,
  38. MAX17042_Age = 0x07,
  39. MAX17042_TEMP = 0x08,
  40. MAX17042_VCELL = 0x09,
  41. MAX17042_Current = 0x0A,
  42. MAX17042_AvgCurrent = 0x0B,
  43. MAX17042_Qresidual = 0x0C,
  44. MAX17042_SOC = 0x0D,
  45. MAX17042_AvSOC = 0x0E,
  46. MAX17042_RemCap = 0x0F,
  47. MAX17402_FullCAP = 0x10,
  48. MAX17042_TTE = 0x11,
  49. MAX17042_V_empty = 0x12,
  50. MAX17042_RSLOW = 0x14,
  51. MAX17042_AvgTA = 0x16,
  52. MAX17042_Cycles = 0x17,
  53. MAX17042_DesignCap = 0x18,
  54. MAX17042_AvgVCELL = 0x19,
  55. MAX17042_MinMaxTemp = 0x1A,
  56. MAX17042_MinMaxVolt = 0x1B,
  57. MAX17042_MinMaxCurr = 0x1C,
  58. MAX17042_CONFIG = 0x1D,
  59. MAX17042_ICHGTerm = 0x1E,
  60. MAX17042_AvCap = 0x1F,
  61. MAX17042_ManName = 0x20,
  62. MAX17042_DevName = 0x21,
  63. MAX17042_DevChem = 0x22,
  64. MAX17042_TempNom = 0x24,
  65. MAX17042_TempCold = 0x25,
  66. MAX17042_TempHot = 0x26,
  67. MAX17042_AIN = 0x27,
  68. MAX17042_LearnCFG = 0x28,
  69. MAX17042_SHFTCFG = 0x29,
  70. MAX17042_RelaxCFG = 0x2A,
  71. MAX17042_MiscCFG = 0x2B,
  72. MAX17042_TGAIN = 0x2C,
  73. MAx17042_TOFF = 0x2D,
  74. MAX17042_CGAIN = 0x2E,
  75. MAX17042_COFF = 0x2F,
  76. MAX17042_Q_empty = 0x33,
  77. MAX17042_T_empty = 0x34,
  78. MAX17042_RCOMP0 = 0x38,
  79. MAX17042_TempCo = 0x39,
  80. MAX17042_Rx = 0x3A,
  81. MAX17042_T_empty0 = 0x3B,
  82. MAX17042_TaskPeriod = 0x3C,
  83. MAX17042_FSTAT = 0x3D,
  84. MAX17042_SHDNTIMER = 0x3F,
  85. MAX17042_VFRemCap = 0x4A,
  86. MAX17042_QH = 0x4D,
  87. MAX17042_QL = 0x4E,
  88. };
  89. struct max17042_chip {
  90. struct i2c_client *client;
  91. struct power_supply battery;
  92. struct max17042_platform_data *pdata;
  93. };
  94. static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
  95. {
  96. int ret = i2c_smbus_write_word_data(client, reg, value);
  97. if (ret < 0)
  98. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  99. return ret;
  100. }
  101. static int max17042_read_reg(struct i2c_client *client, u8 reg)
  102. {
  103. int ret = i2c_smbus_read_word_data(client, reg);
  104. if (ret < 0)
  105. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  106. return ret;
  107. }
  108. static enum power_supply_property max17042_battery_props[] = {
  109. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  110. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  111. POWER_SUPPLY_PROP_CAPACITY,
  112. };
  113. static int max17042_get_property(struct power_supply *psy,
  114. enum power_supply_property psp,
  115. union power_supply_propval *val)
  116. {
  117. struct max17042_chip *chip = container_of(psy,
  118. struct max17042_chip, battery);
  119. switch (psp) {
  120. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  121. val->intval = max17042_read_reg(chip->client,
  122. MAX17042_VCELL) * 83; /* 1000 / 12 = 83 */
  123. break;
  124. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  125. val->intval = max17042_read_reg(chip->client,
  126. MAX17042_AvgVCELL) * 83;
  127. break;
  128. case POWER_SUPPLY_PROP_CAPACITY:
  129. val->intval = max17042_read_reg(chip->client,
  130. MAX17042_SOC) / 256;
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. return 0;
  136. }
  137. static int __devinit max17042_probe(struct i2c_client *client,
  138. const struct i2c_device_id *id)
  139. {
  140. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  141. struct max17042_chip *chip;
  142. int ret;
  143. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  144. return -EIO;
  145. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  146. if (!chip)
  147. return -ENOMEM;
  148. chip->client = client;
  149. chip->pdata = client->dev.platform_data;
  150. i2c_set_clientdata(client, chip);
  151. chip->battery.name = "max17042_battery";
  152. chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  153. chip->battery.get_property = max17042_get_property;
  154. chip->battery.properties = max17042_battery_props;
  155. chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
  156. ret = power_supply_register(&client->dev, &chip->battery);
  157. if (ret) {
  158. dev_err(&client->dev, "failed: power supply register\n");
  159. i2c_set_clientdata(client, NULL);
  160. kfree(chip);
  161. return ret;
  162. }
  163. if (!chip->pdata->enable_current_sense) {
  164. max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
  165. max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
  166. max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
  167. }
  168. return 0;
  169. }
  170. static int __devexit max17042_remove(struct i2c_client *client)
  171. {
  172. struct max17042_chip *chip = i2c_get_clientdata(client);
  173. power_supply_unregister(&chip->battery);
  174. i2c_set_clientdata(client, NULL);
  175. kfree(chip);
  176. return 0;
  177. }
  178. static const struct i2c_device_id max17042_id[] = {
  179. { "max17042", 0 },
  180. { }
  181. };
  182. MODULE_DEVICE_TABLE(i2c, max17042_id);
  183. static struct i2c_driver max17042_i2c_driver = {
  184. .driver = {
  185. .name = "max17042",
  186. },
  187. .probe = max17042_probe,
  188. .remove = __devexit_p(max17042_remove),
  189. .id_table = max17042_id,
  190. };
  191. static int __init max17042_init(void)
  192. {
  193. return i2c_add_driver(&max17042_i2c_driver);
  194. }
  195. module_init(max17042_init);
  196. static void __exit max17042_exit(void)
  197. {
  198. i2c_del_driver(&max17042_i2c_driver);
  199. }
  200. module_exit(max17042_exit);
  201. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  202. MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
  203. MODULE_LICENSE("GPL");