drv_aic_i2s.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-11-19 Urey the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <drivers/audio.h>
  14. #include "dma.h"
  15. #ifdef RT_USING_FINSH
  16. #include <finsh.h>
  17. #endif
  18. #include "board.h"
  19. #include "drv_clock.h"
  20. #include "drv_dma.h"
  21. #include "drv_gpio.h"
  22. #include "drv_aic.h"
  23. #include "drv_aic_i2s.h"
  24. #define AIC_DEBUG 0
  25. #if AIC_DEBUG
  26. #define AIC_DBG(...) rt_kprintf("[AIC]"),rt_kprintf(__VA_ARGS__)
  27. #else
  28. #define AIC_DBG(...)
  29. #endif
  30. static struct jz_aic _g_jz_aic;
  31. int aic_set_rate(struct jz_aic *aic, uint32_t freq)
  32. {
  33. int ret;
  34. // clk_disable(aic->clk);
  35. if (aic->clk_rate != freq)
  36. {
  37. ret = clk_set_rate(aic->clk, freq);
  38. if (!ret)
  39. aic->clk_rate = clk_get_rate(aic->clk);
  40. }
  41. // clk_enable(aic->clk);
  42. AIC_DBG("aic clock = %d\n",clk_get_rate(aic->clk));
  43. return aic->clk_rate;
  44. }
  45. static void aic_irq_handler(int vector, void *param)
  46. {
  47. struct jz_aic *aic = (struct jz_aic *)param;
  48. aic->mask = __aic_get_irq_enmask(aic);
  49. if (aic->mask && (aic->mask & __aic_get_irq_flag(aic)))
  50. {
  51. /*Disable all aic interrupt*/
  52. __aic_set_irq_enmask(aic, 0);
  53. if ((aic->mask & 0x8) && __aic_test_ror(aic))
  54. {
  55. aic->ror++;
  56. AIC_DBG("recieve fifo [overrun] interrupt time [%d]\n",
  57. aic->ror);
  58. }
  59. if ((aic->mask & 0x4) && __aic_test_tur(aic))
  60. {
  61. aic->tur++;
  62. AIC_DBG("transmit fifo [underrun] interrupt time [%d]\n",
  63. aic->tur);
  64. }
  65. if ((aic->mask & 0x2) && __aic_test_rfs(aic))
  66. {
  67. AIC_DBG("[recieve] fifo at or above threshold interrupt time\n");
  68. }
  69. if ((aic->mask & 0x1) && __aic_test_tfs(aic))
  70. {
  71. AIC_DBG("[transmit] fifo at or blow threshold interrupt time\n");
  72. }
  73. /*sleep, avoid frequently interrupt*/
  74. __aic_clear_all_irq_flag(aic);
  75. __aic_set_irq_enmask(aic, aic->mask);
  76. }
  77. }
  78. struct jz_aic* _aic_init(void)
  79. {
  80. struct jz_aic *aic = &_g_jz_aic;
  81. struct rt_device *device;
  82. aic->base = AIC_BASE;
  83. aic->clk_gate = clk_get("aic");
  84. aic->clk = clk_get("cgu_i2s");
  85. if((aic->clk_gate == RT_NULL) || (aic->clk == RT_NULL))
  86. {
  87. AIC_DBG("aic or i2s clk error\n");
  88. goto aic_init_error;
  89. }
  90. /* set system clock */
  91. clk_set_rate(aic->clk, 24000000);
  92. aic->clk_rate = 24000000;
  93. clk_enable(aic->clk_gate);
  94. clk_enable(aic->clk);
  95. aic->irqno = IRQ_AIC0;
  96. aic->irqflags = 0;
  97. rt_hw_interrupt_install(IRQ_AIC0,aic_irq_handler,aic,"irq_aic");
  98. rt_hw_interrupt_umask(IRQ_AIC0);
  99. return aic;
  100. aic_init_error:
  101. clk_put(aic->clk);
  102. clk_put(aic->clk_gate);
  103. return RT_NULL;
  104. }
  105. #define I2S_DEBUG 0
  106. #if I2S_DEBUG
  107. #define I2S_DBG(...) rt_kprintf("[I2S]"),rt_kprintf(__VA_ARGS__)
  108. #else
  109. #define I2S_DBG(...)
  110. #endif
  111. #define I2S_TFIFO_DEPTH 64
  112. #define I2S_RFIFO_DEPTH 32
  113. #define I2S_OSS_FMT 16
  114. #define I2S_ISS_FMT 16
  115. #define I2S_PALY_CHANEL 2
  116. struct jz_i2s _g_jz_i2s =
  117. {
  118. .aic = 0,
  119. .i2s_init = 0,
  120. .i2s_mode = 0,
  121. .tx_dr_base = ((AIC_BASE + AICDR) & 0x1FFFFFFF),
  122. .channels = 2,
  123. .fmt_width = 16,
  124. .tx_dmac = RT_NULL,
  125. .rx_dmac = RT_NULL,
  126. };
  127. #define I2S_DMA_TX_CHAN 2
  128. #define I2S_DMA_RX_CHAN 3
  129. static void aic_i2s_trans_complete(struct rt_dma_channel *dmac, struct dma_message *msg);
  130. static void dump_registers(struct jz_aic *aic)
  131. {
  132. rt_kprintf("AIC_FR 0x%08x : 0x%08x\n", (aic->base+AICFR), jz_aic_read_reg(aic, AICFR));
  133. rt_kprintf("AIC_CR 0x%08x : 0x%08x\n", (aic->base+AICCR), jz_aic_read_reg(aic, AICCR));
  134. rt_kprintf("AIC_I2SCR 0x%08x : 0x%08x\n", (aic->base+I2SCR), jz_aic_read_reg(aic, I2SCR));
  135. rt_kprintf("AIC_SR 0x%08x : 0x%08x\n", (aic->base+AICSR), jz_aic_read_reg(aic, AICSR));
  136. rt_kprintf("AIC_I2SSR 0x%08x : 0x%08x\n", (aic->base+I2SSR), jz_aic_read_reg(aic, I2SSR));
  137. rt_kprintf("AIC_I2SDIV 0x%08x : 0x%08x\n", (aic->base+I2SDIV), jz_aic_read_reg(aic, I2SDIV));
  138. rt_kprintf("AIC_DR 0x%08x : 0x%08x\n", (aic->base+AICDR), jz_aic_read_reg(aic, AICDR));
  139. rt_kprintf("AIC_I2SCDR\t 0x%08x\n",*(volatile unsigned int*)0xb0000060);
  140. rt_kprintf("AIC_I2SCDR1\t 0x%08x\n",*(volatile unsigned int*)0xb0000070);
  141. rt_kprintf("AICSR\t 0x%08x\n",*(volatile unsigned int*)0xb0020014);
  142. return;
  143. }
  144. int dump_aic_i2s(void)
  145. {
  146. dump_registers(_g_jz_i2s.aic);
  147. return 0;
  148. }
  149. MSH_CMD_EXPORT(dump_aic_i2s,dump i2s registers...);
  150. #if 0
  151. int i2scdr_extclk(void)
  152. {
  153. rt_uint32_t regValue;
  154. regValue = readl(0xb0000060);
  155. regValue &= ~(0x01 << 30);
  156. writel(regValue,0xb0000060);
  157. rt_kprintf("AIC_I2SCDR\t 0x%08x\n",*(volatile unsigned int*)0xb0000060);
  158. }
  159. MSH_CMD_EXPORT(i2scdr_extclk,set i2s cdr ext clk...);
  160. int i2scdr_pllclk(void)
  161. {
  162. rt_uint32_t regValue;
  163. regValue = readl(0xb0000060);
  164. regValue |= (0x01 << 30);
  165. writel(regValue,0xb0000060);
  166. rt_kprintf("AIC_I2SCDR\t 0x%08x\n",*(volatile unsigned int*)0xb0000060);
  167. }
  168. MSH_CMD_EXPORT(i2scdr_pllclk,set i2s cdr pll clk...);
  169. #endif
  170. static void aic_i2s_start_substream(struct jz_i2s *i2s,int stream)
  171. {
  172. struct jz_aic *aic = i2s->aic;
  173. if(stream == AUDIO_STREAM_REPLAY)
  174. {
  175. int i = 4;
  176. I2S_DBG("codec fifo level0 %x\n", jz_aic_read_reg(aic, AICSR));
  177. for (i= 0; i < I2S_TFIFO_DEPTH ; i++)
  178. {
  179. __aic_write_txfifo(aic, 0x0);
  180. __aic_write_txfifo(aic, 0x0);
  181. }
  182. __aic_clear_tur(aic);
  183. I2S_DBG("codec fifo level1 %x\n", jz_aic_read_reg(aic, AICSR));
  184. __i2s_enable_replay(aic);
  185. while (!__aic_test_tur(aic)) ;
  186. __i2s_enable_transmit_dma(aic);
  187. __aic_clear_tur(aic);
  188. #if I2S_DEBUG
  189. __aic_en_tur_int(aic);
  190. #endif
  191. }
  192. else
  193. {
  194. __aic_flush_rxfifo(aic);
  195. rt_thread_delay(1);
  196. __i2s_enable_record(aic);
  197. __i2s_enable_receive_dma(aic);
  198. #if I2S_DEBUG
  199. __aic_en_ror_int(aic);
  200. #endif
  201. }
  202. I2S_DBG("strtup sub stream ok!\n");
  203. }
  204. static void aic_i2s_stop_substream(struct jz_i2s *i2s,int stream)
  205. {
  206. struct jz_aic *aic = i2s->aic;
  207. if(stream == AUDIO_STREAM_REPLAY)
  208. {
  209. #if I2S_DEBUG
  210. __aic_dis_tur_int(aic);
  211. #endif
  212. if (__i2s_transmit_dma_is_enable(aic))
  213. {
  214. //wait all dma queue is complete
  215. while(i2s->tx_dmac->get_index != i2s->tx_dmac->put_index)
  216. rt_thread_delay(1);
  217. __i2s_disable_transmit_dma(aic);
  218. __aic_clear_tur(aic);
  219. /*hrtime mode: stop will be happen in any where, make sure there is
  220. * no data transfer on ahb bus before stop dma
  221. */
  222. while(!__aic_test_tur(aic));
  223. }
  224. __i2s_disable_replay(aic);
  225. __aic_clear_tur(aic);
  226. }
  227. else
  228. {
  229. // if (jz_i2s_debug) __aic_dis_ror_int(aic);
  230. if (__i2s_receive_dma_is_enable(aic))
  231. {
  232. __i2s_disable_receive_dma(aic);
  233. __aic_clear_ror(aic);
  234. while(!__aic_test_ror(aic));
  235. }
  236. __i2s_disable_record(aic);
  237. __aic_clear_ror(aic);
  238. }
  239. }
  240. int aic_i2s_set_clkdiv(struct jz_i2s *i2s,int div_id, int div)
  241. {
  242. struct jz_aic *aic = i2s->aic;
  243. I2S_DBG("enter %s div_id %d div %d\n", __func__, div_id , div);
  244. /*BIT CLK fix 64FS*/
  245. /*SYS_CLK is 256, 384, 512, 768*/
  246. if (div != 256 && div != 384 && div != 512 && div != 768)
  247. return -RT_EIO;
  248. __i2s_set_dv(aic, (div/64) - 1);
  249. __i2s_set_idv(aic, (div/64) - 1);
  250. return RT_EOK;
  251. }
  252. /*
  253. * stream = CODEC_STREAM_PLAYBACK or CODEC_STREAM_CAPTURE
  254. */
  255. int aic_i2s_startup(struct jz_i2s *i2s,int stream)
  256. {
  257. struct jz_aic *aic = i2s->aic;
  258. if(!i2s->i2s_mode)
  259. {
  260. I2S_DBG("start set AIC register....\n");
  261. __aic_disable(aic);
  262. __aic_select_i2s(aic);
  263. __i2s_select_i2s_fmt(aic);
  264. #ifndef CODEC_AS_MASTER
  265. __i2s_bclk_output(aic);
  266. __i2s_sync_output(aic);
  267. #else
  268. __i2s_bclk_input(aic);
  269. __i2s_sync_input(aic);
  270. #endif
  271. aic_i2s_set_sysclk(i2s,CODEC_DEF_RATE);
  272. __i2s_play_lastsample(aic);
  273. __i2s_set_transmit_trigger(aic, I2S_TFIFO_DEPTH/4);
  274. __i2s_set_receive_trigger(aic, (I2S_RFIFO_DEPTH/4 - 1));
  275. __aic_enable(aic);
  276. }
  277. /* Set playback or record mode */
  278. if(stream == AUDIO_STREAM_REPLAY)
  279. {
  280. __i2s_send_rfirst(aic);
  281. __i2s_disable_transmit_dma(aic);
  282. __i2s_disable_replay(aic);
  283. __aic_clear_tur(aic);
  284. i2s->i2s_mode |= I2S_WRITE;
  285. }
  286. else
  287. {
  288. __i2s_disable_receive_dma(aic);
  289. __i2s_disable_record(aic);
  290. __aic_clear_ror(aic);
  291. i2s->i2s_mode |= I2S_READ;
  292. }
  293. return 0;
  294. }
  295. int aic_i2s_trigger(struct jz_i2s* i2s,int cmd,int stream)
  296. {
  297. switch (cmd)
  298. {
  299. case I2S_TRIGGER_START:
  300. case I2S_TRIGGER_RESUME:
  301. case I2S_TRIGGER_PAUSE_RELEASE:
  302. aic_i2s_start_substream(i2s,stream);
  303. break;
  304. case I2S_TRIGGER_STOP:
  305. case I2S_TRIGGER_SUSPEND:
  306. case I2S_TRIGGER_PAUSE_PUSH:
  307. default:
  308. aic_i2s_stop_substream(i2s,stream);
  309. break;
  310. }
  311. return 0;
  312. }
  313. int aic_i2s_hw_params(struct jz_i2s* i2s,int stream)
  314. {
  315. struct jz_aic *aic = i2s->aic;
  316. struct dma_config config;
  317. int trigger;
  318. int bus_width;
  319. I2S_DBG("upgrade hw params...\n");
  320. if(stream == AUDIO_STREAM_REPLAY)
  321. {
  322. /* channel */
  323. __i2s_channel(aic, i2s->channels);
  324. /* format */
  325. if(i2s->fmt_width == 8)
  326. bus_width = RT_DMA_BUSWIDTH_1_BYTE;
  327. else if(i2s->fmt_width == 16)
  328. bus_width = RT_DMA_BUSWIDTH_2_BYTES;
  329. else
  330. bus_width = RT_DMA_BUSWIDTH_4_BYTES;
  331. i2s->tx_dmac = rt_dma_get_channel(I2S_DMA_TX_CHAN);
  332. RT_ASSERT(i2s->tx_dmac != RT_NULL);
  333. if(i2s->tx_dmac != RT_NULL)
  334. {
  335. config.direction = RT_DMA_MEM_TO_DEV;
  336. config.src_addr_width = bus_width;
  337. config.src_maxburst = (64 * 1024);
  338. config.dst_addr_width = bus_width;
  339. config.dst_maxburst = (I2S_TFIFO_DEPTH * bus_width)/2;
  340. rt_dma_configture(i2s->tx_dmac,&config);
  341. i2s->tx_dmac->start = RT_NULL;
  342. i2s->tx_dmac->complete = aic_i2s_trans_complete;
  343. }
  344. __i2s_set_oss(aic, i2s->fmt_width);
  345. __i2s_set_transmit_trigger(aic, (I2S_TFIFO_DEPTH / 4));
  346. I2S_DBG("TX_DMAC config ok!\n");
  347. }
  348. else
  349. {
  350. /* format */
  351. if(i2s->fmt_width == 8)
  352. bus_width = RT_DMA_BUSWIDTH_1_BYTE;
  353. else if(i2s->fmt_width == 16)
  354. bus_width = RT_DMA_BUSWIDTH_2_BYTES;
  355. else
  356. bus_width = RT_DMA_BUSWIDTH_4_BYTES;
  357. i2s->rx_dmac = rt_dma_get_channel(I2S_DMA_RX_CHAN);
  358. if(i2s->rx_dmac != RT_NULL)
  359. {
  360. config.direction = RT_DMA_DEV_TO_MEM;
  361. config.src_addr_width = bus_width;
  362. config.src_maxburst = (I2S_RFIFO_DEPTH * bus_width)/2;
  363. config.dst_addr_width = bus_width;
  364. config.dst_maxburst = (64 * 1024);
  365. rt_dma_configture(i2s->rx_dmac,&config);
  366. i2s->rx_dmac->start = RT_NULL;
  367. i2s->rx_dmac->complete = aic_i2s_trans_complete;
  368. I2S_DBG("RX DMA config ok \n");
  369. }
  370. __i2s_set_iss(aic, i2s->fmt_width);
  371. __i2s_set_receive_trigger(aic, (I2S_RFIFO_DEPTH/4 - 1));
  372. }
  373. return 0;
  374. }
  375. void aic_i2s_shutdown(struct jz_i2s *i2s,int stream)
  376. {
  377. struct jz_aic *aic = i2s->aic;
  378. aic_i2s_stop_substream(i2s,stream);
  379. if(stream == AUDIO_STREAM_REPLAY)
  380. i2s->i2s_mode &= ~I2S_WRITE;
  381. else
  382. i2s->i2s_mode &= ~I2S_READ;
  383. if(!i2s->i2s_mode)
  384. __aic_disable(aic);
  385. }
  386. int aic_i2s_set_sysclk(struct jz_i2s *i2s,uint32_t freq)
  387. {
  388. struct jz_aic *aic = i2s->aic;
  389. #ifdef RT_USING_ICODEC
  390. __aic_select_internal_codec(aic);
  391. #else
  392. __aic_select_external_codec(aic);
  393. #endif
  394. __i2s_stop_bitclk(aic);
  395. aic_set_rate(aic, freq);
  396. __i2s_start_bitclk(aic);
  397. #ifdef CFG_AIC_SOC_CLKOUT
  398. /* Master clk output */
  399. __i2s_select_sysclk_output(aic);
  400. __i2s_enable_sysclk_output(aic);
  401. #else
  402. /* Master clk input */
  403. __i2s_select_sysclk_input(aic);
  404. __i2s_disable_sysclk_output(aic);
  405. #endif
  406. return 0;
  407. }
  408. static void aic_i2s_trans_complete(struct rt_dma_channel *dmac, struct dma_message *msg)
  409. {
  410. I2S_DBG("TAG,%d,%s\n",__LINE__,__func__);
  411. if(msg->complete_cb)
  412. {
  413. if(msg->t_mode == JZDMA_REQ_I2S0_TX)
  414. msg->complete_cb(msg->complete_arg,msg->src_addr);
  415. else
  416. msg->complete_cb(msg->complete_arg,msg->dst_addr);
  417. }
  418. }
  419. rt_size_t aic_i2s_send(struct jz_i2s *i2s, const void* buffer, rt_size_t size,void (*tx_callback)(void *,void *), void *tx_arg)
  420. {
  421. struct dma_message message;
  422. I2S_DBG("TAG,%d,%s\n",__LINE__,__func__);
  423. message.src_addr = (uint8_t *) (buffer);
  424. message.src_option = RT_DMA_ADDR_INC;
  425. message.dst_addr = (uint8_t *) (AIC_BASE + AICDR);
  426. message.dst_option = RT_DMA_ADDR_FIX;
  427. message.t_size = size;
  428. message.t_mode = JZDMA_REQ_I2S0_TX;
  429. message.complete_cb = (void *)tx_callback;
  430. message.complete_arg= tx_arg;
  431. I2S_DBG("i2s trans length = %d\n",size);
  432. if (rt_dma_trans_message(i2s->tx_dmac, &message) == RT_EOK)
  433. return size;
  434. return 0;
  435. }
  436. rt_size_t aic_i2s_recv(struct jz_i2s *i2s, void* buffer, rt_size_t size,void (*rx_callback)(void *,void *), void *rx_arg)
  437. {
  438. struct dma_message message;
  439. message.src_addr = (uint8_t *) (AIC_BASE + AICDR);
  440. message.src_option = RT_DMA_ADDR_FIX;
  441. message.dst_addr = (uint8_t *) (buffer);
  442. message.dst_option = RT_DMA_ADDR_INC;
  443. message.t_size = size;
  444. message.t_mode = JZDMA_REQ_I2S0_RX;
  445. message.complete_cb = (void *)rx_callback;
  446. message.complete_arg= rx_arg;
  447. if(rt_dma_trans_message(i2s->rx_dmac,&message) == RT_EOK)
  448. return size;
  449. return 0;
  450. }
  451. struct jz_i2s *rt_hw_aic_i2s_init(void)
  452. {
  453. struct jz_aic *aic;
  454. struct jz_i2s *i2s = &_g_jz_i2s;
  455. #ifndef RT_USING_ICODEC
  456. #ifdef CFG_AIC_SOC_CLKOUT
  457. gpio_set_func(GPIO_PORT_B, GPIO_Pin_0, GPIO_FUNC_1); // I2S_MCLK
  458. #endif
  459. gpio_set_func(GPIO_PORT_B, GPIO_Pin_1, GPIO_FUNC_1); // I2S_BCLK
  460. gpio_set_func(GPIO_PORT_B, GPIO_Pin_2, GPIO_FUNC_1); // I2S_LRCLK
  461. gpio_set_func(GPIO_PORT_B, GPIO_Pin_3, GPIO_FUNC_1); // I2S_DI
  462. gpio_set_func(GPIO_PORT_B, GPIO_Pin_4, GPIO_FUNC_1); // I2S_DO
  463. #endif
  464. I2S_DBG("TAG,%d,%s\n",__LINE__,__func__);
  465. aic = _aic_init();
  466. if(aic == RT_NULL)
  467. return RT_NULL;
  468. i2s->aic = aic;
  469. i2s->i2s_mode = 0;
  470. I2S_DBG("TAG,%d,%s\n",__LINE__,__func__);
  471. /* now ,we just support I2S playback */
  472. aic_i2s_startup(i2s,AUDIO_STREAM_REPLAY);
  473. aic_i2s_hw_params(i2s,AUDIO_STREAM_REPLAY);
  474. return i2s;
  475. }