pmic_tps65218.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <linux/errno.h>
  9. #include <power/pmic.h>
  10. #include <power/tps65218.h>
  11. int tps65218_reg_read(uchar dest_reg, uchar *dest_val)
  12. {
  13. uchar read_val;
  14. int ret;
  15. ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
  16. if (ret)
  17. return ret;
  18. *dest_val = read_val;
  19. return 0;
  20. }
  21. /**
  22. * tps65218_reg_write() - Generic function that can write a TPS65218 PMIC
  23. * register or bit field regardless of protection
  24. * level.
  25. *
  26. * @prot_level: Register password protection. Use
  27. * TPS65218_PROT_LEVEL_NONE,
  28. * TPS65218_PROT_LEVEL_1 or TPS65218_PROT_LEVEL_2
  29. * @dest_reg: Register address to write.
  30. * @dest_val: Value to write.
  31. * @mask: Bit mask (8 bits) to be applied. Function will only
  32. * change bits that are set in the bit mask.
  33. *
  34. * @return: 0 for success, not 0 on failure, as per the i2c API
  35. */
  36. int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
  37. uchar mask)
  38. {
  39. uchar read_val;
  40. uchar xor_reg;
  41. int ret;
  42. /*
  43. * If we are affecting only a bit field, read dest_reg and apply the
  44. * mask
  45. */
  46. if (mask != TPS65218_MASK_ALL_BITS) {
  47. ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
  48. if (ret)
  49. return ret;
  50. read_val &= (~mask);
  51. read_val |= (dest_val & mask);
  52. dest_val = read_val;
  53. }
  54. if (prot_level > 0) {
  55. xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK;
  56. ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
  57. &xor_reg, 1);
  58. if (ret)
  59. return ret;
  60. }
  61. ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
  62. if (ret)
  63. return ret;
  64. if (prot_level == TPS65218_PROT_LEVEL_2) {
  65. ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
  66. &xor_reg, 1);
  67. if (ret)
  68. return ret;
  69. ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
  70. if (ret)
  71. return ret;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * tps65218_voltage_update() - Function to change a voltage level, as this
  77. * is a multi-step process.
  78. * @dc_cntrl_reg: DC voltage control register to change.
  79. * @volt_sel: New value for the voltage register
  80. * @return: 0 for success, not 0 on failure.
  81. */
  82. int tps65218_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
  83. {
  84. if ((dc_cntrl_reg != TPS65218_DCDC1) &&
  85. (dc_cntrl_reg != TPS65218_DCDC2) &&
  86. (dc_cntrl_reg != TPS65218_DCDC3))
  87. return 1;
  88. /* set voltage level */
  89. if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
  90. TPS65218_DCDC_VSEL_MASK))
  91. return 1;
  92. /* set GO bit to initiate voltage transition */
  93. if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, TPS65218_SLEW,
  94. TPS65218_DCDC_GO, TPS65218_DCDC_GO))
  95. return 1;
  96. return 0;
  97. }
  98. /**
  99. * tps65218_toggle_fseal() - Perform the sequence that toggles the FSEAL bit.
  100. *
  101. * @return: 0 on success, -EBADE if the sequence was broken
  102. */
  103. int tps65218_toggle_fseal(void)
  104. {
  105. if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
  106. 0xb1, TPS65218_MASK_ALL_BITS))
  107. return -EBADE;
  108. if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
  109. 0xfe, TPS65218_MASK_ALL_BITS))
  110. return -EBADE;
  111. if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
  112. 0xa3, TPS65218_MASK_ALL_BITS))
  113. return -EBADE;
  114. return 0;
  115. }
  116. /**
  117. * tps65218_lock_fseal() - Perform the sequence that locks the FSEAL bit to 1.
  118. *
  119. * The FSEAL bit prevents the PMIC from turning off DCDC5 and DCDC6. It can be
  120. * toggled at most 3 times: 0->1, 1->0, and finally 0->1. After the third switch
  121. * its value is locked and can only be reset by powering off the PMIC entirely.
  122. *
  123. * @return: 0 on success, -EBADE if the sequence was broken
  124. */
  125. int tps65218_lock_fseal(void)
  126. {
  127. int i;
  128. for (i = 0; i < 3; i++)
  129. if (tps65218_toggle_fseal())
  130. return -EBADE;
  131. return 0;
  132. }
  133. int power_tps65218_init(unsigned char bus)
  134. {
  135. static const char name[] = "TPS65218_PMIC";
  136. struct pmic *p = pmic_alloc();
  137. if (!p) {
  138. printf("%s: POWER allocation error!\n", __func__);
  139. return -ENOMEM;
  140. }
  141. p->name = name;
  142. p->interface = PMIC_I2C;
  143. p->number_of_regs = TPS65218_PMIC_NUM_OF_REGS;
  144. p->hw.i2c.addr = TPS65218_CHIP_PM;
  145. p->hw.i2c.tx_num = 1;
  146. p->bus = bus;
  147. return 0;
  148. }