max8998_charger.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974
  3. *
  4. * Copyright (C) 2009-2010 Samsung Electronics
  5. * MyungJoo Ham <myungjoo.ham@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/err.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/power_supply.h>
  27. #include <linux/mfd/max8998.h>
  28. #include <linux/mfd/max8998-private.h>
  29. struct max8998_battery_data {
  30. struct device *dev;
  31. struct max8998_dev *iodev;
  32. struct power_supply battery;
  33. };
  34. static enum power_supply_property max8998_battery_props[] = {
  35. POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
  36. POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
  37. };
  38. /* Note that the charger control is done by a current regulator "CHARGER" */
  39. static int max8998_battery_get_property(struct power_supply *psy,
  40. enum power_supply_property psp,
  41. union power_supply_propval *val)
  42. {
  43. struct max8998_battery_data *max8998 = container_of(psy,
  44. struct max8998_battery_data, battery);
  45. struct i2c_client *i2c = max8998->iodev->i2c;
  46. int ret;
  47. u8 reg;
  48. switch (psp) {
  49. case POWER_SUPPLY_PROP_PRESENT:
  50. ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
  51. if (ret)
  52. return ret;
  53. if (reg & (1 << 4))
  54. val->intval = 0;
  55. else
  56. val->intval = 1;
  57. break;
  58. case POWER_SUPPLY_PROP_ONLINE:
  59. ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
  60. if (ret)
  61. return ret;
  62. if (reg & (1 << 3))
  63. val->intval = 0;
  64. else
  65. val->intval = 1;
  66. break;
  67. default:
  68. return -EINVAL;
  69. }
  70. return 0;
  71. }
  72. static __devinit int max8998_battery_probe(struct platform_device *pdev)
  73. {
  74. struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  75. struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
  76. struct max8998_battery_data *max8998;
  77. struct i2c_client *i2c;
  78. int ret = 0;
  79. if (!pdata) {
  80. dev_err(pdev->dev.parent, "No platform init data supplied\n");
  81. return -ENODEV;
  82. }
  83. max8998 = kzalloc(sizeof(struct max8998_battery_data), GFP_KERNEL);
  84. if (!max8998)
  85. return -ENOMEM;
  86. max8998->dev = &pdev->dev;
  87. max8998->iodev = iodev;
  88. platform_set_drvdata(pdev, max8998);
  89. i2c = max8998->iodev->i2c;
  90. /* Setup "End of Charge" */
  91. /* If EOC value equals 0,
  92. * remain value set from bootloader or default value */
  93. if (pdata->eoc >= 10 && pdata->eoc <= 45) {
  94. max8998_update_reg(i2c, MAX8998_REG_CHGR1,
  95. (pdata->eoc / 5 - 2) << 5, 0x7 << 5);
  96. } else if (pdata->eoc == 0) {
  97. dev_dbg(max8998->dev,
  98. "EOC value not set: leave it unchanged.\n");
  99. } else {
  100. dev_err(max8998->dev, "Invalid EOC value\n");
  101. ret = -EINVAL;
  102. goto err;
  103. }
  104. /* Setup Charge Restart Level */
  105. switch (pdata->restart) {
  106. case 100:
  107. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
  108. break;
  109. case 150:
  110. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
  111. break;
  112. case 200:
  113. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
  114. break;
  115. case -1:
  116. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
  117. break;
  118. case 0:
  119. dev_dbg(max8998->dev,
  120. "Restart Level not set: leave it unchanged.\n");
  121. break;
  122. default:
  123. dev_err(max8998->dev, "Invalid Restart Level\n");
  124. ret = -EINVAL;
  125. goto err;
  126. }
  127. /* Setup Charge Full Timeout */
  128. switch (pdata->timeout) {
  129. case 5:
  130. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
  131. break;
  132. case 6:
  133. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
  134. break;
  135. case 7:
  136. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
  137. break;
  138. case -1:
  139. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
  140. break;
  141. case 0:
  142. dev_dbg(max8998->dev,
  143. "Full Timeout not set: leave it unchanged.\n");
  144. break;
  145. default:
  146. dev_err(max8998->dev, "Invalid Full Timeout value\n");
  147. ret = -EINVAL;
  148. goto err;
  149. }
  150. max8998->battery.name = "max8998_pmic";
  151. max8998->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  152. max8998->battery.get_property = max8998_battery_get_property;
  153. max8998->battery.properties = max8998_battery_props;
  154. max8998->battery.num_properties = ARRAY_SIZE(max8998_battery_props);
  155. ret = power_supply_register(max8998->dev, &max8998->battery);
  156. if (ret) {
  157. dev_err(max8998->dev, "failed: power supply register\n");
  158. goto err;
  159. }
  160. return 0;
  161. err:
  162. kfree(max8998);
  163. return ret;
  164. }
  165. static int __devexit max8998_battery_remove(struct platform_device *pdev)
  166. {
  167. struct max8998_battery_data *max8998 = platform_get_drvdata(pdev);
  168. power_supply_unregister(&max8998->battery);
  169. kfree(max8998);
  170. return 0;
  171. }
  172. static const struct platform_device_id max8998_battery_id[] = {
  173. { "max8998-battery", TYPE_MAX8998 },
  174. { }
  175. };
  176. static struct platform_driver max8998_battery_driver = {
  177. .driver = {
  178. .name = "max8998-battery",
  179. .owner = THIS_MODULE,
  180. },
  181. .probe = max8998_battery_probe,
  182. .remove = __devexit_p(max8998_battery_remove),
  183. .id_table = max8998_battery_id,
  184. };
  185. module_platform_driver(max8998_battery_driver);
  186. MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
  187. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  188. MODULE_LICENSE("GPL");
  189. MODULE_ALIAS("platform:max8998-battery");