max8997_charger.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/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/max8997.h>
  28. #include <linux/mfd/max8997-private.h>
  29. struct charger_data {
  30. struct device *dev;
  31. struct max8997_dev *iodev;
  32. struct power_supply battery;
  33. };
  34. static enum power_supply_property max8997_battery_props[] = {
  35. POWER_SUPPLY_PROP_STATUS, /* "FULL" or "NOT FULL" only. */
  36. POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
  37. POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
  38. };
  39. /* Note that the charger control is done by a current regulator "CHARGER" */
  40. static int max8997_battery_get_property(struct power_supply *psy,
  41. enum power_supply_property psp,
  42. union power_supply_propval *val)
  43. {
  44. struct charger_data *charger = container_of(psy,
  45. struct charger_data, battery);
  46. struct i2c_client *i2c = charger->iodev->i2c;
  47. int ret;
  48. u8 reg;
  49. switch (psp) {
  50. case POWER_SUPPLY_PROP_STATUS:
  51. val->intval = 0;
  52. ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
  53. if (ret)
  54. return ret;
  55. if ((reg & (1 << 0)) == 0x1)
  56. val->intval = POWER_SUPPLY_STATUS_FULL;
  57. break;
  58. case POWER_SUPPLY_PROP_PRESENT:
  59. val->intval = 0;
  60. ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
  61. if (ret)
  62. return ret;
  63. if ((reg & (1 << 2)) == 0x0)
  64. val->intval = 1;
  65. break;
  66. case POWER_SUPPLY_PROP_ONLINE:
  67. val->intval = 0;
  68. ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
  69. if (ret)
  70. return ret;
  71. /* DCINOK */
  72. if (reg & (1 << 1))
  73. val->intval = 1;
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. static __devinit int max8997_battery_probe(struct platform_device *pdev)
  81. {
  82. int ret = 0;
  83. struct charger_data *charger;
  84. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  85. struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
  86. if (!pdata)
  87. return -EINVAL;
  88. if (pdata->eoc_mA) {
  89. u8 val = (pdata->eoc_mA - 50) / 10;
  90. if (val < 0)
  91. val = 0;
  92. if (val > 0xf)
  93. val = 0xf;
  94. ret = max8997_update_reg(iodev->i2c,
  95. MAX8997_REG_MBCCTRL5, val, 0xf);
  96. if (ret < 0) {
  97. dev_err(&pdev->dev, "Cannot use i2c bus.\n");
  98. return ret;
  99. }
  100. }
  101. switch (pdata->timeout) {
  102. case 5:
  103. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  104. 0x2 << 4, 0x7 << 4);
  105. break;
  106. case 6:
  107. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  108. 0x3 << 4, 0x7 << 4);
  109. break;
  110. case 7:
  111. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  112. 0x4 << 4, 0x7 << 4);
  113. break;
  114. case 0:
  115. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  116. 0x7 << 4, 0x7 << 4);
  117. break;
  118. default:
  119. dev_err(&pdev->dev, "incorrect timeout value (%d)\n",
  120. pdata->timeout);
  121. return -EINVAL;
  122. }
  123. if (ret < 0) {
  124. dev_err(&pdev->dev, "Cannot use i2c bus.\n");
  125. return ret;
  126. }
  127. charger = kzalloc(sizeof(struct charger_data), GFP_KERNEL);
  128. if (charger == NULL) {
  129. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  130. return -ENOMEM;
  131. }
  132. platform_set_drvdata(pdev, charger);
  133. charger->battery.name = "max8997_pmic";
  134. charger->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  135. charger->battery.get_property = max8997_battery_get_property;
  136. charger->battery.properties = max8997_battery_props;
  137. charger->battery.num_properties = ARRAY_SIZE(max8997_battery_props);
  138. charger->dev = &pdev->dev;
  139. charger->iodev = iodev;
  140. ret = power_supply_register(&pdev->dev, &charger->battery);
  141. if (ret) {
  142. dev_err(&pdev->dev, "failed: power supply register\n");
  143. goto err;
  144. }
  145. return 0;
  146. err:
  147. kfree(charger);
  148. return ret;
  149. }
  150. static int __devexit max8997_battery_remove(struct platform_device *pdev)
  151. {
  152. struct charger_data *charger = platform_get_drvdata(pdev);
  153. power_supply_unregister(&charger->battery);
  154. kfree(charger);
  155. return 0;
  156. }
  157. static const struct platform_device_id max8997_battery_id[] = {
  158. { "max8997-battery", 0 },
  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");