axp209.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * (C) Copyright 2012
  3. * Henrik Nordstrom <henrik@henriknordstrom.net>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #include <axp209.h>
  10. enum axp209_reg {
  11. AXP209_POWER_STATUS = 0x00,
  12. AXP209_CHIP_VERSION = 0x03,
  13. AXP209_DCDC2_VOLTAGE = 0x23,
  14. AXP209_DCDC3_VOLTAGE = 0x27,
  15. AXP209_LDO24_VOLTAGE = 0x28,
  16. AXP209_LDO3_VOLTAGE = 0x29,
  17. AXP209_IRQ_STATUS5 = 0x4c,
  18. AXP209_SHUTDOWN = 0x32,
  19. };
  20. #define AXP209_POWER_STATUS_ON_BY_DC (1 << 0)
  21. #define AXP209_IRQ5_PEK_UP (1 << 6)
  22. #define AXP209_IRQ5_PEK_DOWN (1 << 5)
  23. #define AXP209_POWEROFF (1 << 7)
  24. static int axp209_write(enum axp209_reg reg, u8 val)
  25. {
  26. return i2c_write(0x34, reg, 1, &val, 1);
  27. }
  28. static int axp209_read(enum axp209_reg reg, u8 *val)
  29. {
  30. return i2c_read(0x34, reg, 1, val, 1);
  31. }
  32. static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div)
  33. {
  34. if (mvolt < min)
  35. mvolt = min;
  36. else if (mvolt > max)
  37. mvolt = max;
  38. return (mvolt - min) / div;
  39. }
  40. int axp209_set_dcdc2(int mvolt)
  41. {
  42. int rc;
  43. u8 cfg, current;
  44. cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
  45. /* Do we really need to be this gentle? It has built-in voltage slope */
  46. while ((rc = axp209_read(AXP209_DCDC2_VOLTAGE, &current)) == 0 &&
  47. current != cfg) {
  48. if (current < cfg)
  49. current++;
  50. else
  51. current--;
  52. rc = axp209_write(AXP209_DCDC2_VOLTAGE, current);
  53. if (rc)
  54. break;
  55. }
  56. return rc;
  57. }
  58. int axp209_set_dcdc3(int mvolt)
  59. {
  60. u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
  61. return axp209_write(AXP209_DCDC3_VOLTAGE, cfg);
  62. }
  63. int axp209_set_ldo2(int mvolt)
  64. {
  65. int rc;
  66. u8 cfg, reg;
  67. cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
  68. rc = axp209_read(AXP209_LDO24_VOLTAGE, &reg);
  69. if (rc)
  70. return rc;
  71. /* LDO2 configuration is in upper 4 bits */
  72. reg = (reg & 0x0f) | (cfg << 4);
  73. return axp209_write(AXP209_LDO24_VOLTAGE, reg);
  74. }
  75. int axp209_set_ldo3(int mvolt)
  76. {
  77. u8 cfg;
  78. if (mvolt == -1)
  79. cfg = 0x80; /* determined by LDO3IN pin */
  80. else
  81. cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
  82. return axp209_write(AXP209_LDO3_VOLTAGE, cfg);
  83. }
  84. int axp209_set_ldo4(int mvolt)
  85. {
  86. int rc;
  87. static const int vindex[] = {
  88. 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
  89. 2700, 2800, 3000, 3100, 3200, 3300
  90. };
  91. u8 cfg, reg;
  92. /* Translate mvolt to register cfg value, requested <= selected */
  93. for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
  94. rc = axp209_read(AXP209_LDO24_VOLTAGE, &reg);
  95. if (rc)
  96. return rc;
  97. /* LDO4 configuration is in lower 4 bits */
  98. reg = (reg & 0xf0) | (cfg << 0);
  99. return axp209_write(AXP209_LDO24_VOLTAGE, reg);
  100. }
  101. int axp209_init(void)
  102. {
  103. u8 ver;
  104. int rc;
  105. rc = axp209_read(AXP209_CHIP_VERSION, &ver);
  106. if (rc)
  107. return rc;
  108. /* Low 4 bits is chip version */
  109. ver &= 0x0f;
  110. if (ver != 0x1)
  111. return -1;
  112. return 0;
  113. }
  114. int axp209_poweron_by_dc(void)
  115. {
  116. u8 v;
  117. if (axp209_read(AXP209_POWER_STATUS, &v))
  118. return 0;
  119. return (v & AXP209_POWER_STATUS_ON_BY_DC);
  120. }
  121. int axp209_power_button(void)
  122. {
  123. u8 v;
  124. if (axp209_read(AXP209_IRQ_STATUS5, &v))
  125. return 0;
  126. axp209_write(AXP209_IRQ_STATUS5, AXP209_IRQ5_PEK_DOWN);
  127. return v & AXP209_IRQ5_PEK_DOWN;
  128. }