soc-generic-dmaengine-pcm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. }
  267. return 0;
  268. }
  269. static snd_pcm_uframes_t dmaengine_pcm_pointer(
  270. struct snd_pcm_substream *substream)
  271. {
  272. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  273. struct snd_soc_component *component =
  274. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  275. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  276. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  277. return snd_dmaengine_pcm_pointer_no_residue(substream);
  278. else
  279. return snd_dmaengine_pcm_pointer(substream);
  280. }
  281. static int dmaengine_copy_user(struct snd_pcm_substream *substream,
  282. int channel, unsigned long hwoff,
  283. void __user *buf, unsigned long bytes)
  284. {
  285. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  286. struct snd_soc_component *component =
  287. snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
  288. struct snd_pcm_runtime *runtime = substream->runtime;
  289. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  290. int (*process)(struct snd_pcm_substream *substream,
  291. int channel, unsigned long hwoff,
  292. void *buf, unsigned long bytes) = pcm->config->process;
  293. bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  294. void *dma_ptr = runtime->dma_area + hwoff +
  295. channel * (runtime->dma_bytes / runtime->channels);
  296. int ret;
  297. if (is_playback)
  298. if (copy_from_user(dma_ptr, buf, bytes))
  299. return -EFAULT;
  300. if (process) {
  301. ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
  302. if (ret < 0)
  303. return ret;
  304. }
  305. if (!is_playback)
  306. if (copy_to_user(buf, dma_ptr, bytes))
  307. return -EFAULT;
  308. return 0;
  309. }
  310. static const struct snd_pcm_ops dmaengine_pcm_ops = {
  311. .open = dmaengine_pcm_open,
  312. .close = snd_dmaengine_pcm_close,
  313. .ioctl = snd_pcm_lib_ioctl,
  314. .hw_params = dmaengine_pcm_hw_params,
  315. .hw_free = snd_pcm_lib_free_pages,
  316. .trigger = snd_dmaengine_pcm_trigger,
  317. .pointer = dmaengine_pcm_pointer,
  318. };
  319. static const struct snd_pcm_ops dmaengine_pcm_process_ops = {
  320. .open = dmaengine_pcm_open,
  321. .close = snd_dmaengine_pcm_close,
  322. .ioctl = snd_pcm_lib_ioctl,
  323. .hw_params = dmaengine_pcm_hw_params,
  324. .hw_free = snd_pcm_lib_free_pages,
  325. .trigger = snd_dmaengine_pcm_trigger,
  326. .pointer = dmaengine_pcm_pointer,
  327. .copy_user = dmaengine_copy_user,
  328. };
  329. static const struct snd_soc_component_driver dmaengine_pcm_component = {
  330. .name = SND_DMAENGINE_PCM_DRV_NAME,
  331. .probe_order = SND_SOC_COMP_ORDER_LATE,
  332. .ops = &dmaengine_pcm_ops,
  333. .pcm_new = dmaengine_pcm_new,
  334. };
  335. static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
  336. .name = SND_DMAENGINE_PCM_DRV_NAME,
  337. .probe_order = SND_SOC_COMP_ORDER_LATE,
  338. .ops = &dmaengine_pcm_process_ops,
  339. .pcm_new = dmaengine_pcm_new,
  340. };
  341. static const char * const dmaengine_pcm_dma_channel_names[] = {
  342. [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
  343. [SNDRV_PCM_STREAM_CAPTURE] = "rx",
  344. };
  345. static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
  346. struct device *dev, const struct snd_dmaengine_pcm_config *config)
  347. {
  348. unsigned int i;
  349. const char *name;
  350. struct dma_chan *chan;
  351. if ((pcm->flags & (SND_DMAENGINE_PCM_FLAG_NO_DT |
  352. SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME)) ||
  353. !dev->of_node)
  354. return 0;
  355. if (config && config->dma_dev) {
  356. /*
  357. * If this warning is seen, it probably means that your Linux
  358. * device structure does not match your HW device structure.
  359. * It would be best to refactor the Linux device structure to
  360. * correctly match the HW structure.
  361. */
  362. dev_warn(dev, "DMA channels sourced from device %s",
  363. dev_name(config->dma_dev));
  364. dev = config->dma_dev;
  365. }
  366. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  367. i++) {
  368. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  369. name = "rx-tx";
  370. else
  371. name = dmaengine_pcm_dma_channel_names[i];
  372. if (config && config->chan_names[i])
  373. name = config->chan_names[i];
  374. chan = dma_request_slave_channel_reason(dev, name);
  375. if (IS_ERR(chan)) {
  376. if (PTR_ERR(chan) == -EPROBE_DEFER)
  377. return -EPROBE_DEFER;
  378. pcm->chan[i] = NULL;
  379. } else {
  380. pcm->chan[i] = chan;
  381. }
  382. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  383. break;
  384. }
  385. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  386. pcm->chan[1] = pcm->chan[0];
  387. return 0;
  388. }
  389. static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
  390. {
  391. unsigned int i;
  392. for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
  393. i++) {
  394. if (!pcm->chan[i])
  395. continue;
  396. dma_release_channel(pcm->chan[i]);
  397. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  398. break;
  399. }
  400. }
  401. /**
  402. * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
  403. * @dev: The parent device for the PCM device
  404. * @config: Platform specific PCM configuration
  405. * @flags: Platform specific quirks
  406. */
  407. int snd_dmaengine_pcm_register(struct device *dev,
  408. const struct snd_dmaengine_pcm_config *config, unsigned int flags)
  409. {
  410. struct dmaengine_pcm *pcm;
  411. int ret;
  412. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  413. if (!pcm)
  414. return -ENOMEM;
  415. #ifdef CONFIG_DEBUG_FS
  416. pcm->component.debugfs_prefix = "dma";
  417. #endif
  418. pcm->config = config;
  419. pcm->flags = flags;
  420. ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
  421. if (ret)
  422. goto err_free_dma;
  423. if (config && config->process)
  424. ret = snd_soc_add_component(dev, &pcm->component,
  425. &dmaengine_pcm_component_process,
  426. NULL, 0);
  427. else
  428. ret = snd_soc_add_component(dev, &pcm->component,
  429. &dmaengine_pcm_component, NULL, 0);
  430. if (ret)
  431. goto err_free_dma;
  432. return 0;
  433. err_free_dma:
  434. dmaengine_pcm_release_chan(pcm);
  435. kfree(pcm);
  436. return ret;
  437. }
  438. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
  439. /**
  440. * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
  441. * @dev: Parent device the PCM was register with
  442. *
  443. * Removes a dmaengine based PCM device previously registered with
  444. * snd_dmaengine_pcm_register.
  445. */
  446. void snd_dmaengine_pcm_unregister(struct device *dev)
  447. {
  448. struct snd_soc_component *component;
  449. struct dmaengine_pcm *pcm;
  450. component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
  451. if (!component)
  452. return;
  453. pcm = soc_component_to_pcm(component);
  454. snd_soc_unregister_component(dev);
  455. dmaengine_pcm_release_chan(pcm);
  456. kfree(pcm);
  457. }
  458. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
  459. MODULE_LICENSE("GPL");