pmic_tps65217.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2011-2013
  4. * Texas Instruments, <www.ti.com>
  5. */
  6. #include <common.h>
  7. #include <i2c.h>
  8. #include <power/tps65217.h>
  9. struct udevice *tps65217_dev __attribute__((section(".data"))) = NULL;
  10. /**
  11. * tps65217_reg_read() - Generic function that can read a TPS65217 register
  12. * @src_reg: Source register address
  13. * @src_val: Address of destination variable
  14. * @return: 0 for success, not 0 on failure.
  15. */
  16. int tps65217_reg_read(uchar src_reg, uchar *src_val)
  17. {
  18. #if !CONFIG_IS_ENABLED(DM_I2C)
  19. return i2c_read(TPS65217_CHIP_PM, src_reg, 1, src_val, 1);
  20. #else
  21. return dm_i2c_read(tps65217_dev, src_reg, src_val, 1);
  22. #endif
  23. }
  24. /**
  25. * tps65217_reg_write() - Generic function that can write a TPS65217 PMIC
  26. * register or bit field regardless of protection
  27. * level.
  28. *
  29. * @prot_level: Register password protection. Use
  30. * TPS65217_PROT_LEVEL_NONE,
  31. * TPS65217_PROT_LEVEL_1 or TPS65217_PROT_LEVEL_2
  32. * @dest_reg: Register address to write.
  33. * @dest_val: Value to write.
  34. * @mask: Bit mask (8 bits) to be applied. Function will only
  35. * change bits that are set in the bit mask.
  36. *
  37. * @return: 0 for success, not 0 on failure, as per the i2c API
  38. */
  39. int tps65217_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
  40. uchar mask)
  41. {
  42. uchar read_val;
  43. uchar xor_reg;
  44. int ret;
  45. /*
  46. * If we are affecting only a bit field, read dest_reg and apply the
  47. * mask
  48. */
  49. if (mask != TPS65217_MASK_ALL_BITS) {
  50. #if !CONFIG_IS_ENABLED(DM_I2C)
  51. ret = i2c_read(TPS65217_CHIP_PM, dest_reg, 1, &read_val, 1);
  52. #else
  53. ret = dm_i2c_read(tps65217_dev, dest_reg, &read_val, 1);
  54. #endif
  55. if (ret)
  56. return ret;
  57. read_val &= (~mask);
  58. read_val |= (dest_val & mask);
  59. dest_val = read_val;
  60. }
  61. if (prot_level > 0) {
  62. xor_reg = dest_reg ^ TPS65217_PASSWORD_UNLOCK;
  63. #if !CONFIG_IS_ENABLED(DM_I2C)
  64. ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
  65. &xor_reg, 1);
  66. #else
  67. ret = dm_i2c_write(tps65217_dev, TPS65217_PASSWORD,
  68. &xor_reg, 1);
  69. #endif
  70. if (ret)
  71. return ret;
  72. }
  73. #if !CONFIG_IS_ENABLED(DM_I2C)
  74. ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
  75. #else
  76. ret = dm_i2c_write(tps65217_dev, dest_reg, &dest_val, 1);
  77. #endif
  78. if (ret)
  79. return ret;
  80. if (prot_level == TPS65217_PROT_LEVEL_2) {
  81. #if !CONFIG_IS_ENABLED(DM_I2C)
  82. ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
  83. &xor_reg, 1);
  84. #else
  85. ret = dm_i2c_write(tps65217_dev, TPS65217_PASSWORD,
  86. &xor_reg, 1);
  87. #endif
  88. if (ret)
  89. return ret;
  90. #if !CONFIG_IS_ENABLED(DM_I2C)
  91. ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
  92. #else
  93. ret = dm_i2c_write(tps65217_dev, dest_reg, &dest_val, 1);
  94. #endif
  95. if (ret)
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. /**
  101. * tps65217_voltage_update() - Function to change a voltage level, as this
  102. * is a multi-step process.
  103. * @dc_cntrl_reg: DC voltage control register to change.
  104. * @volt_sel: New value for the voltage register
  105. * @return: 0 for success, not 0 on failure.
  106. */
  107. int tps65217_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
  108. {
  109. if ((dc_cntrl_reg != TPS65217_DEFDCDC1) &&
  110. (dc_cntrl_reg != TPS65217_DEFDCDC2) &&
  111. (dc_cntrl_reg != TPS65217_DEFDCDC3))
  112. return 1;
  113. /* set voltage level */
  114. if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
  115. TPS65217_MASK_ALL_BITS))
  116. return 1;
  117. /* set GO bit to initiate voltage transition */
  118. if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, TPS65217_DEFSLEW,
  119. TPS65217_DCDC_GO, TPS65217_DCDC_GO))
  120. return 1;
  121. return 0;
  122. }
  123. int power_tps65217_init(unsigned char bus)
  124. {
  125. #if CONFIG_IS_ENABLED(DM_I2C)
  126. struct udevice *dev = NULL;
  127. int rc;
  128. rc = i2c_get_chip_for_busnum(bus, TPS65217_CHIP_PM, 1, &dev);
  129. if (rc)
  130. return rc;
  131. tps65217_dev = dev;
  132. #endif
  133. return 0;
  134. }