soc-generic-dmaengine-pcm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (C) 2013, Analog Devices Inc.
  4. // Author: Lars-Peter Clausen <lars@metafoo.de>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/dmaengine.h>
  8. #include <linux/slab.h>
  9. #include <sound/pcm.h>
  10. #include <sound/pcm_params.h>
  11. #include <sound/soc.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/of.h>
  14. #include <sound/dmaengine_pcm.h>
  15. /*
  16. * The platforms dmaengine driver does not support reporting the amount of
  17. * bytes that are still left to transfer.
  18. */
  19. #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
  20. struct dmaengine_pcm {
  21. struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
  22. const struct snd_dmaengine_pcm_config *config;
  23. struct snd_soc_component component;
  24. unsigned int flags;
  25. };
  26. static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
  27. {
  28. return container_of(p, struct dmaengine_pcm, component);
  29. }
  30. static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
  31. struct snd_pcm_substream *substream)
  32. {
  33. if (!pcm->chan[substream->stream])
  34. return NULL;
  35. return pcm->chan[substream->stream]->device->dev;
  36. }
  37. /**
  38. * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
  39. * @substream: PCM substream
  40. * @params: hw_params
  41. * @slave_config: DMA slave config to prepare
  42. *
  43. * This function can be used as a generic prepare_slave_config callback for
  44. * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
  45. * DAI DMA data. Internally the function will first call
  46. * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
  47. * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
  48. * remaining fields based on the DAI DMA data.
  49. */
  50. int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
  51. struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
  52. {
  53. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  54. struct snd_dmaengine_dai_dma_data *dma_data;
  55. int ret;
  56. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  57. ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
  58. if (ret)
  59. return ret;
  60. snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
  61. slave_config);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
  65. static int dmaengine_pcm_hw_params(struct snd_pcm_substream *substream,
  66. struct snd_pcm_hw_params *params)
  67. {
  68. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  69. struct snd_soc_component *component =
  70. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  71. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  72. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  73. int (*prepare_slave_config)(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params,
  75. struct dma_slave_config *slave_config);
  76. struct dma_slave_config slave_config;
  77. int ret;
  78. memset(&slave_config, 0, sizeof(slave_config));
  79. if (!pcm->config)
  80. prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config;
  81. else
  82. prepare_slave_config = pcm->config->prepare_slave_config;
  83. if (prepare_slave_config) {
  84. ret = prepare_slave_config(substream, params, &slave_config);
  85. if (ret)
  86. return ret;
  87. ret = dmaengine_slave_config(chan, &slave_config);
  88. if (ret)
  89. return ret;
  90. }
  91. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  92. }
  93. static int dmaengine_pcm_set_runtime_hwparams(struct snd_pcm_substream *substream)
  94. {
  95. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  96. struct snd_soc_component *component =
  97. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  98. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  99. struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
  100. struct dma_chan *chan = pcm->chan[substream->stream];
  101. struct snd_dmaengine_dai_dma_data *dma_data;
  102. struct dma_slave_caps dma_caps;
  103. struct snd_pcm_hardware hw;
  104. u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  105. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  106. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  107. snd_pcm_format_t i;
  108. int ret;
  109. if (pcm->config && pcm->config->pcm_hardware)
  110. return snd_soc_set_runtime_hwparams(substream,
  111. pcm->config->pcm_hardware);
  112. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  113. memset(&hw, 0, sizeof(hw));
  114. hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  115. SNDRV_PCM_INFO_INTERLEAVED;
  116. hw.periods_min = 2;
  117. hw.periods_max = UINT_MAX;
  118. hw.period_bytes_min = 256;
  119. hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
  120. hw.buffer_bytes_max = SIZE_MAX;
  121. hw.fifo_size = dma_data->fifo_size;
  122. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  123. hw.info |= SNDRV_PCM_INFO_BATCH;
  124. ret = dma_get_slave_caps(chan, &dma_caps);
  125. if (ret == 0) {
  126. if (dma_caps.cmd_pause && dma_caps.cmd_resume)
  127. hw.info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
  128. if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
  129. hw.info |= SNDRV_PCM_INFO_BATCH;
  130. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  131. addr_widths = dma_caps.dst_addr_widths;
  132. else
  133. addr_widths = dma_caps.src_addr_widths;
  134. }
  135. /*
  136. * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
  137. * hw.formats set to 0, meaning no restrictions are in place.
  138. * In this case it's the responsibility of the DAI driver to
  139. * provide the supported format information.
  140. */
  141. if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
  142. /*
  143. * Prepare formats mask for valid/allowed sample types. If the
  144. * dma does not have support for the given physical word size,
  145. * it needs to be masked out so user space can not use the
  146. * format which produces corrupted audio.
  147. * In case the dma driver does not implement the slave_caps the
  148. * default assumption is that it supports 1, 2 and 4 bytes
  149. * widths.
  150. */
  151. for (i = SNDRV_PCM_FORMAT_FIRST; i <= SNDRV_PCM_FORMAT_LAST; i++) {
  152. int bits = snd_pcm_format_physical_width(i);
  153. /*
  154. * Enable only samples with DMA supported physical
  155. * widths
  156. */
  157. switch (bits) {
  158. case 8:
  159. case 16:
  160. case 24:
  161. case 32:
  162. case 64:
  163. if (addr_widths & (1 << (bits / 8)))
  164. hw.formats |= pcm_format_to_bits(i);
  165. break;
  166. default:
  167. /* Unsupported types */
  168. break;
  169. }
  170. }
  171. return snd_soc_set_runtime_hwparams(substream, &hw);
  172. }
  173. static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
  174. {
  175. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  176. struct snd_soc_component *component =
  177. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  178. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  179. struct dma_chan *chan = pcm->chan[substream->stream];
  180. int ret;
  181. ret = dmaengine_pcm_set_runtime_hwparams(substream);
  182. if (ret)
  183. return ret;
  184. return snd_dmaengine_pcm_open(substream, chan);
  185. }
  186. static struct dma_chan *dmaengine_pcm_compat_request_channel(
  187. struct snd_soc_pcm_runtime *rtd,
  188. struct snd_pcm_substream *substream)
  189. {
  190. struct snd_soc_component *component =
  191. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  192. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  193. struct snd_dmaengine_dai_dma_data *dma_data;
  194. dma_filter_fn fn = NULL;
  195. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  196. if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
  197. return pcm->chan[0];
  198. if (pcm->config && pcm->config->compat_request_channel)
  199. return pcm->config->compat_request_channel(rtd, substream);
  200. if (pcm->config)
  201. fn = pcm->config->compat_filter_fn;
  202. return snd_dmaengine_pcm_request_channel(fn, dma_data->filter_data);
  203. }
  204. static bool dmaengine_pcm_can_report_residue(struct device *dev,
  205. struct dma_chan *chan)
  206. {
  207. struct dma_slave_caps dma_caps;
  208. int ret;
  209. ret = dma_get_slave_caps(chan, &dma_caps);
  210. if (ret != 0) {
  211. dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
  212. ret);
  213. return false;
  214. }
  215. if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
  216. return false;
  217. return true;
  218. }
  219. static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
  220. {
  221. struct snd_soc_component *component =
  222. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  223. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  224. const struct snd_dmaengine_pcm_config *config = pcm->config;
  225. struct device *dev = component->dev;
  226. struct snd_dmaengine_dai_dma_data *dma_data;
  227. struct snd_pcm_substream *substream;
  228. size_t prealloc_buffer_size;
  229. size_t max_buffer_size;
  230. unsigned int i;
  231. int ret;
  232. if (config && config->prealloc_buffer_size) {
  233. prealloc_buffer_size = config->prealloc_buffer_size;
  234. max_buffer_size = config->pcm_hardware->buffer_bytes_max;
  235. } else {
  236. prealloc_buffer_size = 512 * 1024;
  237. max_buffer_size = SIZE_MAX;
  238. }
  239. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
  240. substream = rtd->pcm->streams[i].substream;
  241. if (!substream)
  242. continue;
  243. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  244. if (!pcm->chan[i] &&
  245. (pcm->flags & SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME))
  246. pcm->chan[i] = dma_request_slave_channel(dev,
  247. dma_data->chan_name);
  248. if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
  249. pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
  250. substream);
  251. }
  252. if (!pcm->chan[i]) {
  253. dev_err(component->dev,
  254. "Missing dma channel for stream: %d\n", i);
  255. return -EINVAL;
  256. }
  257. ret = snd_pcm_lib_preallocate_pages(substream,
  258. SNDRV_DMA_TYPE_DEV_IRAM,
  259. dmaengine_dma_dev(pcm, substream),
  260. prealloc_buffer_size,
  261. max_buffer_size);
  262. if (ret)
  263. return ret;
  264. if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
  265. pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
  266. if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
  267. strncpy(rtd->pcm->streams[i].pcm->name,
  268. rtd->pcm->streams[i].pcm->id,
  269. sizeof(rtd->pcm->streams[i].pcm->name));
  270. }
  271. }
  272. return 0;
  273. }
  274. static snd_pcm_uframes_t dmaengine_pcm_pointer(
  275. struct snd_pcm_substream *substream)
  276. {
  277. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  278. struct snd_soc_component *component =
  279. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  280. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  281. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  282. return snd_dmaengine_pcm_pointer_no_residue(substream);
  283. else
  284. return snd_dmaengine_pcm_pointer(substream);
  285. }
  286. static int dmaengine_copy_user(struct snd_pcm_substream *substream,
  287. int channel, unsigned long hwoff,
  288. void __user *buf, unsigned long bytes)
  289. {
  290. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  291. struct snd_soc_component *component =
  292. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  293. struct snd_pcm_runtime *runtime = substream->runtime;
  294. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  295. int (*process)(struct snd_pcm_substream *substream,
  296. int channel, unsigned long hwoff,
  297. void *buf, unsigned long bytes) = pcm->config->process;
  298. bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  299. void *dma_ptr = runtime->dma_area + hwoff +
  300. channel * (runtime->dma_bytes / runtime->channels);
  301. int ret;
  302. if (is_playback)
  303. if (copy_from_user(dma_ptr, buf, bytes))
  304. return -EFAULT;
  305. if (process) {
  306. ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
  307. if (ret < 0)
  308. return ret;
  309. }
  310. if (!is_playback)
  311. if (copy_to_user(buf, dma_ptr, bytes))
  312. return -EFAULT;
  313. return 0;
  314. }
  315. static const struct snd_pcm_ops dmaengine_pcm_ops = {
  316. .open = dmaengine_pcm_open,
  317. .close = snd_dmaengine_pcm_close,
  318. .ioctl = snd_pcm_lib_ioctl,
  319. .hw_params = dmaengine_pcm_hw_params,
  320. .hw_free = snd_pcm_lib_free_pages,
  321. .trigger = snd_dmaengine_pcm_trigger,
  322. .pointer = dmaengine_pcm_pointer,
  323. };
  324. static const struct snd_pcm_ops dmaengine_pcm_process_ops = {
  325. .open = dmaengine_pcm_open,
  326. .close = snd_dmaengine_pcm_close,
  327. .ioctl = snd_pcm_lib_ioctl,
  328. .hw_params = dmaengine_pcm_hw_params,
  329. .hw_free = snd_pcm_lib_free_pages,
  330. .trigger = snd_dmaengine_pcm_trigger,
  331. .pointer = dmaengine_pcm_pointer,
  332. .copy_user = dmaengine_copy_user,
  333. };
  334. static const struct snd_soc_component_driver dmaengine_pcm_component = {
  335. .name = SND_DMAENGINE_PCM_DRV_NAME,
  336. .probe_order = SND_SOC_COMP_ORDER_LATE,
  337. .ops = &dmaengine_pcm_ops,
  338. .pcm_new = dmaengine_pcm_new,
  339. };
  340. static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
  341. .name = SND_DMAENGINE_PCM_DRV_NAME,
  342. .probe_order = SND_SOC_COMP_ORDER_LATE,
  343. .ops = &dmaengine_pcm_process_ops,
  344. .pcm_new = dmaengine_pcm_new,
  345. };
  346. static const char * const dmaengine_pcm_dma_channel_names[] = {
  347. [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
  348. [SNDRV_PCM_STREAM_CAPTURE] = "rx",
  349. };
  350. static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
  351. struct device *dev, const struct snd_dmaengine_pcm_config *config)
  352. {
  353. unsigned int i;
  354. const char *name;
  355. struct dma_chan *chan;
  356. if ((pcm->flags & (SND_DMAENGINE_PCM_FLAG_NO_DT |
  357. SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME)) ||
  358. !dev->of_node)
  359. return 0;
  360. if (config && config->dma_dev) {
  361. /*
  362. * If this warning is seen, it probably means that your Linux
  363. * device structure does not match your HW device structure.
  364. * It would be best to refactor the Linux device structure to
  365. * correctly match the HW structure.
  366. */
  367. dev_warn(dev, "DMA channels sourced from device %s",
  368. dev_name(config->dma_dev));
  369. dev = config->dma_dev;
  370. }
  371. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  372. i++) {
  373. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  374. name = "rx-tx";
  375. else
  376. name = dmaengine_pcm_dma_channel_names[i];
  377. if (config && config->chan_names[i])
  378. name = config->chan_names[i];
  379. chan = dma_request_slave_channel_reason(dev, name);
  380. if (IS_ERR(chan)) {
  381. if (PTR_ERR(chan) == -EPROBE_DEFER)
  382. return -EPROBE_DEFER;
  383. pcm->chan[i] = NULL;
  384. } else {
  385. pcm->chan[i] = chan;
  386. }
  387. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  388. break;
  389. }
  390. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  391. pcm->chan[1] = pcm->chan[0];
  392. return 0;
  393. }
  394. static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
  395. {
  396. unsigned int i;
  397. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  398. i++) {
  399. if (!pcm->chan[i])
  400. continue;
  401. dma_release_channel(pcm->chan[i]);
  402. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  403. break;
  404. }
  405. }
  406. /**
  407. * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
  408. * @dev: The parent device for the PCM device
  409. * @config: Platform specific PCM configuration
  410. * @flags: Platform specific quirks
  411. */
  412. int snd_dmaengine_pcm_register(struct device *dev,
  413. const struct snd_dmaengine_pcm_config *config, unsigned int flags)
  414. {
  415. struct dmaengine_pcm *pcm;
  416. int ret;
  417. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  418. if (!pcm)
  419. return -ENOMEM;
  420. #ifdef CONFIG_DEBUG_FS
  421. pcm->component.debugfs_prefix = "dma";
  422. #endif
  423. pcm->config = config;
  424. pcm->flags = flags;
  425. ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
  426. if (ret)
  427. goto err_free_dma;
  428. if (config && config->process)
  429. ret = snd_soc_add_component(dev, &pcm->component,
  430. &dmaengine_pcm_component_process,
  431. NULL, 0);
  432. else
  433. ret = snd_soc_add_component(dev, &pcm->component,
  434. &dmaengine_pcm_component, NULL, 0);
  435. if (ret)
  436. goto err_free_dma;
  437. return 0;
  438. err_free_dma:
  439. dmaengine_pcm_release_chan(pcm);
  440. kfree(pcm);
  441. return ret;
  442. }
  443. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
  444. /**
  445. * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
  446. * @dev: Parent device the PCM was register with
  447. *
  448. * Removes a dmaengine based PCM device previously registered with
  449. * snd_dmaengine_pcm_register.
  450. */
  451. void snd_dmaengine_pcm_unregister(struct device *dev)
  452. {
  453. struct snd_soc_component *component;
  454. struct dmaengine_pcm *pcm;
  455. component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
  456. if (!component)
  457. return;
  458. pcm = soc_component_to_pcm(component);
  459. snd_soc_unregister_component(dev);
  460. dmaengine_pcm_release_chan(pcm);
  461. kfree(pcm);
  462. }
  463. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
  464. MODULE_LICENSE("GPL");