drv_i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2025-01-21 zhangyan first version
  11. *
  12. */
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <drv_i2s.h>
  16. #include "fi2s.h"
  17. #include "fi2s_hw.h"
  18. #include "fddma.h"
  19. #include "fddma_hw.h"
  20. #include "fddma_bdl.h"
  21. #include "fes8336.h"
  22. #define DBG_TAG "drv.i2s"
  23. #define DBG_LVL DBG_INFO
  24. #include <rtdbg.h>
  25. #define PER_BUFFER_SIZE 2048
  26. #define TX_RX_BUF_LEN 2048
  27. static struct phytium_i2s_device i2s_dev0;
  28. extern FI2c master_device;
  29. extern FMioCtrl es8336;
  30. #define ES8336_MIO FMIO14_ID
  31. #define ES8336_ADDR 0x10
  32. struct phytium_i2s_device
  33. {
  34. const char *name;
  35. struct rt_audio_device audio;
  36. struct rt_audio_configure config;
  37. FI2s i2s_ctrl;
  38. FI2sConfig i2s_config;
  39. FDdma ddmac;
  40. FDdmaConfig ddmac_config;
  41. rt_uint8_t ddma_ctrl_id;
  42. rt_uint8_t i2s_ctrl_id;
  43. rt_uint8_t *rx_fifo;
  44. FDdmaChanConfig rx_config;
  45. rt_uint8_t rx_channel; /* 接收通道为DDMA通道1 */
  46. rt_uint8_t volume;
  47. };
  48. static void FDdmaSetupInterrupt(FDdma *const instance)
  49. {
  50. FASSERT(instance);
  51. FDdmaConfig *config = &instance->config;
  52. rt_uint32_t cpu_id = rt_hw_cpu_id();
  53. rt_hw_interrupt_set_target_cpus(config->irq_num, cpu_id);
  54. rt_hw_interrupt_set_priority(config->irq_num, 16);
  55. /* register intr callback */
  56. rt_hw_interrupt_install(config->irq_num,
  57. FDdmaIrqHandler,
  58. instance,
  59. NULL);
  60. /* enable ddma0 irq */
  61. rt_hw_interrupt_umask(config->irq_num);
  62. return;
  63. }
  64. FError RtEs8336Init(void)
  65. {
  66. FError ret = FES8336_SUCCESS;
  67. FMioCtrl *pctrl = &es8336;
  68. FMioConfig *mioconfig_p ;
  69. FI2c *instance_p = &master_device;
  70. FI2cConfig i2cconfig;
  71. mioconfig_p = FMioLookupConfig(ES8336_MIO);
  72. if (NULL == mioconfig_p)
  73. {
  74. printf("Mio error inval parameters.\r\n");
  75. return FMIO_ERR_INVAL_PARM;
  76. }
  77. pctrl->config = *mioconfig_p;
  78. ret = FMioFuncInit(pctrl, FMIO_FUNC_SET_I2C);
  79. if (ret != FES8336_SUCCESS)
  80. {
  81. printf("ES8336_MIO MioInit error.\r\n");
  82. return ret;
  83. }
  84. /* get standard config of i2c */
  85. i2cconfig = *FI2cLookupConfig(FI2C0_ID);
  86. /* Modify configuration */
  87. i2cconfig.base_addr = FMioFuncGetAddress(pctrl, FMIO_FUNC_SET_I2C);
  88. i2cconfig.irq_num = FMioFuncGetIrqNum(pctrl, FMIO_FUNC_SET_I2C);
  89. FI2cDeInitialize(instance_p);
  90. /* Initialization */
  91. ret = FI2cCfgInitialize(instance_p, &i2cconfig);
  92. if (ret != FES8336_SUCCESS)
  93. {
  94. return ret;
  95. }
  96. /*set the i2c parameters */
  97. ret = FI2cSetAddress(instance_p, FI2C_MASTER, ES8336_ADDR);
  98. if (FI2C_SUCCESS != ret)
  99. {
  100. printf("set mio slave parameters failed, ret: 0x%x\r\n", ret);
  101. return ret;
  102. }
  103. ret = FI2cSetSpeed(instance_p, FI2C_SPEED_STANDARD_RATE, TRUE);
  104. if (FI2C_SUCCESS != ret)
  105. {
  106. printf("set mio slave parameters failed, ret: 0x%x\r\n", ret);
  107. return ret;
  108. }
  109. /* callback function for FI2C_MASTER_INTR_EVT interrupt */
  110. instance_p->master_evt_handlers[FI2C_EVT_MASTER_TRANS_ABORTED] = NULL;
  111. instance_p->master_evt_handlers[FI2C_EVT_MASTER_READ_DONE] = NULL;
  112. instance_p->master_evt_handlers[FI2C_EVT_MASTER_WRITE_DONE] = NULL;
  113. return ret;
  114. }
  115. static FError FI2sEs8336Init(u32 word_length)
  116. {
  117. FError ret = FT_SUCCESS;
  118. FIOPadSetI2sMux();
  119. ret = RtEs8336Init(); /* es8336初始化,i2s slave设置 */
  120. if (FT_SUCCESS != ret)
  121. {
  122. printf("Es8336 init failed.\r\n");
  123. return ret;
  124. }
  125. FEs8336RegsProbe(); /* 寄存器默认值 */
  126. FEs8336Startup();
  127. ret = FEs8336SetFormat(word_length); /* 设置ES8336工作模式 */
  128. if (FT_SUCCESS != ret)
  129. {
  130. printf("Set the es8336 word length failed.\r\n");
  131. return ret;
  132. }
  133. FEs8336SetVolumel(0x1);
  134. return ret;
  135. }
  136. static FError FI2sRxInit(struct phytium_i2s_device *i2s_dev, u32 word_length)
  137. {
  138. FError ret = FI2S_SUCCESS;
  139. u32 i2s_id = i2s_dev->i2s_ctrl_id;
  140. memset(&i2s_dev->i2s_ctrl, 0, sizeof(FI2s));
  141. memset(&i2s_dev->i2s_ctrl, 0, sizeof(FI2sConfig));
  142. i2s_dev->i2s_ctrl.data_config.word_length = word_length;
  143. i2s_dev->i2s_config = *FI2sLookupConfig(i2s_id);
  144. ret = FI2sCfgInitialize(&i2s_dev->i2s_ctrl, &i2s_dev->i2s_config);
  145. if (FI2S_SUCCESS != ret)
  146. {
  147. printf("Init the i2s failed.\r\n");
  148. return ret;
  149. }
  150. FI2sClkOutDiv(&i2s_dev->i2s_ctrl, i2s_dev->config.samplerate);
  151. FI2sTxRxEnable(&i2s_dev->i2s_ctrl, TRUE); /* 模块使能 */
  152. return ret;
  153. }
  154. static FError FI2sRxDdmaInit(struct phytium_i2s_device *i2s_dev)
  155. {
  156. FError ret = FI2S_SUCCESS;
  157. i2s_dev->ddmac_config = *FDdmaLookupConfig(i2s_dev->ddma_ctrl_id);
  158. ret = FDdmaCfgInitialize(&i2s_dev->ddmac, &i2s_dev->ddmac_config);
  159. if (FI2S_SUCCESS != ret)
  160. {
  161. printf("DDMA config initialization failed.\r\n");
  162. return ret;
  163. }
  164. return ret;
  165. }
  166. static FError FI2sDdmaDeviceRX(struct phytium_i2s_device *i2s_dev, u32 work_mode, const void *src, fsize_t total_bytes, fsize_t per_buff_len)
  167. {
  168. FError ret = FI2S_SUCCESS;
  169. fsize_t bdl_num = total_bytes / per_buff_len;
  170. rt_hw_cpu_dcache_clean((uintptr)src, total_bytes);
  171. for (u32 chan = FDDMA_CHAN_0; chan < FDDMA_NUM_OF_CHAN; chan++) /* 清除中断 */
  172. {
  173. FDdmaClearChanIrq(i2s_dev->ddmac_config.base_addr, chan, i2s_dev->ddmac_config.caps);
  174. }
  175. FDdmaBdlDesc *bdl_desc_list = rt_malloc_align(bdl_num * sizeof(FDdmaBdlDesc), FDDMA_BDL_ADDR_ALIGMENT); /* DDMA描述符首地址需128字节对齐 */
  176. if ((NULL == bdl_desc_list))
  177. {
  178. printf("FDdmaBdlDesc allocate failed.\r\n");
  179. return FDDMA_ERR_IS_USED;
  180. }
  181. memset(bdl_desc_list, 0, bdl_num * sizeof(FDdmaBdlDesc));
  182. FDdmaBdlDescConfig *bdl_desc_config = rt_calloc(1, bdl_num * sizeof(FDdmaBdlDescConfig));
  183. if ((NULL == bdl_desc_config))
  184. {
  185. printf("FDdmaBdlDescConfig allocate failed.\r\n");
  186. return FDDMA_ERR_IS_USED;
  187. }
  188. /* set BDL descriptors */
  189. for (fsize_t loop = 0; loop < bdl_num; loop++)
  190. {
  191. bdl_desc_config[loop].current_desc_num = loop;
  192. bdl_desc_config[loop].src_addr = (uintptr)(src + per_buff_len * loop);
  193. bdl_desc_config[loop].trans_length = per_buff_len;
  194. }
  195. bdl_desc_config[bdl_num -1].ioc = TRUE;
  196. /* set BDL descriptor list with descriptor configs */
  197. for (fsize_t loop = 0; loop < bdl_num; loop++)
  198. {
  199. FDdmaBDLSetDesc(bdl_desc_list, &bdl_desc_config[loop]);
  200. }
  201. i2s_dev->rx_config.slave_id = 0U,
  202. i2s_dev->rx_config.req_mode = AUDIO_PCM_STREAM_CAPTURE;
  203. i2s_dev->rx_config.ddr_addr = (uintptr)src;
  204. i2s_dev->rx_config.dev_addr = i2s_dev->i2s_config.base_addr + FI2S_RXDMA ;
  205. i2s_dev->rx_config.trans_len = total_bytes;
  206. i2s_dev->rx_config.timeout = 0xffff,
  207. i2s_dev->rx_config.first_desc_addr = (uintptr)bdl_desc_list;
  208. i2s_dev->rx_config.valid_desc_num = bdl_num;
  209. ret = FDdmaChanBdlConfigure(&i2s_dev->ddmac, i2s_dev->rx_channel, &i2s_dev->rx_config);
  210. if (ret != FI2S_SUCCESS)
  211. {
  212. printf("DDMA BDL configure failer.\r\n");
  213. return ret;
  214. }
  215. rt_free(bdl_desc_config);
  216. return ret;
  217. }
  218. void dma_transfer_callback(void *args)
  219. {
  220. #if defined(RT_USING_I2S0)
  221. rt_audio_rx_done(&i2s_dev0.audio, &i2s_dev0.rx_fifo[0], TX_RX_BUF_LEN);
  222. #endif
  223. }
  224. static rt_err_t i2s_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  225. {
  226. rt_err_t result = RT_EOK;
  227. struct phytium_i2s_device *i2s_dev;
  228. RT_ASSERT(audio != RT_NULL);
  229. i2s_dev = (struct phytium_i2s_device *)audio->parent.user_data;
  230. switch (caps->main_type)
  231. {
  232. case AUDIO_TYPE_QUERY: /* qurey the types of hw_codec device */
  233. {
  234. switch (caps->sub_type)
  235. {
  236. case AUDIO_TYPE_QUERY:
  237. caps->udata.mask = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_MIXER;
  238. break;
  239. default:
  240. result = -RT_ERROR;
  241. break;
  242. }
  243. break;
  244. }
  245. case AUDIO_TYPE_OUTPUT: /* Provide capabilities of OUTPUT unit */
  246. {
  247. switch (caps->sub_type)
  248. {
  249. case AUDIO_DSP_PARAM:
  250. caps->udata.config.samplerate = i2s_dev->config.samplerate;
  251. caps->udata.config.channels = i2s_dev->config.channels;
  252. caps->udata.config.samplebits = i2s_dev->config.samplebits;
  253. break;
  254. case AUDIO_DSP_SAMPLERATE:
  255. caps->udata.config.samplerate = i2s_dev->config.samplerate;
  256. break;
  257. case AUDIO_DSP_CHANNELS:
  258. caps->udata.config.channels = i2s_dev->config.channels;
  259. break;
  260. case AUDIO_DSP_SAMPLEBITS:
  261. caps->udata.config.samplebits = i2s_dev->config.samplebits;
  262. break;
  263. default:
  264. result = -RT_ERROR;
  265. break;
  266. }
  267. break;
  268. }
  269. case AUDIO_TYPE_MIXER: /* report the Mixer Units */
  270. {
  271. switch (caps->sub_type)
  272. {
  273. case AUDIO_MIXER_QUERY:
  274. caps->udata.mask = AUDIO_MIXER_VOLUME;
  275. break;
  276. case AUDIO_MIXER_VOLUME:
  277. caps->udata.value = i2s_dev->volume;
  278. break;
  279. default:
  280. result = -RT_ERROR;
  281. break;
  282. }
  283. break;
  284. }
  285. default:
  286. result = -RT_ERROR;
  287. break;
  288. }
  289. return result;
  290. }
  291. static rt_err_t i2s_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  292. {
  293. rt_err_t result = RT_EOK;
  294. struct phytium_i2s_device *i2s_dev;
  295. struct rt_audio_replay *replay;
  296. RT_ASSERT(audio != RT_NULL);
  297. i2s_dev = (struct phytium_i2s_device *)audio->parent.user_data;
  298. switch (caps->main_type)
  299. {
  300. case AUDIO_TYPE_INPUT:
  301. {
  302. switch (caps->sub_type)
  303. {
  304. case AUDIO_DSP_PARAM:
  305. {
  306. struct rt_audio_configure config = caps->udata.config;
  307. i2s_dev->config.channels = config.channels;
  308. i2s_dev->config.samplebits = config.samplebits;
  309. i2s_dev->config.samplerate = config.samplerate;
  310. }
  311. default:
  312. result = -RT_ERROR;
  313. break;
  314. }
  315. break;
  316. }
  317. default:
  318. break;
  319. }
  320. return result;
  321. }
  322. static rt_err_t i2s_init(struct rt_audio_device *audio)
  323. {
  324. struct phytium_i2s_device *i2s_dev;
  325. RT_ASSERT(audio != RT_NULL);
  326. i2s_dev = (struct phytium_i2s_device *)audio->parent.user_data;
  327. FError ret = FT_SUCCESS;
  328. u32 word_length = i2s_dev->config.samplebits; /* 16-bits word length */
  329. FI2sEs8336Init(word_length);
  330. if (FT_SUCCESS != ret)
  331. {
  332. printf("Init the escodec failed.\r\n");
  333. return ret;
  334. }
  335. ret = FI2sRxDdmaInit(i2s_dev);
  336. if (FT_SUCCESS != ret)
  337. {
  338. printf("Init DDMA-2 failed.\r\n");
  339. return ret;
  340. }
  341. ret = FI2sRxInit(i2s_dev, word_length);
  342. if (FI2S_SUCCESS != ret)
  343. {
  344. printf("Init the I2S failed.\r\n");
  345. return ret;
  346. }
  347. FDdmaSetupInterrupt(&i2s_dev->ddmac);
  348. FDdmaRegisterChanEvtHandler(&i2s_dev->ddmac, i2s_dev->rx_channel, FDDMA_CHAN_EVT_REQ_DONE, dma_transfer_callback, (void *)i2s_dev);
  349. return ret;
  350. }
  351. static rt_err_t i2s_start(struct rt_audio_device *audio, int stream)
  352. {
  353. struct phytium_i2s_device *i2s_dev;
  354. RT_ASSERT(audio != RT_NULL);
  355. i2s_dev = (struct phytium_i2s_device *)audio->parent.user_data;
  356. if (stream == AUDIO_STREAM_REPLAY)
  357. {
  358. }
  359. else if(stream == AUDIO_STREAM_RECORD)
  360. {
  361. FI2sDdmaDeviceRX(i2s_dev, AUDIO_PCM_STREAM_CAPTURE, &i2s_dev->rx_fifo[0], TX_RX_BUF_LEN, PER_BUFFER_SIZE);
  362. FDdmaChanActive(&i2s_dev->ddmac, i2s_dev->rx_channel);
  363. }
  364. FDdmaStart(&i2s_dev->ddmac);
  365. return RT_EOK;
  366. }
  367. static rt_err_t i2s_stop(struct rt_audio_device *audio, int stream)
  368. {
  369. struct phytium_i2s_device *i2s_dev;
  370. RT_ASSERT(audio != RT_NULL);
  371. i2s_dev = (struct phytium_i2s_device *)audio->parent.user_data;
  372. return RT_EOK;
  373. }
  374. static struct rt_audio_ops i2s_ops =
  375. {
  376. .getcaps = i2s_getcaps,
  377. .configure = i2s_configure,
  378. .init = i2s_init,
  379. .start = i2s_start,
  380. .stop = i2s_stop,
  381. .transmit = NULL,
  382. .buffer_info = NULL,
  383. };
  384. static int i2s_controller_init(struct phytium_i2s_device *i2s_dev)
  385. {
  386. struct rt_audio_device *audio = &i2s_dev->audio;
  387. i2s_dev->rx_fifo = rt_calloc(1, TX_RX_BUF_LEN);
  388. if (i2s_dev->rx_fifo == RT_NULL)
  389. {
  390. return -RT_ENOMEM;
  391. }
  392. i2s_dev->audio.ops = &i2s_ops;
  393. int ret = rt_audio_register(audio, i2s_dev->name, RT_DEVICE_FLAG_RDONLY, (void *)i2s_dev);
  394. RT_ASSERT(RT_EOK == ret);
  395. LOG_D("i2s_controller_init i2s bus reg success. \n");
  396. return ret;
  397. }
  398. int rt_hw_i2s_init(void)
  399. {
  400. #if defined(RT_USING_I2S0)
  401. i2s_dev0.name = "I2S0";
  402. i2s_dev0.i2s_ctrl.config.instance_id = FI2S0_ID;
  403. i2s_dev0.i2s_ctrl_id = FI2S0_ID;
  404. i2s_dev0.ddma_ctrl_id = FDDMA2_I2S_ID;
  405. i2s_dev0.config.channels = 1;
  406. i2s_dev0.config.samplerate = RT_I2S_SAMPLERATE;
  407. i2s_dev0.config.samplebits = RT_I2S_SAMPLEBITS;
  408. i2s_controller_init(&i2s_dev0);
  409. #endif
  410. return RT_EOK;
  411. }
  412. INIT_DEVICE_EXPORT(rt_hw_i2s_init);