audio_i2s.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /****************************************************************************
  2. * drivers/audio/audio_i2s.c
  3. *
  4. * Copyright (C) 2018 Pinecone Inc. All rights reserved.
  5. * Author: Zhong An <zhongan@pinecone.net>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <nuttx/audio/audio.h>
  40. #include <nuttx/audio/i2s.h>
  41. #include <nuttx/kmalloc.h>
  42. /****************************************************************************
  43. * Private Types
  44. ****************************************************************************/
  45. struct audio_i2s_s
  46. {
  47. struct audio_lowerhalf_s dev;
  48. struct i2s_dev_s *i2s;
  49. bool playback;
  50. };
  51. /****************************************************************************
  52. * Private Function Prototypes
  53. ****************************************************************************/
  54. static int audio_i2s_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
  55. FAR struct audio_caps_s *caps);
  56. static int audio_i2s_shutdown(FAR struct audio_lowerhalf_s *dev);
  57. #ifdef CONFIG_AUDIO_MULTI_SESSION
  58. static int audio_i2s_configure(FAR struct audio_lowerhalf_s *dev,
  59. FAR void *session,
  60. FAR const struct audio_caps_s *caps);
  61. static int audio_i2s_start(FAR struct audio_lowerhalf_s *dev,
  62. FAR void *session);
  63. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  64. static int audio_i2s_stop(FAR struct audio_lowerhalf_s *dev,
  65. FAR void *session);
  66. #endif
  67. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  68. static int audio_i2s_pause(FAR struct audio_lowerhalf_s *dev,
  69. FAR void *session);
  70. static int audio_i2s_resume(FAR struct audio_lowerhalf_s *dev,
  71. FAR void *session);
  72. #endif
  73. static int audio_i2s_reserve(FAR struct audio_lowerhalf_s *dev,
  74. FAR void **session);
  75. static int audio_i2s_release(FAR struct audio_lowerhalf_s *dev,
  76. FAR void *session);
  77. #else
  78. static int audio_i2s_configure(FAR struct audio_lowerhalf_s *dev,
  79. FAR const struct audio_caps_s *caps);
  80. static int audio_i2s_start(FAR struct audio_lowerhalf_s *dev);
  81. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  82. static int audio_i2s_stop(FAR struct audio_lowerhalf_s *dev);
  83. #endif
  84. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  85. static int audio_i2s_pause(FAR struct audio_lowerhalf_s *dev);
  86. static int audio_i2s_resume(FAR struct audio_lowerhalf_s *dev);
  87. #endif
  88. static int audio_i2s_reserve(FAR struct audio_lowerhalf_s *dev);
  89. static int audio_i2s_release(FAR struct audio_lowerhalf_s *dev);
  90. #endif
  91. static int audio_i2s_allocbuffer(FAR struct audio_lowerhalf_s *dev,
  92. FAR struct audio_buf_desc_s *bufdesc);
  93. static int audio_i2s_freebuffer(FAR struct audio_lowerhalf_s *dev,
  94. FAR struct audio_buf_desc_s *bufdesc);
  95. static int audio_i2s_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
  96. FAR struct ap_buffer_s *apb);
  97. static int audio_i2s_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd,
  98. unsigned long arg);
  99. static void audio_i2s_callback(struct i2s_dev_s *dev,
  100. FAR struct ap_buffer_s *apb, FAR void *arg,
  101. int result);
  102. /****************************************************************************
  103. * Private Data
  104. ****************************************************************************/
  105. static const struct audio_ops_s g_audio_i2s_ops =
  106. {
  107. .getcaps = audio_i2s_getcaps,
  108. .configure = audio_i2s_configure,
  109. .shutdown = audio_i2s_shutdown,
  110. .start = audio_i2s_start,
  111. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  112. .stop = audio_i2s_stop,
  113. #endif
  114. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  115. .pause = audio_i2s_pause,
  116. .resume = audio_i2s_resume,
  117. #endif
  118. .allocbuffer = audio_i2s_allocbuffer,
  119. .freebuffer = audio_i2s_freebuffer,
  120. .enqueuebuffer = audio_i2s_enqueuebuffer,
  121. .ioctl = audio_i2s_ioctl,
  122. .reserve = audio_i2s_reserve,
  123. .release = audio_i2s_release,
  124. };
  125. /****************************************************************************
  126. * Private Functions
  127. ****************************************************************************/
  128. static int audio_i2s_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
  129. FAR struct audio_caps_s *caps)
  130. {
  131. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  132. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  133. /* Validate the structure */
  134. DEBUGASSERT(caps && caps->ac_len >= sizeof(struct audio_caps_s));
  135. audinfo("type=%d ac_type=%d\n", type, caps->ac_type);
  136. caps->ac_format.hw = 0;
  137. caps->ac_controls.w = 0;
  138. switch (caps->ac_type)
  139. {
  140. /* Caller is querying for the types of units we support */
  141. case AUDIO_TYPE_QUERY:
  142. /* Provide our overall capabilities. The interfacing software
  143. * must then call us back for specific info for each capability.
  144. */
  145. switch (caps->ac_subtype)
  146. {
  147. case AUDIO_TYPE_QUERY:
  148. /* We don't decode any formats! Only something above us in
  149. * the audio stream can perform decoding on our behalf.
  150. */
  151. /* The types of audio units we implement */
  152. if (audio_i2s->playback)
  153. {
  154. caps->ac_controls.b[0] = AUDIO_TYPE_OUTPUT;
  155. }
  156. else
  157. {
  158. caps->ac_controls.b[0] = AUDIO_TYPE_INPUT;
  159. }
  160. caps->ac_format.hw = 1 << (AUDIO_FMT_PCM - 1);
  161. break;
  162. default:
  163. caps->ac_controls.b[0] = AUDIO_SUBFMT_END;
  164. break;
  165. }
  166. break;
  167. /* Provide capabilities of our OUTPUT unit */
  168. case AUDIO_TYPE_OUTPUT:
  169. case AUDIO_TYPE_INPUT:
  170. switch (caps->ac_subtype)
  171. {
  172. case AUDIO_TYPE_QUERY:
  173. /* Report the Sample rates we support */
  174. caps->ac_controls.hw[0] =
  175. AUDIO_SAMP_RATE_8K | AUDIO_SAMP_RATE_11K |
  176. AUDIO_SAMP_RATE_16K | AUDIO_SAMP_RATE_22K |
  177. AUDIO_SAMP_RATE_32K | AUDIO_SAMP_RATE_44K |
  178. AUDIO_SAMP_RATE_48K | AUDIO_SAMP_RATE_96K |
  179. AUDIO_SAMP_RATE_128K | AUDIO_SAMP_RATE_160K |
  180. AUDIO_SAMP_RATE_172K | AUDIO_SAMP_RATE_192K;
  181. break;
  182. default:
  183. I2S_IOCTL(i2s, AUDIOIOC_GETCAPS, (unsigned long)caps);
  184. break;
  185. }
  186. break;
  187. default:
  188. I2S_IOCTL(i2s, AUDIOIOC_GETCAPS, (unsigned long)caps);
  189. break;
  190. }
  191. return caps->ac_len;
  192. }
  193. #ifdef CONFIG_AUDIO_MULTI_SESSION
  194. static int audio_i2s_configure(FAR struct audio_lowerhalf_s *dev,
  195. FAR void *session,
  196. FAR const struct audio_caps_s *caps)
  197. #else
  198. static int audio_i2s_configure(FAR struct audio_lowerhalf_s *dev,
  199. FAR const struct audio_caps_s *caps)
  200. #endif
  201. {
  202. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  203. FAR struct i2s_dev_s *i2s;
  204. int samprate, nchannels, bpsamp;
  205. int ret = OK;
  206. DEBUGASSERT(audio_i2s != NULL && caps != NULL);
  207. i2s = audio_i2s->i2s;
  208. audinfo("ac_type: %d\n", caps->ac_type);
  209. /* Process the configure operation */
  210. switch (caps->ac_type)
  211. {
  212. case AUDIO_TYPE_OUTPUT:
  213. case AUDIO_TYPE_INPUT:
  214. /* Save the current stream configuration */
  215. samprate = caps->ac_controls.hw[0] |
  216. (caps->ac_controls.b[3] << 16);
  217. nchannels = caps->ac_channels;
  218. bpsamp = caps->ac_controls.b[2];
  219. if (audio_i2s->playback)
  220. {
  221. I2S_TXCHANNELS(i2s, nchannels);
  222. I2S_TXDATAWIDTH(i2s, bpsamp);
  223. I2S_TXSAMPLERATE(i2s, samprate);
  224. }
  225. else
  226. {
  227. I2S_RXCHANNELS(i2s, nchannels);
  228. I2S_RXDATAWIDTH(i2s, bpsamp);
  229. I2S_RXSAMPLERATE(i2s, samprate);
  230. }
  231. break;
  232. default:
  233. ret = I2S_IOCTL(i2s, AUDIOIOC_CONFIGURE, (unsigned long)caps);
  234. break;
  235. }
  236. return ret;
  237. }
  238. static int audio_i2s_shutdown(FAR struct audio_lowerhalf_s *dev)
  239. {
  240. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  241. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  242. return I2S_IOCTL(i2s, AUDIOIOC_SHUTDOWN, audio_i2s->playback);
  243. }
  244. #ifdef CONFIG_AUDIO_MULTI_SESSION
  245. static int audio_i2s_start(FAR struct audio_lowerhalf_s *dev,
  246. FAR void *session)
  247. #else
  248. static int audio_i2s_start(FAR struct audio_lowerhalf_s *dev)
  249. #endif
  250. {
  251. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  252. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  253. return I2S_IOCTL(i2s, AUDIOIOC_START, audio_i2s->playback);
  254. }
  255. #ifndef CONFIG_AUDIO_EXCLUDE_STOP
  256. #ifdef CONFIG_AUDIO_MULTI_SESSION
  257. static int audio_i2s_stop(FAR struct audio_lowerhalf_s *dev,
  258. FAR void *session)
  259. #else
  260. static int audio_i2s_stop(FAR struct audio_lowerhalf_s *dev)
  261. #endif
  262. {
  263. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  264. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  265. return I2S_IOCTL(i2s, AUDIOIOC_STOP, audio_i2s->playback);
  266. }
  267. #endif
  268. #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
  269. #ifdef CONFIG_AUDIO_MULTI_SESSION
  270. static int audio_i2s_pause(FAR struct audio_lowerhalf_s *dev,
  271. FAR void *session)
  272. #else
  273. static int audio_i2s_pause(FAR struct audio_lowerhalf_s *dev)
  274. #endif
  275. {
  276. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  277. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  278. return I2S_IOCTL(i2s, AUDIOIOC_PAUSE, audio_i2s->playback);
  279. }
  280. #ifdef CONFIG_AUDIO_MULTI_SESSION
  281. static int audio_i2s_resume(FAR struct audio_lowerhalf_s *dev,
  282. FAR void *session)
  283. #else
  284. static int audio_i2s_resume(FAR struct audio_lowerhalf_s *dev)
  285. #endif
  286. {
  287. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  288. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  289. return I2S_IOCTL(i2s, AUDIOIOC_RESUME, audio_i2s->playback);
  290. }
  291. #endif
  292. static int audio_i2s_allocbuffer(FAR struct audio_lowerhalf_s *dev,
  293. FAR struct audio_buf_desc_s *bufdesc)
  294. {
  295. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  296. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  297. return I2S_IOCTL(i2s, AUDIOIOC_ALLOCBUFFER, (unsigned long)bufdesc);
  298. }
  299. static int audio_i2s_freebuffer(FAR struct audio_lowerhalf_s *dev,
  300. FAR struct audio_buf_desc_s *bufdesc)
  301. {
  302. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  303. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  304. return I2S_IOCTL(i2s, AUDIOIOC_FREEBUFFER, (unsigned long)bufdesc);
  305. }
  306. static int audio_i2s_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
  307. FAR struct ap_buffer_s *apb)
  308. {
  309. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  310. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  311. if (audio_i2s->playback)
  312. {
  313. return I2S_SEND(i2s, apb, audio_i2s_callback, audio_i2s, 0);
  314. }
  315. else
  316. {
  317. return I2S_RECEIVE(i2s, apb, audio_i2s_callback, audio_i2s, 0);
  318. }
  319. }
  320. static int audio_i2s_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd,
  321. unsigned long arg)
  322. {
  323. FAR struct audio_i2s_s *audio_i2s = (struct audio_i2s_s *)dev;
  324. FAR struct i2s_dev_s *i2s = audio_i2s->i2s;
  325. return I2S_IOCTL(i2s, cmd, arg);
  326. }
  327. #ifdef CONFIG_AUDIO_MULTI_SESSION
  328. static int audio_i2s_reserve(FAR struct audio_lowerhalf_s *dev,
  329. FAR void **session)
  330. #else
  331. static int audio_i2s_reserve(FAR struct audio_lowerhalf_s *dev)
  332. #endif
  333. {
  334. #ifdef CONFIG_AUDIO_MULTI_SESSION
  335. *session = (void *)audio_i2s->playback;
  336. #endif
  337. return OK;
  338. }
  339. #ifdef CONFIG_AUDIO_MULTI_SESSION
  340. static int audio_i2s_release(FAR struct audio_lowerhalf_s *dev,
  341. FAR void *session)
  342. #else
  343. static int audio_i2s_release(FAR struct audio_lowerhalf_s *dev)
  344. #endif
  345. {
  346. return OK;
  347. }
  348. static void audio_i2s_callback(struct i2s_dev_s *dev,
  349. FAR struct ap_buffer_s *apb,
  350. FAR void *arg, int result)
  351. {
  352. FAR struct audio_i2s_s *audio_i2s = arg;
  353. bool final = false;
  354. if ((apb->flags & AUDIO_APB_FINAL) != 0)
  355. {
  356. final = true;
  357. }
  358. #ifdef CONFIG_AUDIO_MULTI_SESSION
  359. audio_i2s->dev.upper(audio_i2s->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb,
  360. OK, NULL);
  361. #else
  362. audio_i2s->dev.upper(audio_i2s->dev.priv, AUDIO_CALLBACK_DEQUEUE, apb,
  363. OK);
  364. #endif
  365. if (final)
  366. {
  367. #ifdef CONFIG_AUDIO_MULTI_SESSION
  368. audio_i2s->dev.upper(audio_i2s->dev.priv, AUDIO_CALLBACK_COMPLETE,
  369. NULL, OK, NULL);
  370. #else
  371. audio_i2s->dev.upper(audio_i2s->dev.priv, AUDIO_CALLBACK_COMPLETE,
  372. NULL, OK);
  373. #endif
  374. }
  375. }
  376. /****************************************************************************
  377. * Public Functions
  378. ****************************************************************************/
  379. FAR struct audio_lowerhalf_s *audio_i2s_initialize(FAR struct i2s_dev_s *i2s,
  380. bool playback)
  381. {
  382. FAR struct audio_i2s_s *audio_i2s;
  383. if (i2s == NULL)
  384. {
  385. return NULL;
  386. }
  387. audio_i2s = kmm_zalloc(sizeof(struct audio_i2s_s));
  388. if (audio_i2s == NULL)
  389. {
  390. return NULL;
  391. }
  392. audio_i2s->playback = playback;
  393. audio_i2s->i2s = i2s;
  394. audio_i2s->dev.ops = &g_audio_i2s_ops;
  395. return &audio_i2s->dev;
  396. }