max8998_charger.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/mfd/max8998.h>
  26. #include <linux/mfd/max8998-private.h>
  27. struct max8998_battery_data {
  28. struct device *dev;
  29. struct max8998_dev *iodev;
  30. struct power_supply battery;
  31. };
  32. static enum power_supply_property max8998_battery_props[] = {
  33. POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
  34. POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
  35. };
  36. /* Note that the charger control is done by a current regulator "CHARGER" */
  37. static int max8998_battery_get_property(struct power_supply *psy,
  38. enum power_supply_property psp,
  39. union power_supply_propval *val)
  40. {
  41. struct max8998_battery_data *max8998 = container_of(psy,
  42. struct max8998_battery_data, battery);
  43. struct i2c_client *i2c = max8998->iodev->i2c;
  44. int ret;
  45. u8 reg;
  46. switch (psp) {
  47. case POWER_SUPPLY_PROP_PRESENT:
  48. ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
  49. if (ret)
  50. return ret;
  51. if (reg & (1 << 4))
  52. val->intval = 0;
  53. else
  54. val->intval = 1;
  55. break;
  56. case POWER_SUPPLY_PROP_ONLINE:
  57. ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
  58. if (ret)
  59. return ret;
  60. if (reg & (1 << 3))
  61. val->intval = 0;
  62. else
  63. val->intval = 1;
  64. break;
  65. default:
  66. return -EINVAL;
  67. }
  68. return 0;
  69. }
  70. static __devinit int max8998_battery_probe(struct platform_device *pdev)
  71. {
  72. struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  73. struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
  74. struct max8998_battery_data *max8998;
  75. struct i2c_client *i2c;
  76. int ret = 0;
  77. if (!pdata) {
  78. dev_err(pdev->dev.parent, "No platform init data supplied\n");
  79. return -ENODEV;
  80. }
  81. max8998 = kzalloc(sizeof(struct max8998_battery_data), GFP_KERNEL);
  82. if (!max8998)
  83. return -ENOMEM;
  84. max8998->dev = &pdev->dev;
  85. max8998->iodev = iodev;
  86. platform_set_drvdata(pdev, max8998);
  87. i2c = max8998->iodev->i2c;
  88. /* Setup "End of Charge" */
  89. /* If EOC value equals 0,
  90. * remain value set from bootloader or default value */
  91. if (pdata->eoc >= 10 && pdata->eoc <= 45) {
  92. max8998_update_reg(i2c, MAX8998_REG_CHGR1,
  93. (pdata->eoc / 5 - 2) << 5, 0x7 << 5);
  94. } else if (pdata->eoc == 0) {
  95. dev_dbg(max8998->dev,
  96. "EOC value not set: leave it unchanged.\n");
  97. } else {
  98. dev_err(max8998->dev, "Invalid EOC value\n");
  99. ret = -EINVAL;
  100. goto err;
  101. }
  102. /* Setup Charge Restart Level */
  103. switch (pdata->restart) {
  104. case 100:
  105. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
  106. break;
  107. case 150:
  108. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
  109. break;
  110. case 200:
  111. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
  112. break;
  113. case -1:
  114. max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
  115. break;
  116. case 0:
  117. dev_dbg(max8998->dev,
  118. "Restart Level not set: leave it unchanged.\n");
  119. break;
  120. default:
  121. dev_err(max8998->dev, "Invalid Restart Level\n");
  122. ret = -EINVAL;
  123. goto err;
  124. }
  125. /* Setup Charge Full Timeout */
  126. switch (pdata->timeout) {
  127. case 5:
  128. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
  129. break;
  130. case 6:
  131. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
  132. break;
  133. case 7:
  134. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
  135. break;
  136. case -1:
  137. max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
  138. break;
  139. case 0:
  140. dev_dbg(max8998->dev,
  141. "Full Timeout not set: leave it unchanged.\n");
  142. default:
  143. dev_err(max8998->dev, "Invalid Full Timeout value\n");
  144. ret = -EINVAL;
  145. goto err;
  146. }
  147. max8998->battery.name = "max8998_pmic";
  148. max8998->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  149. max8998->battery.get_property = max8998_battery_get_property;
  150. max8998->battery.properties = max8998_battery_props;
  151. max8998->battery.num_properties = ARRAY_SIZE(max8998_battery_props);
  152. ret = power_supply_register(max8998->dev, &max8998->battery);
  153. if (ret) {
  154. dev_err(max8998->dev, "failed: power supply register\n");
  155. goto err;
  156. }
  157. return 0;
  158. err:
  159. kfree(max8998);
  160. return ret;
  161. }
  162. static int __devexit max8998_battery_remove(struct platform_device *pdev)
  163. {
  164. struct max8998_battery_data *max8998 = platform_get_drvdata(pdev);
  165. power_supply_unregister(&max8998->battery);
  166. kfree(max8998);
  167. return 0;
  168. }
  169. static const struct platform_device_id max8998_battery_id[] = {
  170. { "max8998-battery", TYPE_MAX8998 },
  171. };
  172. static struct platform_driver max8998_battery_driver = {
  173. .driver = {
  174. .name = "max8998-battery",
  175. .owner = THIS_MODULE,
  176. },
  177. .probe = max8998_battery_probe,
  178. .remove = __devexit_p(max8998_battery_remove),
  179. .id_table = max8998_battery_id,
  180. };
  181. static int __init max8998_battery_init(void)
  182. {
  183. return platform_driver_register(&max8998_battery_driver);
  184. }
  185. module_init(max8998_battery_init);
  186. static void __exit max8998_battery_cleanup(void)
  187. {
  188. platform_driver_unregister(&max8998_battery_driver);
  189. }
  190. module_exit(max8998_battery_cleanup);
  191. MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
  192. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  193. MODULE_LICENSE("GPL");
  194. MODULE_ALIAS("platform:max8998-battery");