q6apm-dai.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2021, Linaro Limited
  3. #include <linux/init.h>
  4. #include <linux/err.h>
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/slab.h>
  8. #include <sound/soc.h>
  9. #include <sound/soc-dapm.h>
  10. #include <linux/spinlock.h>
  11. #include <sound/pcm.h>
  12. #include <asm/dma.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/of_device.h>
  15. #include <sound/pcm_params.h>
  16. #include "q6apm.h"
  17. #define DRV_NAME "q6apm-dai"
  18. #define PLAYBACK_MIN_NUM_PERIODS 2
  19. #define PLAYBACK_MAX_NUM_PERIODS 8
  20. #define PLAYBACK_MAX_PERIOD_SIZE 65536
  21. #define PLAYBACK_MIN_PERIOD_SIZE 128
  22. #define CAPTURE_MIN_NUM_PERIODS 2
  23. #define CAPTURE_MAX_NUM_PERIODS 8
  24. #define CAPTURE_MAX_PERIOD_SIZE 4096
  25. #define CAPTURE_MIN_PERIOD_SIZE 320
  26. #define BUFFER_BYTES_MAX (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE)
  27. #define BUFFER_BYTES_MIN (PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE)
  28. #define SID_MASK_DEFAULT 0xF
  29. enum stream_state {
  30. Q6APM_STREAM_IDLE = 0,
  31. Q6APM_STREAM_STOPPED,
  32. Q6APM_STREAM_RUNNING,
  33. };
  34. struct q6apm_dai_rtd {
  35. struct snd_pcm_substream *substream;
  36. struct snd_compr_stream *cstream;
  37. struct snd_compr_params codec_param;
  38. struct snd_dma_buffer dma_buffer;
  39. phys_addr_t phys;
  40. unsigned int pcm_size;
  41. unsigned int pcm_count;
  42. unsigned int pos; /* Buffer position */
  43. unsigned int periods;
  44. unsigned int bytes_sent;
  45. unsigned int bytes_received;
  46. unsigned int copied_total;
  47. uint16_t bits_per_sample;
  48. uint16_t source; /* Encoding source bit mask */
  49. uint16_t session_id;
  50. enum stream_state state;
  51. struct q6apm_graph *graph;
  52. spinlock_t lock;
  53. };
  54. struct q6apm_dai_data {
  55. long long sid;
  56. };
  57. static struct snd_pcm_hardware q6apm_dai_hardware_capture = {
  58. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  59. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
  60. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
  61. SNDRV_PCM_INFO_BATCH),
  62. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
  63. .rates = SNDRV_PCM_RATE_8000_48000,
  64. .rate_min = 8000,
  65. .rate_max = 48000,
  66. .channels_min = 2,
  67. .channels_max = 4,
  68. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
  69. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  70. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  71. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  72. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  73. .fifo_size = 0,
  74. };
  75. static struct snd_pcm_hardware q6apm_dai_hardware_playback = {
  76. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  77. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
  78. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
  79. SNDRV_PCM_INFO_BATCH),
  80. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
  81. .rates = SNDRV_PCM_RATE_8000_192000,
  82. .rate_min = 8000,
  83. .rate_max = 192000,
  84. .channels_min = 2,
  85. .channels_max = 8,
  86. .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE),
  87. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  88. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  89. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  90. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  91. .fifo_size = 0,
  92. };
  93. static void event_handler(uint32_t opcode, uint32_t token, uint32_t *payload, void *priv)
  94. {
  95. struct q6apm_dai_rtd *prtd = priv;
  96. struct snd_pcm_substream *substream = prtd->substream;
  97. unsigned long flags;
  98. switch (opcode) {
  99. case APM_CLIENT_EVENT_CMD_EOS_DONE:
  100. prtd->state = Q6APM_STREAM_STOPPED;
  101. break;
  102. case APM_CLIENT_EVENT_DATA_WRITE_DONE:
  103. spin_lock_irqsave(&prtd->lock, flags);
  104. prtd->pos += prtd->pcm_count;
  105. spin_unlock_irqrestore(&prtd->lock, flags);
  106. snd_pcm_period_elapsed(substream);
  107. if (prtd->state == Q6APM_STREAM_RUNNING)
  108. q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
  109. break;
  110. case APM_CLIENT_EVENT_DATA_READ_DONE:
  111. spin_lock_irqsave(&prtd->lock, flags);
  112. prtd->pos += prtd->pcm_count;
  113. spin_unlock_irqrestore(&prtd->lock, flags);
  114. snd_pcm_period_elapsed(substream);
  115. if (prtd->state == Q6APM_STREAM_RUNNING)
  116. q6apm_read(prtd->graph);
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. static int q6apm_dai_prepare(struct snd_soc_component *component,
  123. struct snd_pcm_substream *substream)
  124. {
  125. struct snd_pcm_runtime *runtime = substream->runtime;
  126. struct q6apm_dai_rtd *prtd = runtime->private_data;
  127. struct audioreach_module_config cfg;
  128. struct device *dev = component->dev;
  129. struct q6apm_dai_data *pdata;
  130. int ret;
  131. pdata = snd_soc_component_get_drvdata(component);
  132. if (!pdata)
  133. return -EINVAL;
  134. if (!prtd || !prtd->graph) {
  135. dev_err(dev, "%s: private data null or audio client freed\n", __func__);
  136. return -EINVAL;
  137. }
  138. cfg.direction = substream->stream;
  139. cfg.sample_rate = runtime->rate;
  140. cfg.num_channels = runtime->channels;
  141. cfg.bit_width = prtd->bits_per_sample;
  142. if (prtd->state) {
  143. /* clear the previous setup if any */
  144. q6apm_graph_stop(prtd->graph);
  145. q6apm_unmap_memory_regions(prtd->graph, substream->stream);
  146. }
  147. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  148. prtd->pos = 0;
  149. /* rate and channels are sent to audio driver */
  150. ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
  151. if (ret < 0) {
  152. dev_err(dev, "%s: q6apm_open_write failed\n", __func__);
  153. return ret;
  154. }
  155. ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
  156. if (ret < 0)
  157. dev_err(dev, "%s: CMD Format block failed\n", __func__);
  158. ret = q6apm_map_memory_regions(prtd->graph, substream->stream, prtd->phys,
  159. (prtd->pcm_size / prtd->periods), prtd->periods);
  160. if (ret < 0) {
  161. dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret);
  162. return -ENOMEM;
  163. }
  164. ret = q6apm_graph_prepare(prtd->graph);
  165. if (ret) {
  166. dev_err(dev, "Failed to prepare Graph %d\n", ret);
  167. return ret;
  168. }
  169. ret = q6apm_graph_start(prtd->graph);
  170. if (ret) {
  171. dev_err(dev, "Failed to Start Graph %d\n", ret);
  172. return ret;
  173. }
  174. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  175. int i;
  176. /* Queue the buffers for Capture ONLY after graph is started */
  177. for (i = 0; i < runtime->periods; i++)
  178. q6apm_read(prtd->graph);
  179. }
  180. /* Now that graph as been prepared and started update the internal state accordingly */
  181. prtd->state = Q6APM_STREAM_RUNNING;
  182. return 0;
  183. }
  184. static int q6apm_dai_trigger(struct snd_soc_component *component,
  185. struct snd_pcm_substream *substream, int cmd)
  186. {
  187. struct snd_pcm_runtime *runtime = substream->runtime;
  188. struct q6apm_dai_rtd *prtd = runtime->private_data;
  189. int ret = 0;
  190. switch (cmd) {
  191. case SNDRV_PCM_TRIGGER_START:
  192. case SNDRV_PCM_TRIGGER_RESUME:
  193. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  194. /* start writing buffers for playback only as we already queued capture buffers */
  195. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  196. ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
  197. break;
  198. case SNDRV_PCM_TRIGGER_STOP:
  199. /* TODO support be handled via SoftPause Module */
  200. prtd->state = Q6APM_STREAM_STOPPED;
  201. break;
  202. case SNDRV_PCM_TRIGGER_SUSPEND:
  203. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  204. break;
  205. default:
  206. ret = -EINVAL;
  207. break;
  208. }
  209. return ret;
  210. }
  211. static int q6apm_dai_open(struct snd_soc_component *component,
  212. struct snd_pcm_substream *substream)
  213. {
  214. struct snd_pcm_runtime *runtime = substream->runtime;
  215. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  216. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_prtd, 0);
  217. struct device *dev = component->dev;
  218. struct q6apm_dai_data *pdata;
  219. struct q6apm_dai_rtd *prtd;
  220. int graph_id, ret;
  221. graph_id = cpu_dai->driver->id;
  222. pdata = snd_soc_component_get_drvdata(component);
  223. if (!pdata) {
  224. dev_err(dev, "Drv data not found ..\n");
  225. return -EINVAL;
  226. }
  227. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  228. if (prtd == NULL)
  229. return -ENOMEM;
  230. spin_lock_init(&prtd->lock);
  231. prtd->substream = substream;
  232. prtd->graph = q6apm_graph_open(dev, (q6apm_cb)event_handler, prtd, graph_id);
  233. if (IS_ERR(prtd->graph)) {
  234. dev_err(dev, "%s: Could not allocate memory\n", __func__);
  235. ret = PTR_ERR(prtd->graph);
  236. goto err;
  237. }
  238. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  239. runtime->hw = q6apm_dai_hardware_playback;
  240. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  241. runtime->hw = q6apm_dai_hardware_capture;
  242. /* Ensure that buffer size is a multiple of period size */
  243. ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  244. if (ret < 0) {
  245. dev_err(dev, "snd_pcm_hw_constraint_integer failed\n");
  246. goto err;
  247. }
  248. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  249. ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  250. BUFFER_BYTES_MIN, BUFFER_BYTES_MAX);
  251. if (ret < 0) {
  252. dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret);
  253. goto err;
  254. }
  255. }
  256. ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  257. if (ret < 0) {
  258. dev_err(dev, "constraint for period bytes step ret = %d\n", ret);
  259. goto err;
  260. }
  261. ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  262. if (ret < 0) {
  263. dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret);
  264. goto err;
  265. }
  266. runtime->private_data = prtd;
  267. runtime->dma_bytes = BUFFER_BYTES_MAX;
  268. if (pdata->sid < 0)
  269. prtd->phys = substream->dma_buffer.addr;
  270. else
  271. prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
  272. return 0;
  273. err:
  274. kfree(prtd);
  275. return ret;
  276. }
  277. static int q6apm_dai_close(struct snd_soc_component *component,
  278. struct snd_pcm_substream *substream)
  279. {
  280. struct snd_pcm_runtime *runtime = substream->runtime;
  281. struct q6apm_dai_rtd *prtd = runtime->private_data;
  282. if (prtd->state) { /* only stop graph that is started */
  283. q6apm_graph_stop(prtd->graph);
  284. q6apm_unmap_memory_regions(prtd->graph, substream->stream);
  285. }
  286. q6apm_graph_close(prtd->graph);
  287. prtd->graph = NULL;
  288. kfree(prtd);
  289. runtime->private_data = NULL;
  290. return 0;
  291. }
  292. static snd_pcm_uframes_t q6apm_dai_pointer(struct snd_soc_component *component,
  293. struct snd_pcm_substream *substream)
  294. {
  295. struct snd_pcm_runtime *runtime = substream->runtime;
  296. struct q6apm_dai_rtd *prtd = runtime->private_data;
  297. snd_pcm_uframes_t ptr;
  298. unsigned long flags;
  299. spin_lock_irqsave(&prtd->lock, flags);
  300. if (prtd->pos == prtd->pcm_size)
  301. prtd->pos = 0;
  302. ptr = bytes_to_frames(runtime, prtd->pos);
  303. spin_unlock_irqrestore(&prtd->lock, flags);
  304. return ptr;
  305. }
  306. static int q6apm_dai_hw_params(struct snd_soc_component *component,
  307. struct snd_pcm_substream *substream,
  308. struct snd_pcm_hw_params *params)
  309. {
  310. struct snd_pcm_runtime *runtime = substream->runtime;
  311. struct q6apm_dai_rtd *prtd = runtime->private_data;
  312. prtd->pcm_size = params_buffer_bytes(params);
  313. prtd->periods = params_periods(params);
  314. switch (params_format(params)) {
  315. case SNDRV_PCM_FORMAT_S16_LE:
  316. prtd->bits_per_sample = 16;
  317. break;
  318. case SNDRV_PCM_FORMAT_S24_LE:
  319. prtd->bits_per_sample = 24;
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. return 0;
  325. }
  326. static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
  327. {
  328. int size = BUFFER_BYTES_MAX;
  329. return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, component->dev, size);
  330. }
  331. static const struct snd_soc_component_driver q6apm_fe_dai_component = {
  332. .name = DRV_NAME,
  333. .open = q6apm_dai_open,
  334. .close = q6apm_dai_close,
  335. .prepare = q6apm_dai_prepare,
  336. .pcm_construct = q6apm_dai_pcm_new,
  337. .hw_params = q6apm_dai_hw_params,
  338. .pointer = q6apm_dai_pointer,
  339. .trigger = q6apm_dai_trigger,
  340. };
  341. static int q6apm_dai_probe(struct platform_device *pdev)
  342. {
  343. struct device *dev = &pdev->dev;
  344. struct device_node *node = dev->of_node;
  345. struct q6apm_dai_data *pdata;
  346. struct of_phandle_args args;
  347. int rc;
  348. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  349. if (!pdata)
  350. return -ENOMEM;
  351. rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
  352. if (rc < 0)
  353. pdata->sid = -1;
  354. else
  355. pdata->sid = args.args[0] & SID_MASK_DEFAULT;
  356. dev_set_drvdata(dev, pdata);
  357. return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0);
  358. }
  359. #ifdef CONFIG_OF
  360. static const struct of_device_id q6apm_dai_device_id[] = {
  361. { .compatible = "qcom,q6apm-dais" },
  362. {},
  363. };
  364. MODULE_DEVICE_TABLE(of, q6apm_dai_device_id);
  365. #endif
  366. static struct platform_driver q6apm_dai_platform_driver = {
  367. .driver = {
  368. .name = "q6apm-dai",
  369. .of_match_table = of_match_ptr(q6apm_dai_device_id),
  370. },
  371. .probe = q6apm_dai_probe,
  372. };
  373. module_platform_driver(q6apm_dai_platform_driver);
  374. MODULE_DESCRIPTION("Q6APM dai driver");
  375. MODULE_LICENSE("GPL");