max8997_charger.c 5.3 KB

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