pmic_tps65218.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #if !CONFIG_IS_ENABLED(DM_I2C)
  12. int tps65218_reg_read(uchar dest_reg, uchar *dest_val)
  13. {
  14. uchar read_val;
  15. int ret;
  16. ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
  17. if (ret)
  18. return ret;
  19. *dest_val = read_val;
  20. return 0;
  21. }
  22. /**
  23. * tps65218_reg_write() - Generic function that can write a TPS65218 PMIC
  24. * register or bit field regardless of protection
  25. * level.
  26. *
  27. * @prot_level: Register password protection. Use
  28. * TPS65218_PROT_LEVEL_NONE,
  29. * TPS65218_PROT_LEVEL_1 or TPS65218_PROT_LEVEL_2
  30. * @dest_reg: Register address to write.
  31. * @dest_val: Value to write.
  32. * @mask: Bit mask (8 bits) to be applied. Function will only
  33. * change bits that are set in the bit mask.
  34. *
  35. * @return: 0 for success, not 0 on failure, as per the i2c API
  36. */
  37. int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
  38. uchar mask)
  39. {
  40. uchar read_val;
  41. uchar xor_reg;
  42. int ret;
  43. /*
  44. * If we are affecting only a bit field, read dest_reg and apply the
  45. * mask
  46. */
  47. if (mask != TPS65218_MASK_ALL_BITS) {
  48. ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1);
  49. if (ret)
  50. return ret;
  51. read_val &= (~mask);
  52. read_val |= (dest_val & mask);
  53. dest_val = read_val;
  54. }
  55. if (prot_level > 0) {
  56. xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK;
  57. ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
  58. &xor_reg, 1);
  59. if (ret)
  60. return ret;
  61. }
  62. ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
  63. if (ret)
  64. return ret;
  65. if (prot_level == TPS65218_PROT_LEVEL_2) {
  66. ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1,
  67. &xor_reg, 1);
  68. if (ret)
  69. return ret;
  70. ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1);
  71. if (ret)
  72. return ret;
  73. }
  74. return 0;
  75. }
  76. #else
  77. struct udevice *tps65218_dev __attribute__((section(".data"))) = NULL;
  78. int tps65218_reg_read(uchar dest_reg, uchar *dest_val)
  79. {
  80. uchar read_val;
  81. int ret;
  82. if (!tps65218_dev)
  83. return -ENODEV;
  84. ret = dm_i2c_read(tps65218_dev, dest_reg, &read_val, 1);
  85. if (ret)
  86. return ret;
  87. *dest_val = read_val;
  88. return 0;
  89. }
  90. int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val,
  91. uchar mask)
  92. {
  93. uchar read_val;
  94. uchar xor_reg;
  95. int ret;
  96. if (!tps65218_dev)
  97. return -ENODEV;
  98. /*
  99. * If we are affecting only a bit field, read dest_reg and apply the
  100. * mask
  101. */
  102. if (mask != TPS65218_MASK_ALL_BITS) {
  103. ret = dm_i2c_read(tps65218_dev, dest_reg, &read_val, 1);
  104. if (ret)
  105. return ret;
  106. read_val &= (~mask);
  107. read_val |= (dest_val & mask);
  108. dest_val = read_val;
  109. }
  110. if (prot_level > 0) {
  111. xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK;
  112. ret = dm_i2c_write(tps65218_dev, TPS65218_PASSWORD, &xor_reg,
  113. 1);
  114. if (ret)
  115. return ret;
  116. }
  117. ret = dm_i2c_write(tps65218_dev, dest_reg, &dest_val, 1);
  118. if (ret)
  119. return ret;
  120. if (prot_level == TPS65218_PROT_LEVEL_2) {
  121. ret = dm_i2c_write(tps65218_dev, TPS65218_PASSWORD, &xor_reg,
  122. 1);
  123. if (ret)
  124. return ret;
  125. ret = dm_i2c_write(tps65218_dev, dest_reg, &dest_val, 1);
  126. if (ret)
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. #endif
  132. /**
  133. * tps65218_voltage_update() - Function to change a voltage level, as this
  134. * is a multi-step process.
  135. * @dc_cntrl_reg: DC voltage control register to change.
  136. * @volt_sel: New value for the voltage register
  137. * @return: 0 for success, not 0 on failure.
  138. */
  139. int tps65218_voltage_update(uchar dc_cntrl_reg, uchar volt_sel)
  140. {
  141. if ((dc_cntrl_reg != TPS65218_DCDC1) &&
  142. (dc_cntrl_reg != TPS65218_DCDC2) &&
  143. (dc_cntrl_reg != TPS65218_DCDC3))
  144. return 1;
  145. /* set voltage level */
  146. if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, dc_cntrl_reg, volt_sel,
  147. TPS65218_DCDC_VSEL_MASK))
  148. return 1;
  149. /* set GO bit to initiate voltage transition */
  150. if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, TPS65218_SLEW,
  151. TPS65218_DCDC_GO, TPS65218_DCDC_GO))
  152. return 1;
  153. return 0;
  154. }
  155. /**
  156. * tps65218_toggle_fseal() - Perform the sequence that toggles the FSEAL bit.
  157. *
  158. * @return: 0 on success, -EBADE if the sequence was broken
  159. */
  160. int tps65218_toggle_fseal(void)
  161. {
  162. if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
  163. 0xb1, TPS65218_MASK_ALL_BITS))
  164. return -EBADE;
  165. if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
  166. 0xfe, TPS65218_MASK_ALL_BITS))
  167. return -EBADE;
  168. if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD,
  169. 0xa3, TPS65218_MASK_ALL_BITS))
  170. return -EBADE;
  171. return 0;
  172. }
  173. /**
  174. * tps65218_lock_fseal() - Perform the sequence that locks the FSEAL bit to 1.
  175. *
  176. * The FSEAL bit prevents the PMIC from turning off DCDC5 and DCDC6. It can be
  177. * toggled at most 3 times: 0->1, 1->0, and finally 0->1. After the third switch
  178. * its value is locked and can only be reset by powering off the PMIC entirely.
  179. *
  180. * @return: 0 on success, -EBADE if the sequence was broken
  181. */
  182. int tps65218_lock_fseal(void)
  183. {
  184. int i;
  185. for (i = 0; i < 3; i++)
  186. if (tps65218_toggle_fseal())
  187. return -EBADE;
  188. return 0;
  189. }
  190. #if !CONFIG_IS_ENABLED(DM_I2C)
  191. int power_tps65218_init(unsigned char bus)
  192. {
  193. static const char name[] = "TPS65218_PMIC";
  194. struct pmic *p = pmic_alloc();
  195. if (!p) {
  196. printf("%s: POWER allocation error!\n", __func__);
  197. return -ENOMEM;
  198. }
  199. p->name = name;
  200. p->interface = PMIC_I2C;
  201. p->number_of_regs = TPS65218_PMIC_NUM_OF_REGS;
  202. p->hw.i2c.addr = TPS65218_CHIP_PM;
  203. p->hw.i2c.tx_num = 1;
  204. p->bus = bus;
  205. return 0;
  206. }
  207. #else
  208. int power_tps65218_init(unsigned char bus)
  209. {
  210. struct udevice *dev = NULL;
  211. int rc;
  212. rc = i2c_get_chip_for_busnum(bus, TPS65218_CHIP_PM, 1, &dev);
  213. if (rc)
  214. return rc;
  215. tps65218_dev = dev;
  216. return 0;
  217. }
  218. #endif