drv_sound.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Date Author Notes
  7. * 2019-07-23 Zero-Free first implementation
  8. */
  9. #include <rtthread.h>
  10. #include <rtdevice.h>
  11. #include <drv_sound.h>
  12. #include <drv_pl041.h>
  13. #include <drv_ac97.h>
  14. #define DBG_TAG "drv.sound"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #define TX_FIFO_SIZE (3840)
  18. struct sound_device
  19. {
  20. struct rt_audio_device audio;
  21. struct rt_audio_configure replay_config;
  22. rt_uint8_t *tx_fifo;
  23. rt_uint8_t volume;
  24. };
  25. static struct sound_device snd_dev = {0};
  26. static void rt_hw_aaci_isr(rt_uint32_t status, void *user_data)
  27. {
  28. if (status & AACI_SR_TXHE)
  29. {
  30. rt_audio_tx_complete(&snd_dev.audio);
  31. }
  32. }
  33. static rt_err_t sound_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  34. {
  35. rt_err_t result = RT_EOK;
  36. struct sound_device *snd_dev;
  37. RT_ASSERT(audio != RT_NULL);
  38. snd_dev = (struct sound_device *)audio->parent.user_data;
  39. switch (caps->main_type)
  40. {
  41. case AUDIO_TYPE_QUERY: /* qurey the types of hw_codec device */
  42. {
  43. switch (caps->sub_type)
  44. {
  45. case AUDIO_TYPE_QUERY:
  46. caps->udata.mask = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_MIXER;
  47. break;
  48. default:
  49. result = -RT_ERROR;
  50. break;
  51. }
  52. break;
  53. }
  54. case AUDIO_TYPE_OUTPUT: /* Provide capabilities of OUTPUT unit */
  55. {
  56. switch (caps->sub_type)
  57. {
  58. case AUDIO_DSP_PARAM:
  59. caps->udata.config.samplerate = snd_dev->replay_config.samplerate;
  60. caps->udata.config.channels = snd_dev->replay_config.channels;
  61. caps->udata.config.samplebits = snd_dev->replay_config.samplebits;
  62. break;
  63. case AUDIO_DSP_SAMPLERATE:
  64. caps->udata.config.samplerate = snd_dev->replay_config.samplerate;
  65. break;
  66. case AUDIO_DSP_CHANNELS:
  67. caps->udata.config.channels = snd_dev->replay_config.channels;
  68. break;
  69. case AUDIO_DSP_SAMPLEBITS:
  70. caps->udata.config.samplebits = snd_dev->replay_config.samplebits;
  71. break;
  72. default:
  73. result = -RT_ERROR;
  74. break;
  75. }
  76. break;
  77. }
  78. case AUDIO_TYPE_MIXER: /* report the Mixer Units */
  79. {
  80. switch (caps->sub_type)
  81. {
  82. case AUDIO_MIXER_QUERY:
  83. caps->udata.mask = AUDIO_MIXER_VOLUME;
  84. break;
  85. case AUDIO_MIXER_VOLUME:
  86. caps->udata.value = snd_dev->volume;
  87. break;
  88. default:
  89. result = -RT_ERROR;
  90. break;
  91. }
  92. break;
  93. }
  94. default:
  95. result = -RT_ERROR;
  96. break;
  97. }
  98. return result;
  99. }
  100. static rt_err_t sound_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  101. {
  102. rt_err_t result = RT_EOK;
  103. struct sound_device *snd_dev;
  104. struct rt_audio_replay *replay;
  105. RT_ASSERT(audio != RT_NULL);
  106. snd_dev = (struct sound_device *)audio->parent.user_data;
  107. switch (caps->main_type)
  108. {
  109. case AUDIO_TYPE_MIXER:
  110. {
  111. switch (caps->sub_type)
  112. {
  113. case AUDIO_MIXER_VOLUME:
  114. {
  115. rt_uint8_t volume = caps->udata.value;
  116. snd_dev->volume = volume;
  117. ac97_set_vol(volume);
  118. LOG_I("set volume %d", volume);
  119. break;
  120. }
  121. default:
  122. result = -RT_ERROR;
  123. break;
  124. }
  125. break;
  126. }
  127. case AUDIO_TYPE_OUTPUT:
  128. {
  129. switch (caps->sub_type)
  130. {
  131. case AUDIO_DSP_PARAM:
  132. {
  133. /* set samplerate */
  134. ac97_set_rate(caps->udata.config.samplerate);
  135. /* update buffer fifo informaition according samplerate */
  136. replay = snd_dev->audio.replay;
  137. replay->buf_info.total_size = caps->udata.config.samplerate / 50 * 4;
  138. replay->buf_info.block_size = replay->buf_info.total_size / 2;
  139. /* save configs */
  140. snd_dev->replay_config.samplerate = caps->udata.config.samplerate;
  141. snd_dev->replay_config.channels = caps->udata.config.channels;
  142. snd_dev->replay_config.samplebits = caps->udata.config.samplebits;
  143. LOG_D("set samplerate %d", snd_dev->replay_config.samplerate);
  144. break;
  145. }
  146. case AUDIO_DSP_SAMPLERATE:
  147. {
  148. ac97_set_rate(caps->udata.config.samplerate);
  149. snd_dev->replay_config.samplerate = caps->udata.config.samplerate;
  150. LOG_D("set samplerate %d", snd_dev->replay_config.samplerate);
  151. break;
  152. }
  153. case AUDIO_DSP_CHANNELS:
  154. {
  155. /* not support */
  156. snd_dev->replay_config.channels = caps->udata.config.channels;
  157. LOG_D("set channels %d", snd_dev->replay_config.channels);
  158. break;
  159. }
  160. case AUDIO_DSP_SAMPLEBITS:
  161. {
  162. /* not support */
  163. snd_dev->replay_config.samplebits = caps->udata.config.samplebits;
  164. break;
  165. }
  166. default:
  167. result = -RT_ERROR;
  168. break;
  169. }
  170. break;
  171. }
  172. default:
  173. break;
  174. }
  175. return result;
  176. }
  177. static rt_err_t sound_init(struct rt_audio_device *audio)
  178. {
  179. rt_err_t result = RT_EOK;
  180. struct sound_device *snd_dev;
  181. struct pl041_cfg _cfg;
  182. RT_ASSERT(audio != RT_NULL);
  183. snd_dev = (struct sound_device *)audio->parent.user_data;
  184. aaci_pl041_init();
  185. _cfg.itype = PL041_CHANNEL_LEFT_ADC | PL041_CHANNEL_RIGHT_ADC;
  186. _cfg.otype = PL041_CHANNEL_LEFT_DAC | PL041_CHANNEL_RIGHT_DAC;
  187. _cfg.vol = snd_dev->volume;
  188. _cfg.rate = snd_dev->replay_config.samplerate;
  189. ac97_reset();
  190. aaci_pl041_channel_cfg(0, &_cfg);
  191. aaci_pl041_irq_register(0, rt_hw_aaci_isr, RT_NULL);
  192. return result;
  193. }
  194. static rt_err_t sound_start(struct rt_audio_device *audio, int stream)
  195. {
  196. RT_ASSERT(audio != RT_NULL);
  197. if (stream == AUDIO_STREAM_REPLAY)
  198. {
  199. LOG_D("open sound device");
  200. aaci_pl041_channel_enable(0);
  201. aaci_pl041_irq_enable(0, AACI_IE_UR | AACI_IE_TX | AACI_IE_TXC);
  202. }
  203. return RT_EOK;
  204. }
  205. static rt_err_t sound_stop(struct rt_audio_device *audio, int stream)
  206. {
  207. RT_ASSERT(audio != RT_NULL);
  208. if (stream == AUDIO_STREAM_REPLAY)
  209. {
  210. /* wait codec free */
  211. rt_thread_mdelay(100);
  212. /* disable irq and channels 0 */
  213. aaci_pl041_irq_disable(0, AACI_IE_UR | AACI_IE_TX | AACI_IE_TXC);
  214. aaci_pl041_channel_disable(0);
  215. LOG_D("close sound device");
  216. }
  217. return RT_EOK;
  218. }
  219. static void sound_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info)
  220. {
  221. struct sound_device *snd_dev;
  222. RT_ASSERT(audio != RT_NULL);
  223. snd_dev = (struct sound_device *)audio->parent.user_data;
  224. /**
  225. * TX_FIFO
  226. * +----------------+----------------+
  227. * | block1 | block2 |
  228. * +----------------+----------------+
  229. * \ block_size /
  230. */
  231. info->buffer = snd_dev->tx_fifo;
  232. info->total_size = TX_FIFO_SIZE;
  233. info->block_size = TX_FIFO_SIZE/2;
  234. info->block_count = 2;
  235. }
  236. static rt_size_t sound_transmit(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size)
  237. {
  238. RT_ASSERT(audio != RT_NULL);
  239. /* write data to channel_0 fifo */
  240. aaci_pl041_channel_write(0, (rt_uint16_t *)writeBuf, size >> 1);
  241. return size;
  242. }
  243. static struct rt_audio_ops snd_ops =
  244. {
  245. .getcaps = sound_getcaps,
  246. .configure = sound_configure,
  247. .init = sound_init,
  248. .start = sound_start,
  249. .stop = sound_stop,
  250. .transmit = sound_transmit,
  251. .buffer_info = sound_buffer_info,
  252. };
  253. int rt_hw_audio_init(void)
  254. {
  255. rt_uint8_t *tx_fifo;
  256. if (snd_dev.tx_fifo)
  257. return RT_EOK;
  258. tx_fifo = rt_malloc(TX_FIFO_SIZE);
  259. if (tx_fifo == RT_NULL)
  260. return -RT_ENOMEM;
  261. rt_memset(tx_fifo, 0, TX_FIFO_SIZE);
  262. snd_dev.tx_fifo = tx_fifo;
  263. /* init default configuration */
  264. {
  265. snd_dev.replay_config.samplerate = 44100;
  266. snd_dev.replay_config.channels = 2;
  267. snd_dev.replay_config.samplebits = 16;
  268. snd_dev.volume = 55;
  269. }
  270. /* register sound device */
  271. snd_dev.audio.ops = &snd_ops;
  272. rt_audio_register(&snd_dev.audio, "sound0", RT_DEVICE_FLAG_WRONLY, &snd_dev);
  273. return RT_EOK;
  274. }
  275. INIT_DEVICE_EXPORT(rt_hw_audio_init);