pmic_tps65217.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /**
  10. * tps65217_reg_read() - Generic function that can read a TPS65217 register
  11. * @src_reg: Source register address
  12. * @src_val: Address of destination variable
  13. * @return: 0 for success, not 0 on failure.
  14. */
  15. int tps65217_reg_read(uchar src_reg, uchar *src_val)
  16. {
  17. return i2c_read(TPS65217_CHIP_PM, src_reg, 1, src_val, 1);
  18. }
  19. /**
  20. * tps65217_reg_write() - Generic function that can write a TPS65217 PMIC
  21. * register or bit field regardless of protection
  22. * level.
  23. *
  24. * @prot_level: Register password protection. Use
  25. * TPS65217_PROT_LEVEL_NONE,
  26. * TPS65217_PROT_LEVEL_1 or TPS65217_PROT_LEVEL_2
  27. * @dest_reg: Register address to write.
  28. * @dest_val: Value to write.
  29. * @mask: Bit mask (8 bits) to be applied. Function will only
  30. * change bits that are set in the bit mask.
  31. *
  32. * @return: 0 for success, not 0 on failure, as per the i2c API
  33. */
  34. int tps65217_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
  35. uchar mask)
  36. {
  37. uchar read_val;
  38. uchar xor_reg;
  39. int ret;
  40. /*
  41. * If we are affecting only a bit field, read dest_reg and apply the
  42. * mask
  43. */
  44. if (mask != TPS65217_MASK_ALL_BITS) {
  45. ret = i2c_read(TPS65217_CHIP_PM, dest_reg, 1, &read_val, 1);
  46. if (ret)
  47. return ret;
  48. read_val &= (~mask);
  49. read_val |= (dest_val & mask);
  50. dest_val = read_val;
  51. }
  52. if (prot_level > 0) {
  53. xor_reg = dest_reg ^ TPS65217_PASSWORD_UNLOCK;
  54. ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
  55. &xor_reg, 1);
  56. if (ret)
  57. return ret;
  58. }
  59. ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
  60. if (ret)
  61. return ret;
  62. if (prot_level == TPS65217_PROT_LEVEL_2) {
  63. ret = i2c_write(TPS65217_CHIP_PM, TPS65217_PASSWORD, 1,
  64. &xor_reg, 1);
  65. if (ret)
  66. return ret;
  67. ret = i2c_write(TPS65217_CHIP_PM, dest_reg, 1, &dest_val, 1);
  68. if (ret)
  69. return ret;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * tps65217_voltage_update() - Function to change a voltage level, as this
  75. * is a multi-step process.
  76. * @dc_cntrl_reg: DC voltage control register to change.
  77. * @volt_sel: New value for the voltage register
  78. * @return: 0 for success, not 0 on failure.
  79. */
  80. int tps65217_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
  81. {
  82. if ((dc_cntrl_reg != TPS65217_DEFDCDC1) &&
  83. (dc_cntrl_reg != TPS65217_DEFDCDC2) &&
  84. (dc_cntrl_reg != TPS65217_DEFDCDC3))
  85. return 1;
  86. /* set voltage level */
  87. if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
  88. TPS65217_MASK_ALL_BITS))
  89. return 1;
  90. /* set GO bit to initiate voltage transition */
  91. if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, TPS65217_DEFSLEW,
  92. TPS65217_DCDC_GO, TPS65217_DCDC_GO))
  93. return 1;
  94. return 0;
  95. }