axp209.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * AXP-209 PMU Emulation
  3. *
  4. * Copyright (C) 2022 Strahinja Jankovic <strahinja.p.jankovic@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * SPDX-License-Identifier: MIT
  25. */
  26. #include "qemu/osdep.h"
  27. #include "qemu/log.h"
  28. #include "trace.h"
  29. #include "hw/i2c/i2c.h"
  30. #include "migration/vmstate.h"
  31. #define TYPE_AXP209_PMU "axp209_pmu"
  32. #define AXP209(obj) \
  33. OBJECT_CHECK(AXP209I2CState, (obj), TYPE_AXP209_PMU)
  34. /* registers */
  35. enum {
  36. REG_POWER_STATUS = 0x0u,
  37. REG_OPERATING_MODE,
  38. REG_OTG_VBUS_STATUS,
  39. REG_CHIP_VERSION,
  40. REG_DATA_CACHE_0,
  41. REG_DATA_CACHE_1,
  42. REG_DATA_CACHE_2,
  43. REG_DATA_CACHE_3,
  44. REG_DATA_CACHE_4,
  45. REG_DATA_CACHE_5,
  46. REG_DATA_CACHE_6,
  47. REG_DATA_CACHE_7,
  48. REG_DATA_CACHE_8,
  49. REG_DATA_CACHE_9,
  50. REG_DATA_CACHE_A,
  51. REG_DATA_CACHE_B,
  52. REG_POWER_OUTPUT_CTRL = 0x12u,
  53. REG_DC_DC2_OUT_V_CTRL = 0x23u,
  54. REG_DC_DC2_DVS_CTRL = 0x25u,
  55. REG_DC_DC3_OUT_V_CTRL = 0x27u,
  56. REG_LDO2_4_OUT_V_CTRL,
  57. REG_LDO3_OUT_V_CTRL,
  58. REG_VBUS_CH_MGMT = 0x30u,
  59. REG_SHUTDOWN_V_CTRL,
  60. REG_SHUTDOWN_CTRL,
  61. REG_CHARGE_CTRL_1,
  62. REG_CHARGE_CTRL_2,
  63. REG_SPARE_CHARGE_CTRL,
  64. REG_PEK_KEY_CTRL,
  65. REG_DC_DC_FREQ_SET,
  66. REG_CHR_TEMP_TH_SET,
  67. REG_CHR_HIGH_TEMP_TH_CTRL,
  68. REG_IPSOUT_WARN_L1,
  69. REG_IPSOUT_WARN_L2,
  70. REG_DISCHR_TEMP_TH_SET,
  71. REG_DISCHR_HIGH_TEMP_TH_CTRL,
  72. REG_IRQ_BANK_1_CTRL = 0x40u,
  73. REG_IRQ_BANK_2_CTRL,
  74. REG_IRQ_BANK_3_CTRL,
  75. REG_IRQ_BANK_4_CTRL,
  76. REG_IRQ_BANK_5_CTRL,
  77. REG_IRQ_BANK_1_STAT = 0x48u,
  78. REG_IRQ_BANK_2_STAT,
  79. REG_IRQ_BANK_3_STAT,
  80. REG_IRQ_BANK_4_STAT,
  81. REG_IRQ_BANK_5_STAT,
  82. REG_ADC_ACIN_V_H = 0x56u,
  83. REG_ADC_ACIN_V_L,
  84. REG_ADC_ACIN_CURR_H,
  85. REG_ADC_ACIN_CURR_L,
  86. REG_ADC_VBUS_V_H,
  87. REG_ADC_VBUS_V_L,
  88. REG_ADC_VBUS_CURR_H,
  89. REG_ADC_VBUS_CURR_L,
  90. REG_ADC_INT_TEMP_H,
  91. REG_ADC_INT_TEMP_L,
  92. REG_ADC_TEMP_SENS_V_H = 0x62u,
  93. REG_ADC_TEMP_SENS_V_L,
  94. REG_ADC_BAT_V_H = 0x78u,
  95. REG_ADC_BAT_V_L,
  96. REG_ADC_BAT_DISCHR_CURR_H,
  97. REG_ADC_BAT_DISCHR_CURR_L,
  98. REG_ADC_BAT_CHR_CURR_H,
  99. REG_ADC_BAT_CHR_CURR_L,
  100. REG_ADC_IPSOUT_V_H,
  101. REG_ADC_IPSOUT_V_L,
  102. REG_DC_DC_MOD_SEL = 0x80u,
  103. REG_ADC_EN_1,
  104. REG_ADC_EN_2,
  105. REG_ADC_SR_CTRL,
  106. REG_ADC_IN_RANGE,
  107. REG_GPIO1_ADC_IRQ_RISING_TH,
  108. REG_GPIO1_ADC_IRQ_FALLING_TH,
  109. REG_TIMER_CTRL = 0x8au,
  110. REG_VBUS_CTRL_MON_SRP,
  111. REG_OVER_TEMP_SHUTDOWN = 0x8fu,
  112. REG_GPIO0_FEAT_SET,
  113. REG_GPIO_OUT_HIGH_SET,
  114. REG_GPIO1_FEAT_SET,
  115. REG_GPIO2_FEAT_SET,
  116. REG_GPIO_SIG_STATE_SET_MON,
  117. REG_GPIO3_SET,
  118. REG_COULOMB_CNTR_CTRL = 0xb8u,
  119. REG_POWER_MEAS_RES,
  120. NR_REGS
  121. };
  122. #define AXP209_CHIP_VERSION_ID (0x01)
  123. #define AXP209_DC_DC2_OUT_V_CTRL_RESET (0x16)
  124. #define AXP209_IRQ_BANK_1_CTRL_RESET (0xd8)
  125. /* A simple I2C slave which returns values of ID or CNT register. */
  126. typedef struct AXP209I2CState {
  127. /*< private >*/
  128. I2CSlave i2c;
  129. /*< public >*/
  130. uint8_t regs[NR_REGS]; /* peripheral registers */
  131. uint8_t ptr; /* current register index */
  132. uint8_t count; /* counter used for tx/rx */
  133. } AXP209I2CState;
  134. /* Reset all counters and load ID register */
  135. static void axp209_reset_enter(Object *obj, ResetType type)
  136. {
  137. AXP209I2CState *s = AXP209(obj);
  138. memset(s->regs, 0, NR_REGS);
  139. s->ptr = 0;
  140. s->count = 0;
  141. s->regs[REG_CHIP_VERSION] = AXP209_CHIP_VERSION_ID;
  142. s->regs[REG_DC_DC2_OUT_V_CTRL] = AXP209_DC_DC2_OUT_V_CTRL_RESET;
  143. s->regs[REG_IRQ_BANK_1_CTRL] = AXP209_IRQ_BANK_1_CTRL_RESET;
  144. }
  145. /* Handle events from master. */
  146. static int axp209_event(I2CSlave *i2c, enum i2c_event event)
  147. {
  148. AXP209I2CState *s = AXP209(i2c);
  149. s->count = 0;
  150. return 0;
  151. }
  152. /* Called when master requests read */
  153. static uint8_t axp209_rx(I2CSlave *i2c)
  154. {
  155. AXP209I2CState *s = AXP209(i2c);
  156. uint8_t ret = 0xff;
  157. if (s->ptr < NR_REGS) {
  158. ret = s->regs[s->ptr++];
  159. }
  160. trace_axp209_rx(s->ptr - 1, ret);
  161. return ret;
  162. }
  163. /*
  164. * Called when master sends write.
  165. * Update ptr with byte 0, then perform write with second byte.
  166. */
  167. static int axp209_tx(I2CSlave *i2c, uint8_t data)
  168. {
  169. AXP209I2CState *s = AXP209(i2c);
  170. if (s->count == 0) {
  171. /* Store register address */
  172. s->ptr = data;
  173. s->count++;
  174. trace_axp209_select(data);
  175. } else {
  176. trace_axp209_tx(s->ptr, data);
  177. if (s->ptr == REG_DC_DC2_OUT_V_CTRL) {
  178. s->regs[s->ptr++] = data;
  179. }
  180. }
  181. return 0;
  182. }
  183. static const VMStateDescription vmstate_axp209 = {
  184. .name = TYPE_AXP209_PMU,
  185. .version_id = 1,
  186. .fields = (VMStateField[]) {
  187. VMSTATE_UINT8_ARRAY(regs, AXP209I2CState, NR_REGS),
  188. VMSTATE_UINT8(count, AXP209I2CState),
  189. VMSTATE_UINT8(ptr, AXP209I2CState),
  190. VMSTATE_END_OF_LIST()
  191. }
  192. };
  193. static void axp209_class_init(ObjectClass *oc, void *data)
  194. {
  195. DeviceClass *dc = DEVICE_CLASS(oc);
  196. I2CSlaveClass *isc = I2C_SLAVE_CLASS(oc);
  197. ResettableClass *rc = RESETTABLE_CLASS(oc);
  198. rc->phases.enter = axp209_reset_enter;
  199. dc->vmsd = &vmstate_axp209;
  200. isc->event = axp209_event;
  201. isc->recv = axp209_rx;
  202. isc->send = axp209_tx;
  203. }
  204. static const TypeInfo axp209_info = {
  205. .name = TYPE_AXP209_PMU,
  206. .parent = TYPE_I2C_SLAVE,
  207. .instance_size = sizeof(AXP209I2CState),
  208. .class_init = axp209_class_init
  209. };
  210. static void axp209_register_devices(void)
  211. {
  212. type_register_static(&axp209_info);
  213. }
  214. type_init(axp209_register_devices);