upower_hal.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2020-2024 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <common/debug.h>
  8. #include <drivers/delay_timer.h>
  9. #include <lib/mmio.h>
  10. #include "upower_api.h"
  11. #include "upower_defs.h"
  12. #define UPOWER_AP_MU1_ADDR U(0x29280000)
  13. struct MU_t *muptr = (struct MU_t *)UPOWER_AP_MU1_ADDR;
  14. void upower_apd_inst_isr(upwr_isr_callb txrx_isr,
  15. upwr_isr_callb excp_isr)
  16. {
  17. /* Do nothing */
  18. }
  19. int upower_status(int status)
  20. {
  21. int ret = -1;
  22. switch (status) {
  23. case 0:
  24. VERBOSE("finished successfully!\n");
  25. ret = 0;
  26. break;
  27. case -1:
  28. VERBOSE("memory allocation or resource failed!\n");
  29. break;
  30. case -2:
  31. VERBOSE("invalid argument!\n");
  32. break;
  33. case -3:
  34. VERBOSE("called in an invalid API state!\n");
  35. break;
  36. default:
  37. VERBOSE("invalid return status\n");
  38. break;
  39. }
  40. return ret;
  41. }
  42. void upower_wait_resp(void)
  43. {
  44. while (muptr->RSR.B.RF0 == 0) {
  45. udelay(100);
  46. }
  47. upwr_txrx_isr();
  48. }
  49. static void user_upwr_rdy_callb(uint32_t soc, uint32_t vmajor, uint32_t vminor)
  50. {
  51. NOTICE("%s: soc=%x\n", __func__, soc);
  52. NOTICE("%s: RAM version:%d.%d\n", __func__, vmajor, vminor);
  53. }
  54. int upower_init(void)
  55. {
  56. int status;
  57. status = upwr_init(APD_DOMAIN, muptr, NULL, NULL, upower_apd_inst_isr, NULL);
  58. if (upower_status(status)) {
  59. ERROR("%s: upower init failure\n", __func__);
  60. return -EINVAL;
  61. }
  62. NOTICE("%s: start uPower RAM service\n", __func__);
  63. status = upwr_start(1, user_upwr_rdy_callb);
  64. upower_wait_resp();
  65. /* poll status */
  66. if (upower_status(status)) {
  67. NOTICE("%s: upower init failure\n", __func__);
  68. return status;
  69. }
  70. return 0;
  71. }
  72. int upower_pwm(int domain_id, bool pwr_on)
  73. {
  74. int ret, ret_val;
  75. uint32_t swt;
  76. if (domain_id == 9U || domain_id == 11U || domain_id == 12U) {
  77. swt = BIT_32(12) | BIT_32(11) | BIT_32(10) | BIT_32(9);
  78. } else {
  79. swt = BIT_32(domain_id);
  80. }
  81. if (pwr_on) {
  82. ret = upwr_pwm_power_on(&swt, NULL, NULL);
  83. } else {
  84. ret = upwr_pwm_power_off(&swt, NULL, NULL);
  85. }
  86. if (ret) {
  87. NOTICE("%s failed: ret: %d, pwr_on: %d\n", __func__, ret, pwr_on);
  88. return ret;
  89. }
  90. upower_wait_resp();
  91. ret = upwr_poll_req_status(UPWR_SG_PWRMGMT, NULL, NULL, &ret_val, 1000);
  92. if (ret != UPWR_REQ_OK) {
  93. NOTICE("Failure %d, %s\n", ret, __func__);
  94. if (ret == UPWR_REQ_BUSY) {
  95. return -EBUSY;
  96. } else {
  97. return -EINVAL;
  98. }
  99. }
  100. return 0;
  101. }
  102. int upower_read_temperature(uint32_t sensor_id, int32_t *temperature)
  103. {
  104. int ret, ret_val;
  105. upwr_resp_t err_code;
  106. int64_t t;
  107. ret = upwr_tpm_get_temperature(sensor_id, NULL);
  108. if (ret) {
  109. return ret;
  110. }
  111. upower_wait_resp();
  112. ret = upwr_poll_req_status(UPWR_SG_TEMPM, NULL, &err_code, &ret_val, 1000);
  113. if (ret > UPWR_REQ_OK) {
  114. return ret;
  115. }
  116. t = ret_val & 0xff;
  117. *temperature = (2673049 * t * t * t / 10000000 + 3734262 * t * t / 100000 +
  118. 4487042 * t / 100 - 4698694) / 100000;
  119. return 0;
  120. }
  121. int upower_pmic_i2c_write(uint32_t reg_addr, uint32_t reg_val)
  122. {
  123. int ret, ret_val;
  124. upwr_resp_t err_code;
  125. ret = upwr_xcp_i2c_access(0x32, 1, 1, reg_addr, reg_val, NULL);
  126. if (ret) {
  127. WARN("pmic i2c read failed ret %d\n", ret);
  128. return ret;
  129. }
  130. upower_wait_resp();
  131. ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, &err_code, &ret_val, 1000);
  132. if (ret != UPWR_REQ_OK) {
  133. WARN("i2c poll Failure %d, err_code %d, ret_val 0x%x\n",
  134. ret, err_code, ret_val);
  135. return ret;
  136. }
  137. VERBOSE("PMIC write reg[0x%x], val[0x%x]\n", reg_addr, reg_val);
  138. return 0;
  139. }
  140. int upower_pmic_i2c_read(uint32_t reg_addr, uint32_t *reg_val)
  141. {
  142. int ret, ret_val;
  143. upwr_resp_t err_code;
  144. if (reg_val == NULL) {
  145. return -1;
  146. }
  147. ret = upwr_xcp_i2c_access(0x32, -1, 1, reg_addr, 0, NULL);
  148. if (ret) {
  149. WARN("pmic i2c read failed ret %d\n", ret);
  150. return ret;
  151. }
  152. upower_wait_resp();
  153. ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, &err_code, &ret_val, 1000);
  154. if (ret != UPWR_REQ_OK) {
  155. WARN("i2c poll Failure %d, err_code %d, ret_val 0x%x\n",
  156. ret, err_code, ret_val);
  157. return ret;
  158. }
  159. *reg_val = ret_val;
  160. VERBOSE("PMIC read reg[0x%x], val[0x%x]\n", reg_addr, *reg_val);
  161. return 0;
  162. }