pmic_bus.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
  3. *
  4. * Sunxi PMIC bus access helpers
  5. *
  6. * The axp152 & axp209 use an i2c bus, the axp221 uses the p2wi bus and the
  7. * axp223 uses the rsb bus, these functions abstract this.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <asm/arch/p2wi.h>
  13. #include <asm/arch/rsb.h>
  14. #include <i2c.h>
  15. #include <asm/arch/pmic_bus.h>
  16. #define AXP152_I2C_ADDR 0x30
  17. #define AXP209_I2C_ADDR 0x34
  18. #define AXP221_CHIP_ADDR 0x68
  19. #define AXP221_CTRL_ADDR 0x3e
  20. #define AXP221_INIT_DATA 0x3e
  21. /* AXP818 device and runtime addresses are same as AXP223 */
  22. #define AXP223_DEVICE_ADDR 0x3a3
  23. #define AXP223_RUNTIME_ADDR 0x2d
  24. int pmic_bus_init(void)
  25. {
  26. /* This cannot be 0 because it is used in SPL before BSS is ready */
  27. static int needs_init = 1;
  28. __maybe_unused int ret;
  29. if (!needs_init)
  30. return 0;
  31. #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
  32. # ifdef CONFIG_MACH_SUN6I
  33. p2wi_init();
  34. ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
  35. AXP221_INIT_DATA);
  36. # else
  37. ret = rsb_init();
  38. if (ret)
  39. return ret;
  40. ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
  41. # endif
  42. if (ret)
  43. return ret;
  44. #endif
  45. needs_init = 0;
  46. return 0;
  47. }
  48. int pmic_bus_read(u8 reg, u8 *data)
  49. {
  50. #ifdef CONFIG_AXP152_POWER
  51. return i2c_read(AXP152_I2C_ADDR, reg, 1, data, 1);
  52. #elif defined CONFIG_AXP209_POWER
  53. return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
  54. #elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
  55. # ifdef CONFIG_MACH_SUN6I
  56. return p2wi_read(reg, data);
  57. # else
  58. return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
  59. # endif
  60. #endif
  61. }
  62. int pmic_bus_write(u8 reg, u8 data)
  63. {
  64. #ifdef CONFIG_AXP152_POWER
  65. return i2c_write(AXP152_I2C_ADDR, reg, 1, &data, 1);
  66. #elif defined CONFIG_AXP209_POWER
  67. return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
  68. #elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
  69. # ifdef CONFIG_MACH_SUN6I
  70. return p2wi_write(reg, data);
  71. # else
  72. return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
  73. # endif
  74. #endif
  75. }
  76. int pmic_bus_setbits(u8 reg, u8 bits)
  77. {
  78. int ret;
  79. u8 val;
  80. ret = pmic_bus_read(reg, &val);
  81. if (ret)
  82. return ret;
  83. val |= bits;
  84. return pmic_bus_write(reg, val);
  85. }
  86. int pmic_bus_clrbits(u8 reg, u8 bits)
  87. {
  88. int ret;
  89. u8 val;
  90. ret = pmic_bus_read(reg, &val);
  91. if (ret)
  92. return ret;
  93. val &= ~bits;
  94. return pmic_bus_write(reg, val);
  95. }