i2s.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*******************************************************************************
  2. * File Name : i2s.h
  3. * Author : ZJH
  4. * Date First Issued : 6/24/2024
  5. * Description : i2s drive.
  6. ********************************************************************************
  7. * History:
  8. * 6/24/2024: V0.1
  9. *******************************************************************************/
  10. #ifndef __ARK_I2S_H
  11. #define __ARK_I2S_H
  12. /*
  13. * I2S Controller Register and Bit Definitions
  14. */
  15. #define I2S_SACR0 0x00 /* Global Control Register */
  16. #define I2S_SACR1 0x04 /* Serial Audio I 2 S/MSB-Justified Control Register */
  17. #define I2S_DACR0 0x08
  18. #define I2S_WRCTL 0x08
  19. #define I2S_RDCTL 0x10
  20. #define I2S_SASR0 0x0C /* Serial Audio I 2 S/MSB-Justified Interface and FIFO Status Register */
  21. #define I2S_SAIMR 0x14 /* Serial Audio Interrupt Mask Register */
  22. #define I2S_SAICR 0x18 /* Serial Audio Interrupt Clear Register */
  23. #define I2S_ADCR0 0x1c
  24. #define I2S_SADR 0x80 /* Serial Audio Data Register (TX and RX FIFO access Register). */
  25. #define SACR0_RFIFIFIRSTBIT (1 << 26) /* rx fifo first bit */
  26. #define SACR0_TFIFOFIRSTBIT (1 << 25) /* Tx fifo first bit */
  27. #define SACR0_CHANLOCK (1 << 24) /* Channel lock(left first or right first) */
  28. #define SACR0_SCBIT (1 << 23) /* */
  29. #define SACR0_BITS (1 << 22) /* I2S Bit Select(16/32 bits) */
  30. #define SACR0_SYNCINV (1 << 21) /* SYNC Clock Invert */
  31. #define SACR0_RFTH_MASK (0x1F << 16)
  32. #define SACR0_RFTH(x) ((x) << 16) /* Rx FIFO Interrupt or DMA Trigger Threshold */
  33. #define SACR0_TFTH_MASK (0X1F << 8)
  34. #define SACR0_TFTH(x) ((x) << 8) /* Tx FIFO Interrupt or DMA Trigger Threshold */
  35. #define SACR0_STRF (1 << 7) /* DAC output clk edge select */
  36. #define SACR0_RDMAEN (1 << 6) /* RX DMA Enable */
  37. #define SACR0_ENLBF (1 << 5) /* Enable Loopback */
  38. #define SACR0_RST (1 << 4) /* FIFO, i2s Register Reset */
  39. #define SACR0_TDMAEN (1 << 3) /* TX DMA Enable */
  40. #define SACR0_BCKD (1 << 2) /* Bit Clock Direction */
  41. #define SACR0_SYNCD (1 << 1) /* Word Select(sync) Clock Direction */
  42. #define SACR0_ENB (1 << 0) /* Enable I2S Link */
  43. #define SACR1_DRPL (1 << 1) /* Disable Replaying Function */
  44. #define SACR1_DREC (1 << 0) /* Disable Recording Function */
  45. #define SASR0_RFL(x) ((x) << 16) /* Rx FIFO Level */
  46. #define SASR0_TFL(x) ((x) << 8) /* Tx FIFO Level */
  47. #define SASR0_ROR (1 << 6) /* Rx FIFO Overrun */
  48. #define SASR0_TUR (1 << 5) /* Tx FIFO Underrun */
  49. #define SASR0_RFS (1 << 4) /* Rx FIFO Service Request */
  50. #define SASR0_TFS (1 << 3) /* Tx FIFO Service Request */
  51. #define SASR0_BSY (1 << 2) /* I2S Busy */
  52. #define SASR0_RNE (1 << 1) /* Rx FIFO Not Empty */
  53. #define SASR0_TNF (1 << 0) /* Tx FIFO Not Full */
  54. #define SAICR_ROR (1 << 6) /* Clear Rx FIFO Overrun Interrupt */
  55. #define SAICR_TUR (1 << 5) /* Clear Tx FIFO Underrun Interrupt */
  56. #define SAICR_RFS (1 << 4) /* Clear Rx FIFO Service Interrupt */
  57. #define SAICR_TFS (1 << 3) /* Clear Tx FIFO Service Interrupt */
  58. #define SAIMR_ROR (1 << 6) /* Enable Rx FIFO Overrun Condition Interrupt */
  59. #define SAIMR_TUR (1 << 5) /* Enable Tx FIFO Underrun Condition Interrupt */
  60. #define SAIMR_RFS (1 << 4) /* Enable Rx FIFO Service Interrupt */
  61. #define SAIMR_TFS (1 << 3) /* Enable Tx FIFO Service Interrupt */
  62. #define I2S_SUP_MASTER (1 << 0)
  63. #define I2S_SUP_SLAVER (1 << 1)
  64. #define I2S_SUP_OUTPUT (1 << 2)
  65. #define I2S_SUP_INPUT (1 << 3)
  66. #define I2S_SUP_FULL_DUPLEX (1 << 4)
  67. #define I2S_DIR_OUTPUT (1 << 0)
  68. #define I2S_DIR_INPUT (1 << 1)
  69. #define I2S_DIR_FULL_DUPLEX (I2S_DIR_OUTPUT | I2S_DIR_INPUT)
  70. #define I2S_VOL_MAX AUDIO_VOLUME_MAX
  71. #define I2S_VOL_MIN AUDIO_VOLUME_MIN
  72. #define I2S_STA_OUTPUTTING (1 << 0)
  73. #define I2S_STA_INPUTTING (1 << 1)
  74. enum i2s_type{
  75. IIS_BUS,
  76. };
  77. struct ark_i2s_cfg {
  78. uint8_t dir;
  79. int rates;
  80. uint8_t bits;
  81. int channels;
  82. uint8_t in_lvol;
  83. uint8_t in_rvol;
  84. uint8_t out_lvol;
  85. uint8_t out_rvol;
  86. uint8_t status;
  87. uint8_t rfirst;
  88. };
  89. struct ark_i2s_drv {
  90. uint32_t id;
  91. uint32_t base;
  92. uint32_t nco_base;
  93. uint32_t irqn;
  94. uint32_t dma_tx_req_id;
  95. uint32_t dma_rx_req_id;
  96. uint32_t clkid;
  97. uint32_t sup_func;
  98. uint32_t chn_sum;
  99. uint32_t softreset_id;
  100. uint32_t is_master;
  101. uint32_t dma_tx_chn;
  102. uint32_t dma_rx_chn;
  103. enum i2s_type type;
  104. struct dma_chan *dma_txch;
  105. struct dma_chan *dma_rxch;
  106. struct ark_i2s_cfg *cfg;
  107. const char *decoder_name;
  108. const char *encoder_name;
  109. void *extdata;
  110. };
  111. int ark_i2s_init(struct ark_i2s_drv *i2s, int dir);
  112. int ark_i2s_set_samplerate(struct ark_i2s_drv *i2s, int rates);
  113. int ark_i2s_set_channels(struct ark_i2s_drv *i2s, int channels);
  114. int ark_i2s_set_samplebits(struct ark_i2s_drv *i2s, int bits);
  115. int ark_i2s_set_rfirst(struct ark_i2s_drv *i2s, int enable);
  116. int ark_i2s_set_volume(struct ark_i2s_drv *i2s, int dir, int lvol, int rvol);
  117. int ark_i2s_set_mute(struct ark_i2s_drv *i2s, int dir, int mute);
  118. int ark_i2s_startup(struct ark_i2s_drv *i2s, int dir);
  119. int ark_i2s_stop(struct ark_i2s_drv *i2s, int dir);
  120. #endif