pmic_max8997.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics
  3. * Lukasz Majewski <l.majewski@samsung.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <power/pmic.h>
  25. #include <power/max8997_pmic.h>
  26. #include <i2c.h>
  27. #include <errno.h>
  28. unsigned char max8997_reg_ldo(int uV)
  29. {
  30. unsigned char ret;
  31. if (uV <= 800000)
  32. return 0;
  33. if (uV >= 3950000)
  34. return MAX8997_LDO_MAX_VAL;
  35. ret = (uV - 800000) / 50000;
  36. if (ret > MAX8997_LDO_MAX_VAL) {
  37. printf("MAX8997 LDO SETTING ERROR (%duV) -> %u\n", uV, ret);
  38. ret = MAX8997_LDO_MAX_VAL;
  39. }
  40. return ret;
  41. }
  42. static int pmic_charger_state(struct pmic *p, int state, int current)
  43. {
  44. unsigned char fc;
  45. u32 val = 0;
  46. if (pmic_probe(p))
  47. return -1;
  48. if (state == CHARGER_DISABLE) {
  49. puts("Disable the charger.\n");
  50. pmic_reg_read(p, MAX8997_REG_MBCCTRL2, &val);
  51. val &= ~(MBCHOSTEN | VCHGR_FC);
  52. pmic_reg_write(p, MAX8997_REG_MBCCTRL2, val);
  53. return -1;
  54. }
  55. if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) {
  56. printf("%s: Wrong charge current: %d [mA]\n",
  57. __func__, current);
  58. return -1;
  59. }
  60. fc = (current - CHARGER_MIN_CURRENT) / CHARGER_CURRENT_RESOLUTION;
  61. fc = fc & 0xf; /* up to 950 mA */
  62. printf("Enable the charger @ %d [mA]\n", fc * CHARGER_CURRENT_RESOLUTION
  63. + CHARGER_MIN_CURRENT);
  64. val = fc | MBCICHFCSET;
  65. pmic_reg_write(p, MAX8997_REG_MBCCTRL4, val);
  66. pmic_reg_read(p, MAX8997_REG_MBCCTRL2, &val);
  67. val = MBCHOSTEN | VCHGR_FC; /* enable charger & fast charge */
  68. pmic_reg_write(p, MAX8997_REG_MBCCTRL2, val);
  69. return 0;
  70. }
  71. static int pmic_charger_bat_present(struct pmic *p)
  72. {
  73. u32 val;
  74. if (pmic_probe(p))
  75. return -1;
  76. pmic_reg_read(p, MAX8997_REG_STATUS4, &val);
  77. return !(val & DETBAT);
  78. }
  79. static struct power_chrg power_chrg_pmic_ops = {
  80. .chrg_bat_present = pmic_charger_bat_present,
  81. .chrg_state = pmic_charger_state,
  82. };
  83. int pmic_init(unsigned char bus)
  84. {
  85. static const char name[] = "MAX8997_PMIC";
  86. struct pmic *p = pmic_alloc();
  87. if (!p) {
  88. printf("%s: POWER allocation error!\n", __func__);
  89. return -ENOMEM;
  90. }
  91. debug("Board PMIC init\n");
  92. p->name = name;
  93. p->interface = PMIC_I2C;
  94. p->number_of_regs = PMIC_NUM_OF_REGS;
  95. p->hw.i2c.addr = MAX8997_I2C_ADDR;
  96. p->hw.i2c.tx_num = 1;
  97. p->bus = bus;
  98. p->chrg = &power_chrg_pmic_ops;
  99. return 0;
  100. }