drv_dmic.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * drv_dmic.h
  3. *
  4. * Created on: 2017Äê1ÔÂ11ÈÕ
  5. * Author: Urey
  6. */
  7. #ifndef _DRV_DMIC_H_
  8. #define _DRV_DMIC_H_
  9. /*
  10. * File : drv_dmic.h
  11. * This file is part of RT-Thread RTOS
  12. * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  27. *
  28. * Change Logs:
  29. * Date Author Notes
  30. * 2015-11-19 Urey the first version
  31. */
  32. #include <dma.h>
  33. #include "audio_pipe.h"
  34. #define DMIC_DMA_PAGE_SIZE 512
  35. #define DMIC_DMA_PAGE_NUM RT_DMA_MAX_NODES
  36. struct jz_dmic
  37. {
  38. struct rt_audio_pipe pipe;
  39. struct rt_audio_configure record_config;
  40. uint32_t io_base;
  41. struct clk *clk_gate;
  42. struct rt_dma_channel *rx_dmac;
  43. rt_uint8_t *dma_buf;
  44. rt_uint32_t dma_offset;
  45. /* record */
  46. int record_gain;
  47. };
  48. static inline void dmic_write_reg(struct jz_dmic *dmic, uint32_t reg, uint32_t val)
  49. {
  50. writel(val, dmic->io_base + reg);
  51. }
  52. static inline uint32_t dmic_read_reg(struct jz_dmic *jz_dmic, unsigned int reg)
  53. {
  54. return readl(jz_dmic->io_base + reg);
  55. }
  56. #define dmic_set_reg(dmic, addr, val, mask, offset)\
  57. do { \
  58. int tmp_val = val; \
  59. int read_val = dmic_read_reg(dmic, addr); \
  60. read_val &= (~mask); \
  61. tmp_val = ((tmp_val << offset) & mask); \
  62. tmp_val |= read_val; \
  63. dmic_write_reg(dmic, addr, tmp_val); \
  64. }while(0)
  65. #define dmic_get_reg(dmic, addr, mask, offset) \
  66. ((dmic_read_reg(dmic, addr) & mask) >> offset)
  67. /*********************************************************************************************************
  68. **
  69. *********************************************************************************************************/
  70. #define DMICCR0 0x00
  71. #define DMICGCR 0x04
  72. #define DMICIMR 0x08
  73. #define DMICINTCR 0x0c
  74. #define DMICTRICR 0x10
  75. #define DMICTHRH 0x14
  76. #define DMICTHRL 0x18
  77. #define DMICTRIMMAX 0x1c
  78. #define DMICTRINMAX 0x20
  79. #define DMICDR 0x30
  80. #define DMICFTHR 0x34
  81. #define DMICFSR 0x38
  82. #define DMICCGDIS 0x50
  83. /* DMICCR0 */
  84. #define DMIC_RESET 31
  85. #define DMIC_RESET_MASK (0x1 << DMIC_RESET)
  86. #define DMIC_RESET_TRI 30
  87. #define DMIC_RESET_TRI_MASK (0x1 << DMIC_RESET_TRI)
  88. #define DMIC_CHNUM 16
  89. #define DMIC_CHNUM_MASK (0x7 << DMIC_CHNUM)
  90. #define DMIC_UNPACK_MSB 13
  91. #define DMIC_UNPACK_MSB_MASK (0x1 << DMIC_UNPACK_MSB)
  92. #define DMIC_UNPACK_DIS 12
  93. #define DMIC_UNPACK_DIS_MASK (0x1 << DMIC_UNPACK_DIS)
  94. #define DMIC_SW_LR 11
  95. #define DMIC_SW_LR_MASK (0x1 << DMIC_SW_LR)
  96. #define DMIC_SPLIT_DI 10
  97. #define DMIC_SPLIT_DI_MASK (0x1 << DMIC_SPLIT_DI)
  98. #define DMIC_PACK_EN 8
  99. #define DMIC_PACK_EN_MASK (0x1 << DMIC_PACK_EN)
  100. #define DMIC_SR 6
  101. #define DMIC_SR_MASK (0x3 << DMIC_SR)
  102. #define DMIC_LP_MODE 3
  103. #define DMIC_LP_MODE_MASK (0x1 << DMIC_LP_MODE)
  104. #define DMIC_HPF1_MODE 2
  105. #define DMIC_HPF1_MODE_MASK (0x1 << DMIC_HPF1_MODE)
  106. #define DMIC_TRI_EN 1
  107. #define DMIC_TRI_EN_MASK (0x1 << DMIC_TRI_EN)
  108. #define DMIC_EN 0
  109. #define DMIC_EN_MASK (0x1 << DMIC_EN)
  110. #define __dmic_reset(dmic)\
  111. dmic_set_reg(dmic,DMICCR0,1,DMIC_RESET_MASK,DMIC_RESET)
  112. #define __dmic_get_reset(dmic)\
  113. dmic_get_reg(dmic,DMICCR0,DMIC_RESET_MASK,DMIC_RESET)
  114. #define __dmic_reset_tri(dmic)\
  115. dmic_set_reg(dmic,DMICCR0,1,DMIC_RESET_TRI_MASK,DMIC_RESET_TRI)
  116. #define __dmic_set_chnum(dmic,n)\
  117. dmic_set_reg(dmic,DMICCR0,n,DMIC_CHNUM_MASK,DMIC_CHNUM)
  118. #define __dmic_get_chnum(dmic,n)\
  119. dmic_set_reg(dmic,DMICCR0,DMIC_CHNUM_MASK,DMIC_CHNUM)
  120. #define __dmic_unpack_msb(dmic)\
  121. dmic_set_reg(dmic,DMICCR0,1,DMIC_UNPACK_MSB_MASK,DMIC_UNPACK_MSB)
  122. #define __dmic_unpack_dis(dmic)\
  123. dmic_set_reg(dmic,DMICCR0,1,DMIC_UNPACK_DIS_MASK,DMIC_UNPACK_DIS)
  124. #define __dmic_enable_sw_lr(dmic)\
  125. dmic_set_reg(dmic,DMICCR0,1,DMIC_SW_LR_MASK,DMIC_SW_LR)
  126. #define __dmic_disable_sw_lr(dmic)\
  127. dmic_set_reg(dmic,DMICCR0,0,DMIC_SW_LR_MASK,DMIC_SW_LR)
  128. #define __dmic_split(dmic)\
  129. dmic_set_reg(dmic,DMICCR0,1,DMIC_SPLIT_DI_MASK,DMIC_SPLIT_DI)
  130. #define __dmic_enable_pack(dmic)\
  131. dmic_set_reg(dmic,DMICCR0,1,DMIC_PACK_EN_MASK,DMIC_PACK_EN)
  132. #define __dmic_set_sr(dmic,n)\
  133. dmic_set_reg(dmic,DMICCR0,n,DMIC_SR_MASK,DMIC_SR)
  134. #define __dmic_set_sr_8k(dmic)\
  135. __dmic_set_sr(dmic,0)
  136. #define __dmic_set_sr_16k(dmic)\
  137. __dmic_set_sr(dmic,1)
  138. #define __dmic_set_sr_48k(dmic)\
  139. __dmic_set_sr(dmic,2)
  140. #define __dmic_enable_lp(dmic)\
  141. dmic_set_reg(dmic,DMICCR0,1,DMIC_LP_MODE_MASK,DMIC_LP_MODE)
  142. #define __dmic_disable_lp(dmic)\
  143. dmic_set_reg(dmic,DMICCR0,0,DMIC_LP_MODE_MASK,DMIC_LP_MODE)
  144. #define __dmic_enable_hpf1(dmic)\
  145. dmic_set_reg(dmic,DMICCR0,1,DMIC_HPF1_MODE_MASK,DMIC_HPF1_MODE)
  146. #define __dmic_disable_hpf1(dmic)\
  147. dmic_set_reg(dmic,DMICCR0,0,DMIC_HPF1_MODE_MASK,DMIC_HPF1_MODE)
  148. #define __dmic_is_enable_tri(dmic)\
  149. dmic_get_reg(dmic,DMICCR0,DMIC_TRI_EN_MASK,DMIC_TRI_EN)
  150. #define __dmic_enable_tri(dmic)\
  151. dmic_set_reg(dmic,DMICCR0,1,DMIC_TRI_EN_MASK,DMIC_TRI_EN)
  152. #define __dmic_disable_tri(dmic)\
  153. dmic_set_reg(dmic,DMICCR0,0,DMIC_TRI_EN_MASK,DMIC_TRI_EN)
  154. #define __dmic_is_enable(dmic)\
  155. dmic_get_reg(dmic,DMICCR0,DMIC_EN_MASK,DMIC_EN)
  156. #define __dmic_enable(dmic)\
  157. dmic_set_reg(dmic,DMICCR0,1,DMIC_EN_MASK,DMIC_EN)
  158. #define __dmic_disable(dmic)\
  159. dmic_set_reg(dmic,DMICCR0,0,DMIC_EN_MASK,DMIC_EN)
  160. /*DMICGCR*/
  161. #define DMIC_GCR 0
  162. #define DMIC_GCR_MASK (0Xf << DMIC_GCR)
  163. #define __dmic_set_gcr(dmic,n)\
  164. dmic_set_reg(dmic, DMICGCR, n, DMIC_GCR_MASK,DMIC_GCR)
  165. /* DMICIMR */
  166. #define DMIC_FIFO_TRIG_MASK 5
  167. #define DMIC_FIFO_TRIG_MSK (1 << DMIC_FIFO_TRIG_MASK)
  168. #define DMIC_WAKE_MASK 4
  169. #define DMIC_WAKE_MSK (1 << DMIC_WAKE_MASK)
  170. #define DMIC_EMPTY_MASK 3
  171. #define DMIC_EMPTY_MSK (1 << DMIC_EMPTY_MASK)
  172. #define DMIC_FULL_MASK 2
  173. #define DMIC_FULL_MSK (1 << DMIC_FULL_MASK)
  174. #define DMIC_PRERD_MASK 1
  175. #define DMIC_PRERD_MSK (1 << DMIC_PRERD_MASK)
  176. #define DMIC_TRI_MASK 0
  177. #define DMIC_TRI_MSK (1 << DMIC_TRI_MASK)
  178. #define __dmic_mask_all_int(dmic)\
  179. dmic_set_reg(dmic,DMICIMR, 0x3f, 0x3f, 0)
  180. /*DMICINTCR*/
  181. #define DMIC_FIFO_TRIG_FLAG 4
  182. #define DMIC_FIFO_TRIG_FLAG_MASK (1 << DMIC_WAKE_FLAG)
  183. #define DMIC_WAKE_FLAG 4
  184. #define DMIC_WAKE_FLAG_MASK (1 << DMIC_WAKE_FLAG)
  185. #define DMIC_EMPTY_FLAG 3
  186. #define DMIC_EMPTY_FLAG_MASK (1 << DMIC_EMPTY_FLAG)
  187. #define DMIC_FULL_FLAG 2
  188. #define DMIC_FULL_FLAG_MASK (1 << DMIC_FULL_FLAG)
  189. #define DMIC_PRERD_FLAG 1
  190. #define DMIC_PRERD_FLAG_MASK (1 << DMIC_PRERD_FLAG)
  191. #define DMIC_TRI_FLAG 0
  192. #define DMIC_TRI_FLAG_MASK (1 << DMIC_TRI_FLAG)
  193. /*DMICTRICR*/
  194. #define DMIC_TRI_MODE 16
  195. #define DMIC_TRI_MODE_MASK (0xf << DMIC_TRI_MODE)
  196. #define DMIC_TRI_DEBUG 4
  197. #define DMIC_TRI_DEBUG_MASK (0x1 << DMIC_TRI_DEBUG)
  198. #define DMIC_HPF2_EN 3
  199. #define DMIC_HPF2_EN_MASK (0x1 << DMIC_HPF2_EN)
  200. #define DMIC_PREFETCH 1
  201. #define DMIC_PREFETCH_MASK (0x3 << DMIC_PREFETCH)
  202. #define DMIC_TRI_CLR 0
  203. #define DMIC_TRI_CLR_MASK (0x1 << DMIC_TRI_CLR)
  204. #define __dmic_enable_hpf2(dmic) \
  205. dmic_set_reg(dmic, DMICTRICR, 1, DMIC_HPF2_EN_MASK, DMIC_HPF2_EN)
  206. #define __dmic_disable_hpf2(dmic) \
  207. dmic_set_reg(dmic, DMICTRICR, 0, DMIC_HPF2_EN_MASK, DMIC_HPF2_EN)
  208. /*DMICTHRH*/
  209. #define DMIC_THR_H 0
  210. #define DMIC_THR_H_MASK (0xfffff << DMIC_THR_H)
  211. #define __dmic_set_thr_high(dmic,n) \
  212. dmic_set_reg(dmic, DMICTHRH, n, DMIC_THR_H_MASK, DMIC_THR_H)
  213. /*DMICTHRL*/
  214. #define DMIC_THR_L 0
  215. #define DMIC_THR_L_MASK (0xfffff << DMIC_THR_L)
  216. #define __dmic_set_thr_low(dmic,n) \
  217. dmic_set_reg(dmic, DMICTHRL, n, DMIC_THR_L_MASK, DMIC_THR_L)
  218. /* DMICTRIMMAX */
  219. #define DMIC_M_MAX 0
  220. #define DMIC_M_MAX_MASK (0xffffff << DMIC_M_MAX)
  221. /* DMICTRINMAX */
  222. #define DMIC_N_MAX 0
  223. #define DMIC_N_MAX_MASK (0xffff << DMIC_N_MAX)
  224. /* DMICFTHR */
  225. #define DMIC_RDMS 31
  226. #define DMIC_RDMS_MASK (0x1 << DMIC_RDMS)
  227. #define DMIC_FIFO_THR 0
  228. #define DMIC_FIFO_THR_MASK (0x3f << DMIC_FIFO_THR)
  229. #define __dmic_is_enable_rdms(dmic)\
  230. dmic_get_reg(dmic, DMICFTHR,DMIC_RDMS_MASK,DMIC_RDMS)
  231. #define __dmic_enable_rdms(dmic)\
  232. dmic_set_reg(dmic, DMICFTHR,1,DMIC_RDMS_MASK,DMIC_RDMS)
  233. #define __dmic_disable_rdms(dmic)\
  234. dmic_set_reg(dmic, DMICFTHR,1,DMIC_RDMS_MASK,DMIC_RDMS)
  235. #define __dmic_set_request(dmic,n) \
  236. dmic_set_reg(dmic, DMICFTHR, n, DMIC_FIFO_THR_MASK, DMIC_FIFO_THR)
  237. /*DMICFSR*/
  238. #define DMIC_FULLS 19
  239. #define DMIC_FULLS_MASK (0x1 << DMIC_FULLS)
  240. #define DMIC_TRIGS 18
  241. #define DMIC_TRIGS_MASK (0x1 << DMIC_TRIGS)
  242. #define DMIC_PRERDS 17
  243. #define DMIC_PRERDS_MASK (0x1 << DMIC_PRERDS)
  244. #define DMIC_EMPTYS 16
  245. #define DMIC_EMPTYS_MASK (0x1 << DMIC_EMPTYS)
  246. #define DMIC_FIFO_LVL 0
  247. #define DMIC_FIFO_LVL_MASK (0x3f << DMIC_FIFO_LVL)
  248. /*********************************************************************************************************
  249. **
  250. *********************************************************************************************************/
  251. struct jz_dmic* rt_hw_dmic_init(void);
  252. int jz_dmic_set_rate(struct jz_dmic* dmic, int rate);
  253. int jz_dmic_set_gain(struct jz_dmic* dmic, int vol);
  254. int jz_dmic_set_channels(struct jz_dmic* dmic, int channels);
  255. #endif /* _DRV_DMIC_H_ */