max8997_charger.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 = power_supply_get_drvdata(psy);
  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 const struct power_supply_desc max8997_battery_desc = {
  79. .name = "max8997_pmic",
  80. .type = POWER_SUPPLY_TYPE_BATTERY,
  81. .get_property = max8997_battery_get_property,
  82. .properties = max8997_battery_props,
  83. .num_properties = ARRAY_SIZE(max8997_battery_props),
  84. };
  85. static int max8997_battery_probe(struct platform_device *pdev)
  86. {
  87. int ret = 0;
  88. struct charger_data *charger;
  89. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  90. struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
  91. struct power_supply_config psy_cfg = {};
  92. if (!pdata)
  93. return -EINVAL;
  94. if (pdata->eoc_mA) {
  95. int val = (pdata->eoc_mA - 50) / 10;
  96. if (val < 0)
  97. val = 0;
  98. if (val > 0xf)
  99. val = 0xf;
  100. ret = max8997_update_reg(iodev->i2c,
  101. MAX8997_REG_MBCCTRL5, val, 0xf);
  102. if (ret < 0) {
  103. dev_err(&pdev->dev, "Cannot use i2c bus.\n");
  104. return ret;
  105. }
  106. }
  107. switch (pdata->timeout) {
  108. case 5:
  109. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  110. 0x2 << 4, 0x7 << 4);
  111. break;
  112. case 6:
  113. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  114. 0x3 << 4, 0x7 << 4);
  115. break;
  116. case 7:
  117. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  118. 0x4 << 4, 0x7 << 4);
  119. break;
  120. case 0:
  121. ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1,
  122. 0x7 << 4, 0x7 << 4);
  123. break;
  124. default:
  125. dev_err(&pdev->dev, "incorrect timeout value (%d)\n",
  126. pdata->timeout);
  127. return -EINVAL;
  128. }
  129. if (ret < 0) {
  130. dev_err(&pdev->dev, "Cannot use i2c bus.\n");
  131. return ret;
  132. }
  133. charger = devm_kzalloc(&pdev->dev, sizeof(struct charger_data),
  134. GFP_KERNEL);
  135. if (charger == NULL) {
  136. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  137. return -ENOMEM;
  138. }
  139. platform_set_drvdata(pdev, charger);
  140. charger->dev = &pdev->dev;
  141. charger->iodev = iodev;
  142. psy_cfg.drv_data = charger;
  143. charger->battery = power_supply_register(&pdev->dev,
  144. &max8997_battery_desc,
  145. &psy_cfg);
  146. if (IS_ERR(charger->battery)) {
  147. dev_err(&pdev->dev, "failed: power supply register\n");
  148. return PTR_ERR(charger->battery);
  149. }
  150. return 0;
  151. }
  152. static int max8997_battery_remove(struct platform_device *pdev)
  153. {
  154. struct charger_data *charger = platform_get_drvdata(pdev);
  155. power_supply_unregister(charger->battery);
  156. return 0;
  157. }
  158. static const struct platform_device_id max8997_battery_id[] = {
  159. { "max8997-battery", 0 },
  160. { }
  161. };
  162. static struct platform_driver max8997_battery_driver = {
  163. .driver = {
  164. .name = "max8997-battery",
  165. },
  166. .probe = max8997_battery_probe,
  167. .remove = max8997_battery_remove,
  168. .id_table = max8997_battery_id,
  169. };
  170. static int __init max8997_battery_init(void)
  171. {
  172. return platform_driver_register(&max8997_battery_driver);
  173. }
  174. subsys_initcall(max8997_battery_init);
  175. static void __exit max8997_battery_cleanup(void)
  176. {
  177. platform_driver_unregister(&max8997_battery_driver);
  178. }
  179. module_exit(max8997_battery_cleanup);
  180. MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
  181. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  182. MODULE_LICENSE("GPL");