max8997_charger.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * max8997_charger.c - Power supply consumer driver for the Maxim 8997/8966
  3. *
  4. * Copyright (C) 2011 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/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/power_supply.h>
  26. #include <linux/mfd/max8997.h>
  27. #include <linux/mfd/max8997-private.h>
  28. struct charger_data {
  29. struct device *dev;
  30. struct max8997_dev *iodev;
  31. struct power_supply battery;
  32. };
  33. static enum power_supply_property max8997_battery_props[] = {
  34. POWER_SUPPLY_PROP_STATUS, /* "FULL" or "NOT FULL" only. */
  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 max8997_battery_get_property(struct power_supply *psy,
  40. enum power_supply_property psp,
  41. union power_supply_propval *val)
  42. {
  43. struct charger_data *charger = container_of(psy,
  44. struct charger_data, battery);
  45. struct i2c_client *i2c = charger->iodev->i2c;
  46. int ret;
  47. u8 reg;
  48. switch (psp) {
  49. case POWER_SUPPLY_PROP_STATUS:
  50. val->intval = 0;
  51. ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
  52. if (ret)
  53. return ret;
  54. if ((reg & (1 << 0)) == 0x1)
  55. val->intval = POWER_SUPPLY_STATUS_FULL;
  56. break;
  57. case POWER_SUPPLY_PROP_PRESENT:
  58. val->intval = 0;
  59. ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
  60. if (ret)
  61. return ret;
  62. if ((reg & (1 << 2)) == 0x0)
  63. val->intval = 1;
  64. break;
  65. case POWER_SUPPLY_PROP_ONLINE:
  66. val->intval = 0;
  67. ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
  68. if (ret)
  69. return ret;
  70. /* DCINOK */
  71. if (reg & (1 << 1))
  72. val->intval = 1;
  73. break;
  74. default:
  75. return -EINVAL;
  76. }
  77. return 0;
  78. }
  79. static __devinit int max8997_battery_probe(struct platform_device *pdev)
  80. {
  81. int ret = 0;
  82. struct charger_data *charger;
  83. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  84. struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
  85. if (!pdata)
  86. return -EINVAL;
  87. if (pdata->eoc_mA) {
  88. int val = (pdata->eoc_mA - 50) / 10;
  89. if (val < 0)
  90. val = 0;
  91. if (val > 0xf)
  92. val = 0xf;
  93. ret = max8997_update_reg(iodev->i2c,
  94. MAX8997_REG_MBCCTRL5, val, 0xf);
  95. if (ret < 0) {
  96. dev_err(&pdev->dev, "Cannot use i2c bus.\n");
  97. return ret;
  98. }
  99. }
  100. switch (pdata->timeout) {
  101. case 5:
  102. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  103. 0x2 << 4, 0x7 << 4);
  104. break;
  105. case 6:
  106. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  107. 0x3 << 4, 0x7 << 4);
  108. break;
  109. case 7:
  110. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  111. 0x4 << 4, 0x7 << 4);
  112. break;
  113. case 0:
  114. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  115. 0x7 << 4, 0x7 << 4);
  116. break;
  117. default:
  118. dev_err(&pdev->dev, "incorrect timeout value (%d)\n",
  119. pdata->timeout);
  120. return -EINVAL;
  121. }
  122. if (ret < 0) {
  123. dev_err(&pdev->dev, "Cannot use i2c bus.\n");
  124. return ret;
  125. }
  126. charger = kzalloc(sizeof(struct charger_data), GFP_KERNEL);
  127. if (charger == NULL) {
  128. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  129. return -ENOMEM;
  130. }
  131. platform_set_drvdata(pdev, charger);
  132. charger->battery.name = "max8997_pmic";
  133. charger->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  134. charger->battery.get_property = max8997_battery_get_property;
  135. charger->battery.properties = max8997_battery_props;
  136. charger->battery.num_properties = ARRAY_SIZE(max8997_battery_props);
  137. charger->dev = &pdev->dev;
  138. charger->iodev = iodev;
  139. ret = power_supply_register(&pdev->dev, &charger->battery);
  140. if (ret) {
  141. dev_err(&pdev->dev, "failed: power supply register\n");
  142. goto err;
  143. }
  144. return 0;
  145. err:
  146. kfree(charger);
  147. return ret;
  148. }
  149. static int __devexit max8997_battery_remove(struct platform_device *pdev)
  150. {
  151. struct charger_data *charger = platform_get_drvdata(pdev);
  152. power_supply_unregister(&charger->battery);
  153. kfree(charger);
  154. return 0;
  155. }
  156. static const struct platform_device_id max8997_battery_id[] = {
  157. { "max8997-battery", 0 },
  158. { }
  159. };
  160. static struct platform_driver max8997_battery_driver = {
  161. .driver = {
  162. .name = "max8997-battery",
  163. .owner = THIS_MODULE,
  164. },
  165. .probe = max8997_battery_probe,
  166. .remove = __devexit_p(max8997_battery_remove),
  167. .id_table = max8997_battery_id,
  168. };
  169. static int __init max8997_battery_init(void)
  170. {
  171. return platform_driver_register(&max8997_battery_driver);
  172. }
  173. subsys_initcall(max8997_battery_init);
  174. static void __exit max8997_battery_cleanup(void)
  175. {
  176. platform_driver_unregister(&max8997_battery_driver);
  177. }
  178. module_exit(max8997_battery_cleanup);
  179. MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
  180. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  181. MODULE_LICENSE("GPL");