q6asm-dai.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2018, Linaro Limited
  4. #include <linux/init.h>
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/slab.h>
  9. #include <sound/soc.h>
  10. #include <sound/soc-dapm.h>
  11. #include <sound/pcm.h>
  12. #include <linux/spinlock.h>
  13. #include <sound/compress_driver.h>
  14. #include <asm/dma.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/of_device.h>
  17. #include <sound/pcm_params.h>
  18. #include "q6asm.h"
  19. #include "q6routing.h"
  20. #include "q6dsp-errno.h"
  21. #define DRV_NAME "q6asm-fe-dai"
  22. #define PLAYBACK_MIN_NUM_PERIODS 2
  23. #define PLAYBACK_MAX_NUM_PERIODS 8
  24. #define PLAYBACK_MAX_PERIOD_SIZE 65536
  25. #define PLAYBACK_MIN_PERIOD_SIZE 128
  26. #define CAPTURE_MIN_NUM_PERIODS 2
  27. #define CAPTURE_MAX_NUM_PERIODS 8
  28. #define CAPTURE_MAX_PERIOD_SIZE 4096
  29. #define CAPTURE_MIN_PERIOD_SIZE 320
  30. #define SID_MASK_DEFAULT 0xF
  31. /* Default values used if user space does not set */
  32. #define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024)
  33. #define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024)
  34. #define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4)
  35. #define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4)
  36. #define ALAC_CH_LAYOUT_MONO ((101 << 16) | 1)
  37. #define ALAC_CH_LAYOUT_STEREO ((101 << 16) | 2)
  38. enum stream_state {
  39. Q6ASM_STREAM_IDLE = 0,
  40. Q6ASM_STREAM_STOPPED,
  41. Q6ASM_STREAM_RUNNING,
  42. };
  43. struct q6asm_dai_rtd {
  44. struct snd_pcm_substream *substream;
  45. struct snd_compr_stream *cstream;
  46. struct snd_codec codec;
  47. struct snd_dma_buffer dma_buffer;
  48. spinlock_t lock;
  49. phys_addr_t phys;
  50. unsigned int pcm_size;
  51. unsigned int pcm_count;
  52. unsigned int pcm_irq_pos; /* IRQ position */
  53. unsigned int periods;
  54. unsigned int bytes_sent;
  55. unsigned int bytes_received;
  56. unsigned int copied_total;
  57. uint16_t bits_per_sample;
  58. uint16_t source; /* Encoding source bit mask */
  59. struct audio_client *audio_client;
  60. uint32_t next_track_stream_id;
  61. bool next_track;
  62. uint32_t stream_id;
  63. uint16_t session_id;
  64. enum stream_state state;
  65. uint32_t initial_samples_drop;
  66. uint32_t trailing_samples_drop;
  67. bool notify_on_drain;
  68. };
  69. struct q6asm_dai_data {
  70. struct snd_soc_dai_driver *dais;
  71. int num_dais;
  72. long long int sid;
  73. };
  74. static const struct snd_pcm_hardware q6asm_dai_hardware_capture = {
  75. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH |
  76. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  77. SNDRV_PCM_INFO_MMAP_VALID |
  78. SNDRV_PCM_INFO_INTERLEAVED |
  79. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  80. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  81. SNDRV_PCM_FMTBIT_S24_LE),
  82. .rates = SNDRV_PCM_RATE_8000_48000,
  83. .rate_min = 8000,
  84. .rate_max = 48000,
  85. .channels_min = 1,
  86. .channels_max = 4,
  87. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  88. CAPTURE_MAX_PERIOD_SIZE,
  89. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  90. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  91. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  92. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  93. .fifo_size = 0,
  94. };
  95. static struct snd_pcm_hardware q6asm_dai_hardware_playback = {
  96. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH |
  97. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  98. SNDRV_PCM_INFO_MMAP_VALID |
  99. SNDRV_PCM_INFO_INTERLEAVED |
  100. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  101. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  102. SNDRV_PCM_FMTBIT_S24_LE),
  103. .rates = SNDRV_PCM_RATE_8000_192000,
  104. .rate_min = 8000,
  105. .rate_max = 192000,
  106. .channels_min = 1,
  107. .channels_max = 8,
  108. .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS *
  109. PLAYBACK_MAX_PERIOD_SIZE),
  110. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  111. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  112. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  113. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  114. .fifo_size = 0,
  115. };
  116. #define Q6ASM_FEDAI_DRIVER(num) { \
  117. .playback = { \
  118. .stream_name = "MultiMedia"#num" Playback", \
  119. .rates = (SNDRV_PCM_RATE_8000_192000| \
  120. SNDRV_PCM_RATE_KNOT), \
  121. .formats = (SNDRV_PCM_FMTBIT_S16_LE | \
  122. SNDRV_PCM_FMTBIT_S24_LE), \
  123. .channels_min = 1, \
  124. .channels_max = 8, \
  125. .rate_min = 8000, \
  126. .rate_max = 192000, \
  127. }, \
  128. .capture = { \
  129. .stream_name = "MultiMedia"#num" Capture", \
  130. .rates = (SNDRV_PCM_RATE_8000_48000| \
  131. SNDRV_PCM_RATE_KNOT), \
  132. .formats = (SNDRV_PCM_FMTBIT_S16_LE | \
  133. SNDRV_PCM_FMTBIT_S24_LE), \
  134. .channels_min = 1, \
  135. .channels_max = 4, \
  136. .rate_min = 8000, \
  137. .rate_max = 48000, \
  138. }, \
  139. .name = "MultiMedia"#num, \
  140. .id = MSM_FRONTEND_DAI_MULTIMEDIA##num, \
  141. }
  142. /* Conventional and unconventional sample rate supported */
  143. static unsigned int supported_sample_rates[] = {
  144. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  145. 88200, 96000, 176400, 192000
  146. };
  147. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  148. .count = ARRAY_SIZE(supported_sample_rates),
  149. .list = supported_sample_rates,
  150. .mask = 0,
  151. };
  152. static const struct snd_compr_codec_caps q6asm_compr_caps = {
  153. .num_descriptors = 1,
  154. .descriptor[0].max_ch = 2,
  155. .descriptor[0].sample_rates = { 8000, 11025, 12000, 16000, 22050,
  156. 24000, 32000, 44100, 48000, 88200,
  157. 96000, 176400, 192000 },
  158. .descriptor[0].num_sample_rates = 13,
  159. .descriptor[0].bit_rate[0] = 320,
  160. .descriptor[0].bit_rate[1] = 128,
  161. .descriptor[0].num_bitrates = 2,
  162. .descriptor[0].profiles = 0,
  163. .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,
  164. .descriptor[0].formats = 0,
  165. };
  166. static void event_handler(uint32_t opcode, uint32_t token,
  167. void *payload, void *priv)
  168. {
  169. struct q6asm_dai_rtd *prtd = priv;
  170. struct snd_pcm_substream *substream = prtd->substream;
  171. switch (opcode) {
  172. case ASM_CLIENT_EVENT_CMD_RUN_DONE:
  173. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  174. q6asm_write_async(prtd->audio_client, prtd->stream_id,
  175. prtd->pcm_count, 0, 0, 0);
  176. break;
  177. case ASM_CLIENT_EVENT_CMD_EOS_DONE:
  178. prtd->state = Q6ASM_STREAM_STOPPED;
  179. break;
  180. case ASM_CLIENT_EVENT_DATA_WRITE_DONE: {
  181. prtd->pcm_irq_pos += prtd->pcm_count;
  182. snd_pcm_period_elapsed(substream);
  183. if (prtd->state == Q6ASM_STREAM_RUNNING)
  184. q6asm_write_async(prtd->audio_client, prtd->stream_id,
  185. prtd->pcm_count, 0, 0, 0);
  186. break;
  187. }
  188. case ASM_CLIENT_EVENT_DATA_READ_DONE:
  189. prtd->pcm_irq_pos += prtd->pcm_count;
  190. snd_pcm_period_elapsed(substream);
  191. if (prtd->state == Q6ASM_STREAM_RUNNING)
  192. q6asm_read(prtd->audio_client, prtd->stream_id);
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. static int q6asm_dai_prepare(struct snd_soc_component *component,
  199. struct snd_pcm_substream *substream)
  200. {
  201. struct snd_pcm_runtime *runtime = substream->runtime;
  202. struct snd_soc_pcm_runtime *soc_prtd = asoc_substream_to_rtd(substream);
  203. struct q6asm_dai_rtd *prtd = runtime->private_data;
  204. struct q6asm_dai_data *pdata;
  205. struct device *dev = component->dev;
  206. int ret, i;
  207. pdata = snd_soc_component_get_drvdata(component);
  208. if (!pdata)
  209. return -EINVAL;
  210. if (!prtd || !prtd->audio_client) {
  211. dev_err(dev, "%s: private data null or audio client freed\n",
  212. __func__);
  213. return -EINVAL;
  214. }
  215. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  216. prtd->pcm_irq_pos = 0;
  217. /* rate and channels are sent to audio driver */
  218. if (prtd->state) {
  219. /* clear the previous setup if any */
  220. q6asm_cmd(prtd->audio_client, prtd->stream_id, CMD_CLOSE);
  221. q6asm_unmap_memory_regions(substream->stream,
  222. prtd->audio_client);
  223. q6routing_stream_close(soc_prtd->dai_link->id,
  224. substream->stream);
  225. }
  226. ret = q6asm_map_memory_regions(substream->stream, prtd->audio_client,
  227. prtd->phys,
  228. (prtd->pcm_size / prtd->periods),
  229. prtd->periods);
  230. if (ret < 0) {
  231. dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n",
  232. ret);
  233. return -ENOMEM;
  234. }
  235. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  236. ret = q6asm_open_write(prtd->audio_client, prtd->stream_id,
  237. FORMAT_LINEAR_PCM,
  238. 0, prtd->bits_per_sample, false);
  239. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  240. ret = q6asm_open_read(prtd->audio_client, prtd->stream_id,
  241. FORMAT_LINEAR_PCM,
  242. prtd->bits_per_sample);
  243. }
  244. if (ret < 0) {
  245. dev_err(dev, "%s: q6asm_open_write failed\n", __func__);
  246. goto open_err;
  247. }
  248. prtd->session_id = q6asm_get_session_id(prtd->audio_client);
  249. ret = q6routing_stream_open(soc_prtd->dai_link->id, LEGACY_PCM_MODE,
  250. prtd->session_id, substream->stream);
  251. if (ret) {
  252. dev_err(dev, "%s: stream reg failed ret:%d\n", __func__, ret);
  253. goto routing_err;
  254. }
  255. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  256. ret = q6asm_media_format_block_multi_ch_pcm(
  257. prtd->audio_client, prtd->stream_id,
  258. runtime->rate, runtime->channels, NULL,
  259. prtd->bits_per_sample);
  260. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  261. ret = q6asm_enc_cfg_blk_pcm_format_support(prtd->audio_client,
  262. prtd->stream_id,
  263. runtime->rate,
  264. runtime->channels,
  265. prtd->bits_per_sample);
  266. /* Queue the buffers */
  267. for (i = 0; i < runtime->periods; i++)
  268. q6asm_read(prtd->audio_client, prtd->stream_id);
  269. }
  270. if (ret < 0)
  271. dev_info(dev, "%s: CMD Format block failed\n", __func__);
  272. else
  273. prtd->state = Q6ASM_STREAM_RUNNING;
  274. return ret;
  275. routing_err:
  276. q6asm_cmd(prtd->audio_client, prtd->stream_id, CMD_CLOSE);
  277. open_err:
  278. q6asm_unmap_memory_regions(substream->stream, prtd->audio_client);
  279. q6asm_audio_client_free(prtd->audio_client);
  280. prtd->audio_client = NULL;
  281. return ret;
  282. }
  283. static int q6asm_dai_trigger(struct snd_soc_component *component,
  284. struct snd_pcm_substream *substream, int cmd)
  285. {
  286. int ret = 0;
  287. struct snd_pcm_runtime *runtime = substream->runtime;
  288. struct q6asm_dai_rtd *prtd = runtime->private_data;
  289. switch (cmd) {
  290. case SNDRV_PCM_TRIGGER_START:
  291. case SNDRV_PCM_TRIGGER_RESUME:
  292. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  293. ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id,
  294. 0, 0, 0);
  295. break;
  296. case SNDRV_PCM_TRIGGER_STOP:
  297. prtd->state = Q6ASM_STREAM_STOPPED;
  298. ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
  299. CMD_EOS);
  300. break;
  301. case SNDRV_PCM_TRIGGER_SUSPEND:
  302. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  303. ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
  304. CMD_PAUSE);
  305. break;
  306. default:
  307. ret = -EINVAL;
  308. break;
  309. }
  310. return ret;
  311. }
  312. static int q6asm_dai_open(struct snd_soc_component *component,
  313. struct snd_pcm_substream *substream)
  314. {
  315. struct snd_pcm_runtime *runtime = substream->runtime;
  316. struct snd_soc_pcm_runtime *soc_prtd = asoc_substream_to_rtd(substream);
  317. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_prtd, 0);
  318. struct q6asm_dai_rtd *prtd;
  319. struct q6asm_dai_data *pdata;
  320. struct device *dev = component->dev;
  321. int ret = 0;
  322. int stream_id;
  323. stream_id = cpu_dai->driver->id;
  324. pdata = snd_soc_component_get_drvdata(component);
  325. if (!pdata) {
  326. dev_err(dev, "Drv data not found ..\n");
  327. return -EINVAL;
  328. }
  329. prtd = kzalloc(sizeof(struct q6asm_dai_rtd), GFP_KERNEL);
  330. if (prtd == NULL)
  331. return -ENOMEM;
  332. prtd->substream = substream;
  333. prtd->audio_client = q6asm_audio_client_alloc(dev,
  334. (q6asm_cb)event_handler, prtd, stream_id,
  335. LEGACY_PCM_MODE);
  336. if (IS_ERR(prtd->audio_client)) {
  337. dev_info(dev, "%s: Could not allocate memory\n", __func__);
  338. ret = PTR_ERR(prtd->audio_client);
  339. kfree(prtd);
  340. return ret;
  341. }
  342. /* DSP expects stream id from 1 */
  343. prtd->stream_id = 1;
  344. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  345. runtime->hw = q6asm_dai_hardware_playback;
  346. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  347. runtime->hw = q6asm_dai_hardware_capture;
  348. ret = snd_pcm_hw_constraint_list(runtime, 0,
  349. SNDRV_PCM_HW_PARAM_RATE,
  350. &constraints_sample_rates);
  351. if (ret < 0)
  352. dev_info(dev, "snd_pcm_hw_constraint_list failed\n");
  353. /* Ensure that buffer size is a multiple of period size */
  354. ret = snd_pcm_hw_constraint_integer(runtime,
  355. SNDRV_PCM_HW_PARAM_PERIODS);
  356. if (ret < 0)
  357. dev_info(dev, "snd_pcm_hw_constraint_integer failed\n");
  358. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  359. ret = snd_pcm_hw_constraint_minmax(runtime,
  360. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  361. PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  362. PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  363. if (ret < 0) {
  364. dev_err(dev, "constraint for buffer bytes min max ret = %d\n",
  365. ret);
  366. }
  367. }
  368. ret = snd_pcm_hw_constraint_step(runtime, 0,
  369. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  370. if (ret < 0) {
  371. dev_err(dev, "constraint for period bytes step ret = %d\n",
  372. ret);
  373. }
  374. ret = snd_pcm_hw_constraint_step(runtime, 0,
  375. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  376. if (ret < 0) {
  377. dev_err(dev, "constraint for buffer bytes step ret = %d\n",
  378. ret);
  379. }
  380. runtime->private_data = prtd;
  381. snd_soc_set_runtime_hwparams(substream, &q6asm_dai_hardware_playback);
  382. runtime->dma_bytes = q6asm_dai_hardware_playback.buffer_bytes_max;
  383. if (pdata->sid < 0)
  384. prtd->phys = substream->dma_buffer.addr;
  385. else
  386. prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
  387. return 0;
  388. }
  389. static int q6asm_dai_close(struct snd_soc_component *component,
  390. struct snd_pcm_substream *substream)
  391. {
  392. struct snd_pcm_runtime *runtime = substream->runtime;
  393. struct snd_soc_pcm_runtime *soc_prtd = asoc_substream_to_rtd(substream);
  394. struct q6asm_dai_rtd *prtd = runtime->private_data;
  395. if (prtd->audio_client) {
  396. if (prtd->state)
  397. q6asm_cmd(prtd->audio_client, prtd->stream_id,
  398. CMD_CLOSE);
  399. q6asm_unmap_memory_regions(substream->stream,
  400. prtd->audio_client);
  401. q6asm_audio_client_free(prtd->audio_client);
  402. prtd->audio_client = NULL;
  403. }
  404. q6routing_stream_close(soc_prtd->dai_link->id,
  405. substream->stream);
  406. kfree(prtd);
  407. return 0;
  408. }
  409. static snd_pcm_uframes_t q6asm_dai_pointer(struct snd_soc_component *component,
  410. struct snd_pcm_substream *substream)
  411. {
  412. struct snd_pcm_runtime *runtime = substream->runtime;
  413. struct q6asm_dai_rtd *prtd = runtime->private_data;
  414. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  415. prtd->pcm_irq_pos = 0;
  416. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  417. }
  418. static int q6asm_dai_hw_params(struct snd_soc_component *component,
  419. struct snd_pcm_substream *substream,
  420. struct snd_pcm_hw_params *params)
  421. {
  422. struct snd_pcm_runtime *runtime = substream->runtime;
  423. struct q6asm_dai_rtd *prtd = runtime->private_data;
  424. prtd->pcm_size = params_buffer_bytes(params);
  425. prtd->periods = params_periods(params);
  426. switch (params_format(params)) {
  427. case SNDRV_PCM_FORMAT_S16_LE:
  428. prtd->bits_per_sample = 16;
  429. break;
  430. case SNDRV_PCM_FORMAT_S24_LE:
  431. prtd->bits_per_sample = 24;
  432. break;
  433. }
  434. return 0;
  435. }
  436. static void compress_event_handler(uint32_t opcode, uint32_t token,
  437. void *payload, void *priv)
  438. {
  439. struct q6asm_dai_rtd *prtd = priv;
  440. struct snd_compr_stream *substream = prtd->cstream;
  441. unsigned long flags;
  442. u32 wflags = 0;
  443. uint64_t avail;
  444. uint32_t bytes_written, bytes_to_write;
  445. bool is_last_buffer = false;
  446. switch (opcode) {
  447. case ASM_CLIENT_EVENT_CMD_RUN_DONE:
  448. spin_lock_irqsave(&prtd->lock, flags);
  449. if (!prtd->bytes_sent) {
  450. q6asm_stream_remove_initial_silence(prtd->audio_client,
  451. prtd->stream_id,
  452. prtd->initial_samples_drop);
  453. q6asm_write_async(prtd->audio_client, prtd->stream_id,
  454. prtd->pcm_count, 0, 0, 0);
  455. prtd->bytes_sent += prtd->pcm_count;
  456. }
  457. spin_unlock_irqrestore(&prtd->lock, flags);
  458. break;
  459. case ASM_CLIENT_EVENT_CMD_EOS_DONE:
  460. spin_lock_irqsave(&prtd->lock, flags);
  461. if (prtd->notify_on_drain) {
  462. if (substream->partial_drain) {
  463. /*
  464. * Close old stream and make it stale, switch
  465. * the active stream now!
  466. */
  467. q6asm_cmd_nowait(prtd->audio_client,
  468. prtd->stream_id,
  469. CMD_CLOSE);
  470. /*
  471. * vaild stream ids start from 1, So we are
  472. * toggling this between 1 and 2.
  473. */
  474. prtd->stream_id = (prtd->stream_id == 1 ? 2 : 1);
  475. }
  476. snd_compr_drain_notify(prtd->cstream);
  477. prtd->notify_on_drain = false;
  478. } else {
  479. prtd->state = Q6ASM_STREAM_STOPPED;
  480. }
  481. spin_unlock_irqrestore(&prtd->lock, flags);
  482. break;
  483. case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
  484. spin_lock_irqsave(&prtd->lock, flags);
  485. bytes_written = token >> ASM_WRITE_TOKEN_LEN_SHIFT;
  486. prtd->copied_total += bytes_written;
  487. snd_compr_fragment_elapsed(substream);
  488. if (prtd->state != Q6ASM_STREAM_RUNNING) {
  489. spin_unlock_irqrestore(&prtd->lock, flags);
  490. break;
  491. }
  492. avail = prtd->bytes_received - prtd->bytes_sent;
  493. if (avail > prtd->pcm_count) {
  494. bytes_to_write = prtd->pcm_count;
  495. } else {
  496. if (substream->partial_drain || prtd->notify_on_drain)
  497. is_last_buffer = true;
  498. bytes_to_write = avail;
  499. }
  500. if (bytes_to_write) {
  501. if (substream->partial_drain && is_last_buffer) {
  502. wflags |= ASM_LAST_BUFFER_FLAG;
  503. q6asm_stream_remove_trailing_silence(prtd->audio_client,
  504. prtd->stream_id,
  505. prtd->trailing_samples_drop);
  506. }
  507. q6asm_write_async(prtd->audio_client, prtd->stream_id,
  508. bytes_to_write, 0, 0, wflags);
  509. prtd->bytes_sent += bytes_to_write;
  510. }
  511. if (prtd->notify_on_drain && is_last_buffer)
  512. q6asm_cmd_nowait(prtd->audio_client,
  513. prtd->stream_id, CMD_EOS);
  514. spin_unlock_irqrestore(&prtd->lock, flags);
  515. break;
  516. default:
  517. break;
  518. }
  519. }
  520. static int q6asm_dai_compr_open(struct snd_soc_component *component,
  521. struct snd_compr_stream *stream)
  522. {
  523. struct snd_soc_pcm_runtime *rtd = stream->private_data;
  524. struct snd_compr_runtime *runtime = stream->runtime;
  525. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  526. struct q6asm_dai_data *pdata;
  527. struct device *dev = component->dev;
  528. struct q6asm_dai_rtd *prtd;
  529. int stream_id, size, ret;
  530. stream_id = cpu_dai->driver->id;
  531. pdata = snd_soc_component_get_drvdata(component);
  532. if (!pdata) {
  533. dev_err(dev, "Drv data not found ..\n");
  534. return -EINVAL;
  535. }
  536. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  537. if (!prtd)
  538. return -ENOMEM;
  539. /* DSP expects stream id from 1 */
  540. prtd->stream_id = 1;
  541. prtd->cstream = stream;
  542. prtd->audio_client = q6asm_audio_client_alloc(dev,
  543. (q6asm_cb)compress_event_handler,
  544. prtd, stream_id, LEGACY_PCM_MODE);
  545. if (IS_ERR(prtd->audio_client)) {
  546. dev_err(dev, "Could not allocate memory\n");
  547. ret = PTR_ERR(prtd->audio_client);
  548. goto free_prtd;
  549. }
  550. size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE *
  551. COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
  552. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size,
  553. &prtd->dma_buffer);
  554. if (ret) {
  555. dev_err(dev, "Cannot allocate buffer(s)\n");
  556. goto free_client;
  557. }
  558. if (pdata->sid < 0)
  559. prtd->phys = prtd->dma_buffer.addr;
  560. else
  561. prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32);
  562. snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer);
  563. spin_lock_init(&prtd->lock);
  564. runtime->private_data = prtd;
  565. return 0;
  566. free_client:
  567. q6asm_audio_client_free(prtd->audio_client);
  568. free_prtd:
  569. kfree(prtd);
  570. return ret;
  571. }
  572. static int q6asm_dai_compr_free(struct snd_soc_component *component,
  573. struct snd_compr_stream *stream)
  574. {
  575. struct snd_compr_runtime *runtime = stream->runtime;
  576. struct q6asm_dai_rtd *prtd = runtime->private_data;
  577. struct snd_soc_pcm_runtime *rtd = stream->private_data;
  578. if (prtd->audio_client) {
  579. if (prtd->state) {
  580. q6asm_cmd(prtd->audio_client, prtd->stream_id,
  581. CMD_CLOSE);
  582. if (prtd->next_track_stream_id) {
  583. q6asm_cmd(prtd->audio_client,
  584. prtd->next_track_stream_id,
  585. CMD_CLOSE);
  586. }
  587. }
  588. snd_dma_free_pages(&prtd->dma_buffer);
  589. q6asm_unmap_memory_regions(stream->direction,
  590. prtd->audio_client);
  591. q6asm_audio_client_free(prtd->audio_client);
  592. prtd->audio_client = NULL;
  593. }
  594. q6routing_stream_close(rtd->dai_link->id, stream->direction);
  595. kfree(prtd);
  596. return 0;
  597. }
  598. static int __q6asm_dai_compr_set_codec_params(struct snd_soc_component *component,
  599. struct snd_compr_stream *stream,
  600. struct snd_codec *codec,
  601. int stream_id)
  602. {
  603. struct snd_compr_runtime *runtime = stream->runtime;
  604. struct q6asm_dai_rtd *prtd = runtime->private_data;
  605. struct q6asm_flac_cfg flac_cfg;
  606. struct q6asm_wma_cfg wma_cfg;
  607. struct q6asm_alac_cfg alac_cfg;
  608. struct q6asm_ape_cfg ape_cfg;
  609. unsigned int wma_v9 = 0;
  610. struct device *dev = component->dev;
  611. int ret;
  612. union snd_codec_options *codec_options;
  613. struct snd_dec_flac *flac;
  614. struct snd_dec_wma *wma;
  615. struct snd_dec_alac *alac;
  616. struct snd_dec_ape *ape;
  617. codec_options = &(prtd->codec.options);
  618. memcpy(&prtd->codec, codec, sizeof(*codec));
  619. switch (codec->id) {
  620. case SND_AUDIOCODEC_FLAC:
  621. memset(&flac_cfg, 0x0, sizeof(struct q6asm_flac_cfg));
  622. flac = &codec_options->flac_d;
  623. flac_cfg.ch_cfg = codec->ch_in;
  624. flac_cfg.sample_rate = codec->sample_rate;
  625. flac_cfg.stream_info_present = 1;
  626. flac_cfg.sample_size = flac->sample_size;
  627. flac_cfg.min_blk_size = flac->min_blk_size;
  628. flac_cfg.max_blk_size = flac->max_blk_size;
  629. flac_cfg.max_frame_size = flac->max_frame_size;
  630. flac_cfg.min_frame_size = flac->min_frame_size;
  631. ret = q6asm_stream_media_format_block_flac(prtd->audio_client,
  632. stream_id,
  633. &flac_cfg);
  634. if (ret < 0) {
  635. dev_err(dev, "FLAC CMD Format block failed:%d\n", ret);
  636. return -EIO;
  637. }
  638. break;
  639. case SND_AUDIOCODEC_WMA:
  640. wma = &codec_options->wma_d;
  641. memset(&wma_cfg, 0x0, sizeof(struct q6asm_wma_cfg));
  642. wma_cfg.sample_rate = codec->sample_rate;
  643. wma_cfg.num_channels = codec->ch_in;
  644. wma_cfg.bytes_per_sec = codec->bit_rate / 8;
  645. wma_cfg.block_align = codec->align;
  646. wma_cfg.bits_per_sample = prtd->bits_per_sample;
  647. wma_cfg.enc_options = wma->encoder_option;
  648. wma_cfg.adv_enc_options = wma->adv_encoder_option;
  649. wma_cfg.adv_enc_options2 = wma->adv_encoder_option2;
  650. if (wma_cfg.num_channels == 1)
  651. wma_cfg.channel_mask = 4; /* Mono Center */
  652. else if (wma_cfg.num_channels == 2)
  653. wma_cfg.channel_mask = 3; /* Stereo FL/FR */
  654. else
  655. return -EINVAL;
  656. /* check the codec profile */
  657. switch (codec->profile) {
  658. case SND_AUDIOPROFILE_WMA9:
  659. wma_cfg.fmtag = 0x161;
  660. wma_v9 = 1;
  661. break;
  662. case SND_AUDIOPROFILE_WMA10:
  663. wma_cfg.fmtag = 0x166;
  664. break;
  665. case SND_AUDIOPROFILE_WMA9_PRO:
  666. wma_cfg.fmtag = 0x162;
  667. break;
  668. case SND_AUDIOPROFILE_WMA9_LOSSLESS:
  669. wma_cfg.fmtag = 0x163;
  670. break;
  671. case SND_AUDIOPROFILE_WMA10_LOSSLESS:
  672. wma_cfg.fmtag = 0x167;
  673. break;
  674. default:
  675. dev_err(dev, "Unknown WMA profile:%x\n",
  676. codec->profile);
  677. return -EIO;
  678. }
  679. if (wma_v9)
  680. ret = q6asm_stream_media_format_block_wma_v9(
  681. prtd->audio_client, stream_id,
  682. &wma_cfg);
  683. else
  684. ret = q6asm_stream_media_format_block_wma_v10(
  685. prtd->audio_client, stream_id,
  686. &wma_cfg);
  687. if (ret < 0) {
  688. dev_err(dev, "WMA9 CMD failed:%d\n", ret);
  689. return -EIO;
  690. }
  691. break;
  692. case SND_AUDIOCODEC_ALAC:
  693. memset(&alac_cfg, 0x0, sizeof(alac_cfg));
  694. alac = &codec_options->alac_d;
  695. alac_cfg.sample_rate = codec->sample_rate;
  696. alac_cfg.avg_bit_rate = codec->bit_rate;
  697. alac_cfg.bit_depth = prtd->bits_per_sample;
  698. alac_cfg.num_channels = codec->ch_in;
  699. alac_cfg.frame_length = alac->frame_length;
  700. alac_cfg.pb = alac->pb;
  701. alac_cfg.mb = alac->mb;
  702. alac_cfg.kb = alac->kb;
  703. alac_cfg.max_run = alac->max_run;
  704. alac_cfg.compatible_version = alac->compatible_version;
  705. alac_cfg.max_frame_bytes = alac->max_frame_bytes;
  706. switch (codec->ch_in) {
  707. case 1:
  708. alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_MONO;
  709. break;
  710. case 2:
  711. alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_STEREO;
  712. break;
  713. }
  714. ret = q6asm_stream_media_format_block_alac(prtd->audio_client,
  715. stream_id,
  716. &alac_cfg);
  717. if (ret < 0) {
  718. dev_err(dev, "ALAC CMD Format block failed:%d\n", ret);
  719. return -EIO;
  720. }
  721. break;
  722. case SND_AUDIOCODEC_APE:
  723. memset(&ape_cfg, 0x0, sizeof(ape_cfg));
  724. ape = &codec_options->ape_d;
  725. ape_cfg.sample_rate = codec->sample_rate;
  726. ape_cfg.num_channels = codec->ch_in;
  727. ape_cfg.bits_per_sample = prtd->bits_per_sample;
  728. ape_cfg.compatible_version = ape->compatible_version;
  729. ape_cfg.compression_level = ape->compression_level;
  730. ape_cfg.format_flags = ape->format_flags;
  731. ape_cfg.blocks_per_frame = ape->blocks_per_frame;
  732. ape_cfg.final_frame_blocks = ape->final_frame_blocks;
  733. ape_cfg.total_frames = ape->total_frames;
  734. ape_cfg.seek_table_present = ape->seek_table_present;
  735. ret = q6asm_stream_media_format_block_ape(prtd->audio_client,
  736. stream_id,
  737. &ape_cfg);
  738. if (ret < 0) {
  739. dev_err(dev, "APE CMD Format block failed:%d\n", ret);
  740. return -EIO;
  741. }
  742. break;
  743. default:
  744. break;
  745. }
  746. return 0;
  747. }
  748. static int q6asm_dai_compr_set_params(struct snd_soc_component *component,
  749. struct snd_compr_stream *stream,
  750. struct snd_compr_params *params)
  751. {
  752. struct snd_compr_runtime *runtime = stream->runtime;
  753. struct q6asm_dai_rtd *prtd = runtime->private_data;
  754. struct snd_soc_pcm_runtime *rtd = stream->private_data;
  755. int dir = stream->direction;
  756. struct q6asm_dai_data *pdata;
  757. struct device *dev = component->dev;
  758. int ret;
  759. pdata = snd_soc_component_get_drvdata(component);
  760. if (!pdata)
  761. return -EINVAL;
  762. if (!prtd || !prtd->audio_client) {
  763. dev_err(dev, "private data null or audio client freed\n");
  764. return -EINVAL;
  765. }
  766. prtd->periods = runtime->fragments;
  767. prtd->pcm_count = runtime->fragment_size;
  768. prtd->pcm_size = runtime->fragments * runtime->fragment_size;
  769. prtd->bits_per_sample = 16;
  770. if (dir == SND_COMPRESS_PLAYBACK) {
  771. ret = q6asm_open_write(prtd->audio_client, prtd->stream_id, params->codec.id,
  772. params->codec.profile, prtd->bits_per_sample,
  773. true);
  774. if (ret < 0) {
  775. dev_err(dev, "q6asm_open_write failed\n");
  776. q6asm_audio_client_free(prtd->audio_client);
  777. prtd->audio_client = NULL;
  778. return ret;
  779. }
  780. }
  781. prtd->session_id = q6asm_get_session_id(prtd->audio_client);
  782. ret = q6routing_stream_open(rtd->dai_link->id, LEGACY_PCM_MODE,
  783. prtd->session_id, dir);
  784. if (ret) {
  785. dev_err(dev, "Stream reg failed ret:%d\n", ret);
  786. return ret;
  787. }
  788. ret = __q6asm_dai_compr_set_codec_params(component, stream,
  789. &params->codec,
  790. prtd->stream_id);
  791. if (ret) {
  792. dev_err(dev, "codec param setup failed ret:%d\n", ret);
  793. return ret;
  794. }
  795. ret = q6asm_map_memory_regions(dir, prtd->audio_client, prtd->phys,
  796. (prtd->pcm_size / prtd->periods),
  797. prtd->periods);
  798. if (ret < 0) {
  799. dev_err(dev, "Buffer Mapping failed ret:%d\n", ret);
  800. return -ENOMEM;
  801. }
  802. prtd->state = Q6ASM_STREAM_RUNNING;
  803. return 0;
  804. }
  805. static int q6asm_dai_compr_set_metadata(struct snd_soc_component *component,
  806. struct snd_compr_stream *stream,
  807. struct snd_compr_metadata *metadata)
  808. {
  809. struct snd_compr_runtime *runtime = stream->runtime;
  810. struct q6asm_dai_rtd *prtd = runtime->private_data;
  811. int ret = 0;
  812. switch (metadata->key) {
  813. case SNDRV_COMPRESS_ENCODER_PADDING:
  814. prtd->trailing_samples_drop = metadata->value[0];
  815. break;
  816. case SNDRV_COMPRESS_ENCODER_DELAY:
  817. prtd->initial_samples_drop = metadata->value[0];
  818. if (prtd->next_track_stream_id) {
  819. ret = q6asm_open_write(prtd->audio_client,
  820. prtd->next_track_stream_id,
  821. prtd->codec.id,
  822. prtd->codec.profile,
  823. prtd->bits_per_sample,
  824. true);
  825. if (ret < 0) {
  826. dev_err(component->dev, "q6asm_open_write failed\n");
  827. return ret;
  828. }
  829. ret = __q6asm_dai_compr_set_codec_params(component, stream,
  830. &prtd->codec,
  831. prtd->next_track_stream_id);
  832. if (ret < 0) {
  833. dev_err(component->dev, "q6asm_open_write failed\n");
  834. return ret;
  835. }
  836. ret = q6asm_stream_remove_initial_silence(prtd->audio_client,
  837. prtd->next_track_stream_id,
  838. prtd->initial_samples_drop);
  839. prtd->next_track_stream_id = 0;
  840. }
  841. break;
  842. default:
  843. ret = -EINVAL;
  844. break;
  845. }
  846. return ret;
  847. }
  848. static int q6asm_dai_compr_trigger(struct snd_soc_component *component,
  849. struct snd_compr_stream *stream, int cmd)
  850. {
  851. struct snd_compr_runtime *runtime = stream->runtime;
  852. struct q6asm_dai_rtd *prtd = runtime->private_data;
  853. int ret = 0;
  854. switch (cmd) {
  855. case SNDRV_PCM_TRIGGER_START:
  856. case SNDRV_PCM_TRIGGER_RESUME:
  857. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  858. ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id,
  859. 0, 0, 0);
  860. break;
  861. case SNDRV_PCM_TRIGGER_STOP:
  862. prtd->state = Q6ASM_STREAM_STOPPED;
  863. ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
  864. CMD_EOS);
  865. break;
  866. case SNDRV_PCM_TRIGGER_SUSPEND:
  867. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  868. ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
  869. CMD_PAUSE);
  870. break;
  871. case SND_COMPR_TRIGGER_NEXT_TRACK:
  872. prtd->next_track = true;
  873. prtd->next_track_stream_id = (prtd->stream_id == 1 ? 2 : 1);
  874. break;
  875. case SND_COMPR_TRIGGER_DRAIN:
  876. case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
  877. prtd->notify_on_drain = true;
  878. break;
  879. default:
  880. ret = -EINVAL;
  881. break;
  882. }
  883. return ret;
  884. }
  885. static int q6asm_dai_compr_pointer(struct snd_soc_component *component,
  886. struct snd_compr_stream *stream,
  887. struct snd_compr_tstamp *tstamp)
  888. {
  889. struct snd_compr_runtime *runtime = stream->runtime;
  890. struct q6asm_dai_rtd *prtd = runtime->private_data;
  891. unsigned long flags;
  892. spin_lock_irqsave(&prtd->lock, flags);
  893. tstamp->copied_total = prtd->copied_total;
  894. tstamp->byte_offset = prtd->copied_total % prtd->pcm_size;
  895. spin_unlock_irqrestore(&prtd->lock, flags);
  896. return 0;
  897. }
  898. static int q6asm_compr_copy(struct snd_soc_component *component,
  899. struct snd_compr_stream *stream, char __user *buf,
  900. size_t count)
  901. {
  902. struct snd_compr_runtime *runtime = stream->runtime;
  903. struct q6asm_dai_rtd *prtd = runtime->private_data;
  904. unsigned long flags;
  905. u32 wflags = 0;
  906. int avail, bytes_in_flight = 0;
  907. void *dstn;
  908. size_t copy;
  909. u32 app_pointer;
  910. u32 bytes_received;
  911. bytes_received = prtd->bytes_received;
  912. /**
  913. * Make sure that next track data pointer is aligned at 32 bit boundary
  914. * This is a Mandatory requirement from DSP data buffers alignment
  915. */
  916. if (prtd->next_track)
  917. bytes_received = ALIGN(prtd->bytes_received, prtd->pcm_count);
  918. app_pointer = bytes_received/prtd->pcm_size;
  919. app_pointer = bytes_received - (app_pointer * prtd->pcm_size);
  920. dstn = prtd->dma_buffer.area + app_pointer;
  921. if (count < prtd->pcm_size - app_pointer) {
  922. if (copy_from_user(dstn, buf, count))
  923. return -EFAULT;
  924. } else {
  925. copy = prtd->pcm_size - app_pointer;
  926. if (copy_from_user(dstn, buf, copy))
  927. return -EFAULT;
  928. if (copy_from_user(prtd->dma_buffer.area, buf + copy,
  929. count - copy))
  930. return -EFAULT;
  931. }
  932. spin_lock_irqsave(&prtd->lock, flags);
  933. bytes_in_flight = prtd->bytes_received - prtd->copied_total;
  934. if (prtd->next_track) {
  935. prtd->next_track = false;
  936. prtd->copied_total = ALIGN(prtd->copied_total, prtd->pcm_count);
  937. prtd->bytes_sent = ALIGN(prtd->bytes_sent, prtd->pcm_count);
  938. }
  939. prtd->bytes_received = bytes_received + count;
  940. /* Kick off the data to dsp if its starving!! */
  941. if (prtd->state == Q6ASM_STREAM_RUNNING && (bytes_in_flight == 0)) {
  942. uint32_t bytes_to_write = prtd->pcm_count;
  943. avail = prtd->bytes_received - prtd->bytes_sent;
  944. if (avail < prtd->pcm_count)
  945. bytes_to_write = avail;
  946. q6asm_write_async(prtd->audio_client, prtd->stream_id,
  947. bytes_to_write, 0, 0, wflags);
  948. prtd->bytes_sent += bytes_to_write;
  949. }
  950. spin_unlock_irqrestore(&prtd->lock, flags);
  951. return count;
  952. }
  953. static int q6asm_dai_compr_mmap(struct snd_soc_component *component,
  954. struct snd_compr_stream *stream,
  955. struct vm_area_struct *vma)
  956. {
  957. struct snd_compr_runtime *runtime = stream->runtime;
  958. struct q6asm_dai_rtd *prtd = runtime->private_data;
  959. struct device *dev = component->dev;
  960. return dma_mmap_coherent(dev, vma,
  961. prtd->dma_buffer.area, prtd->dma_buffer.addr,
  962. prtd->dma_buffer.bytes);
  963. }
  964. static int q6asm_dai_compr_get_caps(struct snd_soc_component *component,
  965. struct snd_compr_stream *stream,
  966. struct snd_compr_caps *caps)
  967. {
  968. caps->direction = SND_COMPRESS_PLAYBACK;
  969. caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE;
  970. caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE;
  971. caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS;
  972. caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
  973. caps->num_codecs = 5;
  974. caps->codecs[0] = SND_AUDIOCODEC_MP3;
  975. caps->codecs[1] = SND_AUDIOCODEC_FLAC;
  976. caps->codecs[2] = SND_AUDIOCODEC_WMA;
  977. caps->codecs[3] = SND_AUDIOCODEC_ALAC;
  978. caps->codecs[4] = SND_AUDIOCODEC_APE;
  979. return 0;
  980. }
  981. static int q6asm_dai_compr_get_codec_caps(struct snd_soc_component *component,
  982. struct snd_compr_stream *stream,
  983. struct snd_compr_codec_caps *codec)
  984. {
  985. switch (codec->codec) {
  986. case SND_AUDIOCODEC_MP3:
  987. *codec = q6asm_compr_caps;
  988. break;
  989. default:
  990. break;
  991. }
  992. return 0;
  993. }
  994. static const struct snd_compress_ops q6asm_dai_compress_ops = {
  995. .open = q6asm_dai_compr_open,
  996. .free = q6asm_dai_compr_free,
  997. .set_params = q6asm_dai_compr_set_params,
  998. .set_metadata = q6asm_dai_compr_set_metadata,
  999. .pointer = q6asm_dai_compr_pointer,
  1000. .trigger = q6asm_dai_compr_trigger,
  1001. .get_caps = q6asm_dai_compr_get_caps,
  1002. .get_codec_caps = q6asm_dai_compr_get_codec_caps,
  1003. .mmap = q6asm_dai_compr_mmap,
  1004. .copy = q6asm_compr_copy,
  1005. };
  1006. static int q6asm_dai_pcm_new(struct snd_soc_component *component,
  1007. struct snd_soc_pcm_runtime *rtd)
  1008. {
  1009. struct snd_pcm *pcm = rtd->pcm;
  1010. size_t size = q6asm_dai_hardware_playback.buffer_bytes_max;
  1011. return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  1012. component->dev, size);
  1013. }
  1014. static const struct snd_soc_dapm_widget q6asm_dapm_widgets[] = {
  1015. SND_SOC_DAPM_AIF_IN("MM_DL1", "MultiMedia1 Playback", 0, SND_SOC_NOPM, 0, 0),
  1016. SND_SOC_DAPM_AIF_IN("MM_DL2", "MultiMedia2 Playback", 0, SND_SOC_NOPM, 0, 0),
  1017. SND_SOC_DAPM_AIF_IN("MM_DL3", "MultiMedia3 Playback", 0, SND_SOC_NOPM, 0, 0),
  1018. SND_SOC_DAPM_AIF_IN("MM_DL4", "MultiMedia4 Playback", 0, SND_SOC_NOPM, 0, 0),
  1019. SND_SOC_DAPM_AIF_IN("MM_DL5", "MultiMedia5 Playback", 0, SND_SOC_NOPM, 0, 0),
  1020. SND_SOC_DAPM_AIF_IN("MM_DL6", "MultiMedia6 Playback", 0, SND_SOC_NOPM, 0, 0),
  1021. SND_SOC_DAPM_AIF_IN("MM_DL7", "MultiMedia7 Playback", 0, SND_SOC_NOPM, 0, 0),
  1022. SND_SOC_DAPM_AIF_IN("MM_DL8", "MultiMedia8 Playback", 0, SND_SOC_NOPM, 0, 0),
  1023. SND_SOC_DAPM_AIF_OUT("MM_UL1", "MultiMedia1 Capture", 0, SND_SOC_NOPM, 0, 0),
  1024. SND_SOC_DAPM_AIF_OUT("MM_UL2", "MultiMedia2 Capture", 0, SND_SOC_NOPM, 0, 0),
  1025. SND_SOC_DAPM_AIF_OUT("MM_UL3", "MultiMedia3 Capture", 0, SND_SOC_NOPM, 0, 0),
  1026. SND_SOC_DAPM_AIF_OUT("MM_UL4", "MultiMedia4 Capture", 0, SND_SOC_NOPM, 0, 0),
  1027. SND_SOC_DAPM_AIF_OUT("MM_UL5", "MultiMedia5 Capture", 0, SND_SOC_NOPM, 0, 0),
  1028. SND_SOC_DAPM_AIF_OUT("MM_UL6", "MultiMedia6 Capture", 0, SND_SOC_NOPM, 0, 0),
  1029. SND_SOC_DAPM_AIF_OUT("MM_UL7", "MultiMedia7 Capture", 0, SND_SOC_NOPM, 0, 0),
  1030. SND_SOC_DAPM_AIF_OUT("MM_UL8", "MultiMedia8 Capture", 0, SND_SOC_NOPM, 0, 0),
  1031. };
  1032. static const struct snd_soc_component_driver q6asm_fe_dai_component = {
  1033. .name = DRV_NAME,
  1034. .open = q6asm_dai_open,
  1035. .hw_params = q6asm_dai_hw_params,
  1036. .close = q6asm_dai_close,
  1037. .prepare = q6asm_dai_prepare,
  1038. .trigger = q6asm_dai_trigger,
  1039. .pointer = q6asm_dai_pointer,
  1040. .pcm_construct = q6asm_dai_pcm_new,
  1041. .compress_ops = &q6asm_dai_compress_ops,
  1042. .dapm_widgets = q6asm_dapm_widgets,
  1043. .num_dapm_widgets = ARRAY_SIZE(q6asm_dapm_widgets),
  1044. .legacy_dai_naming = 1,
  1045. };
  1046. static struct snd_soc_dai_driver q6asm_fe_dais_template[] = {
  1047. Q6ASM_FEDAI_DRIVER(1),
  1048. Q6ASM_FEDAI_DRIVER(2),
  1049. Q6ASM_FEDAI_DRIVER(3),
  1050. Q6ASM_FEDAI_DRIVER(4),
  1051. Q6ASM_FEDAI_DRIVER(5),
  1052. Q6ASM_FEDAI_DRIVER(6),
  1053. Q6ASM_FEDAI_DRIVER(7),
  1054. Q6ASM_FEDAI_DRIVER(8),
  1055. };
  1056. static int of_q6asm_parse_dai_data(struct device *dev,
  1057. struct q6asm_dai_data *pdata)
  1058. {
  1059. struct snd_soc_dai_driver *dai_drv;
  1060. struct snd_soc_pcm_stream empty_stream;
  1061. struct device_node *node;
  1062. int ret, id, dir, idx = 0;
  1063. pdata->num_dais = of_get_child_count(dev->of_node);
  1064. if (!pdata->num_dais) {
  1065. dev_err(dev, "No dais found in DT\n");
  1066. return -EINVAL;
  1067. }
  1068. pdata->dais = devm_kcalloc(dev, pdata->num_dais, sizeof(*dai_drv),
  1069. GFP_KERNEL);
  1070. if (!pdata->dais)
  1071. return -ENOMEM;
  1072. memset(&empty_stream, 0, sizeof(empty_stream));
  1073. for_each_child_of_node(dev->of_node, node) {
  1074. ret = of_property_read_u32(node, "reg", &id);
  1075. if (ret || id >= MAX_SESSIONS || id < 0) {
  1076. dev_err(dev, "valid dai id not found:%d\n", ret);
  1077. continue;
  1078. }
  1079. dai_drv = &pdata->dais[idx++];
  1080. *dai_drv = q6asm_fe_dais_template[id];
  1081. ret = of_property_read_u32(node, "direction", &dir);
  1082. if (ret)
  1083. continue;
  1084. if (dir == Q6ASM_DAI_RX)
  1085. dai_drv->capture = empty_stream;
  1086. else if (dir == Q6ASM_DAI_TX)
  1087. dai_drv->playback = empty_stream;
  1088. if (of_property_read_bool(node, "is-compress-dai"))
  1089. dai_drv->compress_new = snd_soc_new_compress;
  1090. }
  1091. return 0;
  1092. }
  1093. static int q6asm_dai_probe(struct platform_device *pdev)
  1094. {
  1095. struct device *dev = &pdev->dev;
  1096. struct device_node *node = dev->of_node;
  1097. struct of_phandle_args args;
  1098. struct q6asm_dai_data *pdata;
  1099. int rc;
  1100. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1101. if (!pdata)
  1102. return -ENOMEM;
  1103. rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
  1104. if (rc < 0)
  1105. pdata->sid = -1;
  1106. else
  1107. pdata->sid = args.args[0] & SID_MASK_DEFAULT;
  1108. dev_set_drvdata(dev, pdata);
  1109. rc = of_q6asm_parse_dai_data(dev, pdata);
  1110. if (rc)
  1111. return rc;
  1112. return devm_snd_soc_register_component(dev, &q6asm_fe_dai_component,
  1113. pdata->dais, pdata->num_dais);
  1114. }
  1115. #ifdef CONFIG_OF
  1116. static const struct of_device_id q6asm_dai_device_id[] = {
  1117. { .compatible = "qcom,q6asm-dais" },
  1118. {},
  1119. };
  1120. MODULE_DEVICE_TABLE(of, q6asm_dai_device_id);
  1121. #endif
  1122. static struct platform_driver q6asm_dai_platform_driver = {
  1123. .driver = {
  1124. .name = "q6asm-dai",
  1125. .of_match_table = of_match_ptr(q6asm_dai_device_id),
  1126. },
  1127. .probe = q6asm_dai_probe,
  1128. };
  1129. module_platform_driver(q6asm_dai_platform_driver);
  1130. MODULE_DESCRIPTION("Q6ASM dai driver");
  1131. MODULE_LICENSE("GPL v2");