soc-generic-dmaengine-pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (C) 2013, Analog Devices Inc.
  4. // Author: Lars-Peter Clausen <[email protected]>
  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. static unsigned int prealloc_buffer_size_kbytes = 512;
  16. module_param(prealloc_buffer_size_kbytes, uint, 0444);
  17. MODULE_PARM_DESC(prealloc_buffer_size_kbytes, "Preallocate DMA buffer size (KB).");
  18. /*
  19. * The platforms dmaengine driver does not support reporting the amount of
  20. * bytes that are still left to transfer.
  21. */
  22. #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
  23. static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
  24. struct snd_pcm_substream *substream)
  25. {
  26. if (!pcm->chan[substream->stream])
  27. return NULL;
  28. return pcm->chan[substream->stream]->device->dev;
  29. }
  30. /**
  31. * snd_dmaengine_pcm_prepare_slave_config() - Generic prepare_slave_config callback
  32. * @substream: PCM substream
  33. * @params: hw_params
  34. * @slave_config: DMA slave config to prepare
  35. *
  36. * This function can be used as a generic prepare_slave_config callback for
  37. * platforms which make use of the snd_dmaengine_dai_dma_data struct for their
  38. * DAI DMA data. Internally the function will first call
  39. * snd_hwparams_to_dma_slave_config to fill in the slave config based on the
  40. * hw_params, followed by snd_dmaengine_set_config_from_dai_data to fill in the
  41. * remaining fields based on the DAI DMA data.
  42. */
  43. int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
  44. struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
  45. {
  46. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  47. struct snd_dmaengine_dai_dma_data *dma_data;
  48. int ret;
  49. if (rtd->dai_link->num_cpus > 1) {
  50. dev_err(rtd->dev,
  51. "%s doesn't support Multi CPU yet\n", __func__);
  52. return -EINVAL;
  53. }
  54. dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  55. ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
  56. if (ret)
  57. return ret;
  58. snd_dmaengine_pcm_set_config_from_dai_data(substream, dma_data,
  59. slave_config);
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
  63. static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
  64. struct snd_pcm_substream *substream,
  65. struct snd_pcm_hw_params *params)
  66. {
  67. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  68. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  69. struct dma_slave_config slave_config;
  70. int ret;
  71. if (!pcm->config->prepare_slave_config)
  72. return 0;
  73. memset(&slave_config, 0, sizeof(slave_config));
  74. ret = pcm->config->prepare_slave_config(substream, params, &slave_config);
  75. if (ret)
  76. return ret;
  77. return dmaengine_slave_config(chan, &slave_config);
  78. }
  79. static int
  80. dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
  81. struct snd_pcm_substream *substream)
  82. {
  83. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  84. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  85. struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
  86. struct dma_chan *chan = pcm->chan[substream->stream];
  87. struct snd_dmaengine_dai_dma_data *dma_data;
  88. struct snd_pcm_hardware hw;
  89. if (rtd->dai_link->num_cpus > 1) {
  90. dev_err(rtd->dev,
  91. "%s doesn't support Multi CPU yet\n", __func__);
  92. return -EINVAL;
  93. }
  94. if (pcm->config->pcm_hardware)
  95. return snd_soc_set_runtime_hwparams(substream,
  96. pcm->config->pcm_hardware);
  97. dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  98. memset(&hw, 0, sizeof(hw));
  99. hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  100. SNDRV_PCM_INFO_INTERLEAVED;
  101. hw.periods_min = 2;
  102. hw.periods_max = UINT_MAX;
  103. hw.period_bytes_min = dma_data->maxburst * DMA_SLAVE_BUSWIDTH_8_BYTES;
  104. if (!hw.period_bytes_min)
  105. hw.period_bytes_min = 256;
  106. hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
  107. hw.buffer_bytes_max = SIZE_MAX;
  108. hw.fifo_size = dma_data->fifo_size;
  109. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  110. hw.info |= SNDRV_PCM_INFO_BATCH;
  111. /**
  112. * FIXME: Remove the return value check to align with the code
  113. * before adding snd_dmaengine_pcm_refine_runtime_hwparams
  114. * function.
  115. */
  116. snd_dmaengine_pcm_refine_runtime_hwparams(substream,
  117. dma_data,
  118. &hw,
  119. chan);
  120. return snd_soc_set_runtime_hwparams(substream, &hw);
  121. }
  122. static int dmaengine_pcm_open(struct snd_soc_component *component,
  123. struct snd_pcm_substream *substream)
  124. {
  125. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  126. struct dma_chan *chan = pcm->chan[substream->stream];
  127. int ret;
  128. ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
  129. if (ret)
  130. return ret;
  131. return snd_dmaengine_pcm_open(substream, chan);
  132. }
  133. static int dmaengine_pcm_close(struct snd_soc_component *component,
  134. struct snd_pcm_substream *substream)
  135. {
  136. return snd_dmaengine_pcm_close(substream);
  137. }
  138. static int dmaengine_pcm_trigger(struct snd_soc_component *component,
  139. struct snd_pcm_substream *substream, int cmd)
  140. {
  141. return snd_dmaengine_pcm_trigger(substream, cmd);
  142. }
  143. static struct dma_chan *dmaengine_pcm_compat_request_channel(
  144. struct snd_soc_component *component,
  145. struct snd_soc_pcm_runtime *rtd,
  146. struct snd_pcm_substream *substream)
  147. {
  148. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  149. struct snd_dmaengine_dai_dma_data *dma_data;
  150. if (rtd->dai_link->num_cpus > 1) {
  151. dev_err(rtd->dev,
  152. "%s doesn't support Multi CPU yet\n", __func__);
  153. return NULL;
  154. }
  155. dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  156. if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
  157. return pcm->chan[0];
  158. if (pcm->config->compat_request_channel)
  159. return pcm->config->compat_request_channel(rtd, substream);
  160. return snd_dmaengine_pcm_request_channel(pcm->config->compat_filter_fn,
  161. dma_data->filter_data);
  162. }
  163. static bool dmaengine_pcm_can_report_residue(struct device *dev,
  164. struct dma_chan *chan)
  165. {
  166. struct dma_slave_caps dma_caps;
  167. int ret;
  168. ret = dma_get_slave_caps(chan, &dma_caps);
  169. if (ret != 0) {
  170. dev_warn(dev, "Failed to get DMA channel capabilities, falling back to period counting: %d\n",
  171. ret);
  172. return false;
  173. }
  174. if (dma_caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR)
  175. return false;
  176. return true;
  177. }
  178. static int dmaengine_pcm_new(struct snd_soc_component *component,
  179. struct snd_soc_pcm_runtime *rtd)
  180. {
  181. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  182. const struct snd_dmaengine_pcm_config *config = pcm->config;
  183. struct device *dev = component->dev;
  184. size_t prealloc_buffer_size;
  185. size_t max_buffer_size;
  186. unsigned int i;
  187. if (config->prealloc_buffer_size)
  188. prealloc_buffer_size = config->prealloc_buffer_size;
  189. else
  190. prealloc_buffer_size = prealloc_buffer_size_kbytes * 1024;
  191. if (config->pcm_hardware && config->pcm_hardware->buffer_bytes_max)
  192. max_buffer_size = config->pcm_hardware->buffer_bytes_max;
  193. else
  194. max_buffer_size = SIZE_MAX;
  195. for_each_pcm_streams(i) {
  196. struct snd_pcm_substream *substream = rtd->pcm->streams[i].substream;
  197. if (!substream)
  198. continue;
  199. if (!pcm->chan[i] && config->chan_names[i])
  200. pcm->chan[i] = dma_request_slave_channel(dev,
  201. config->chan_names[i]);
  202. if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
  203. pcm->chan[i] = dmaengine_pcm_compat_request_channel(
  204. component, rtd, substream);
  205. }
  206. if (!pcm->chan[i]) {
  207. dev_err(component->dev,
  208. "Missing dma channel for stream: %d\n", i);
  209. return -EINVAL;
  210. }
  211. snd_pcm_set_managed_buffer(substream,
  212. SNDRV_DMA_TYPE_DEV_IRAM,
  213. dmaengine_dma_dev(pcm, substream),
  214. prealloc_buffer_size,
  215. max_buffer_size);
  216. if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
  217. pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
  218. if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
  219. strscpy_pad(rtd->pcm->streams[i].pcm->name,
  220. rtd->pcm->streams[i].pcm->id,
  221. sizeof(rtd->pcm->streams[i].pcm->name));
  222. }
  223. }
  224. return 0;
  225. }
  226. static snd_pcm_uframes_t dmaengine_pcm_pointer(
  227. struct snd_soc_component *component,
  228. struct snd_pcm_substream *substream)
  229. {
  230. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  231. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
  232. return snd_dmaengine_pcm_pointer_no_residue(substream);
  233. else
  234. return snd_dmaengine_pcm_pointer(substream);
  235. }
  236. static int dmaengine_copy_user(struct snd_soc_component *component,
  237. struct snd_pcm_substream *substream,
  238. int channel, unsigned long hwoff,
  239. void __user *buf, unsigned long bytes)
  240. {
  241. struct snd_pcm_runtime *runtime = substream->runtime;
  242. struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
  243. int (*process)(struct snd_pcm_substream *substream,
  244. int channel, unsigned long hwoff,
  245. void *buf, unsigned long bytes) = pcm->config->process;
  246. bool is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  247. void *dma_ptr = runtime->dma_area + hwoff +
  248. channel * (runtime->dma_bytes / runtime->channels);
  249. if (is_playback)
  250. if (copy_from_user(dma_ptr, buf, bytes))
  251. return -EFAULT;
  252. if (process) {
  253. int ret = process(substream, channel, hwoff, (__force void *)buf, bytes);
  254. if (ret < 0)
  255. return ret;
  256. }
  257. if (!is_playback)
  258. if (copy_to_user(buf, dma_ptr, bytes))
  259. return -EFAULT;
  260. return 0;
  261. }
  262. static const struct snd_soc_component_driver dmaengine_pcm_component = {
  263. .name = SND_DMAENGINE_PCM_DRV_NAME,
  264. .probe_order = SND_SOC_COMP_ORDER_LATE,
  265. .open = dmaengine_pcm_open,
  266. .close = dmaengine_pcm_close,
  267. .hw_params = dmaengine_pcm_hw_params,
  268. .trigger = dmaengine_pcm_trigger,
  269. .pointer = dmaengine_pcm_pointer,
  270. .pcm_construct = dmaengine_pcm_new,
  271. };
  272. static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
  273. .name = SND_DMAENGINE_PCM_DRV_NAME,
  274. .probe_order = SND_SOC_COMP_ORDER_LATE,
  275. .open = dmaengine_pcm_open,
  276. .close = dmaengine_pcm_close,
  277. .hw_params = dmaengine_pcm_hw_params,
  278. .trigger = dmaengine_pcm_trigger,
  279. .pointer = dmaengine_pcm_pointer,
  280. .copy_user = dmaengine_copy_user,
  281. .pcm_construct = dmaengine_pcm_new,
  282. };
  283. static const char * const dmaengine_pcm_dma_channel_names[] = {
  284. [SNDRV_PCM_STREAM_PLAYBACK] = "tx",
  285. [SNDRV_PCM_STREAM_CAPTURE] = "rx",
  286. };
  287. static int dmaengine_pcm_request_chan_of(struct dmaengine_pcm *pcm,
  288. struct device *dev, const struct snd_dmaengine_pcm_config *config)
  289. {
  290. unsigned int i;
  291. const char *name;
  292. struct dma_chan *chan;
  293. if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
  294. !(config->dma_dev && config->dma_dev->of_node)))
  295. return 0;
  296. if (config->dma_dev) {
  297. /*
  298. * If this warning is seen, it probably means that your Linux
  299. * device structure does not match your HW device structure.
  300. * It would be best to refactor the Linux device structure to
  301. * correctly match the HW structure.
  302. */
  303. dev_warn(dev, "DMA channels sourced from device %s",
  304. dev_name(config->dma_dev));
  305. dev = config->dma_dev;
  306. }
  307. for_each_pcm_streams(i) {
  308. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  309. name = "rx-tx";
  310. else
  311. name = dmaengine_pcm_dma_channel_names[i];
  312. if (config->chan_names[i])
  313. name = config->chan_names[i];
  314. chan = dma_request_chan(dev, name);
  315. if (IS_ERR(chan)) {
  316. /*
  317. * Only report probe deferral errors, channels
  318. * might not be present for devices that
  319. * support only TX or only RX.
  320. */
  321. if (PTR_ERR(chan) == -EPROBE_DEFER)
  322. return -EPROBE_DEFER;
  323. pcm->chan[i] = NULL;
  324. } else {
  325. pcm->chan[i] = chan;
  326. }
  327. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  328. break;
  329. }
  330. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  331. pcm->chan[1] = pcm->chan[0];
  332. return 0;
  333. }
  334. static void dmaengine_pcm_release_chan(struct dmaengine_pcm *pcm)
  335. {
  336. unsigned int i;
  337. for_each_pcm_streams(i) {
  338. if (!pcm->chan[i])
  339. continue;
  340. dma_release_channel(pcm->chan[i]);
  341. if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
  342. break;
  343. }
  344. }
  345. static const struct snd_dmaengine_pcm_config snd_dmaengine_pcm_default_config = {
  346. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  347. };
  348. /**
  349. * snd_dmaengine_pcm_register - Register a dmaengine based PCM device
  350. * @dev: The parent device for the PCM device
  351. * @config: Platform specific PCM configuration
  352. * @flags: Platform specific quirks
  353. */
  354. int snd_dmaengine_pcm_register(struct device *dev,
  355. const struct snd_dmaengine_pcm_config *config, unsigned int flags)
  356. {
  357. const struct snd_soc_component_driver *driver;
  358. struct dmaengine_pcm *pcm;
  359. int ret;
  360. pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
  361. if (!pcm)
  362. return -ENOMEM;
  363. #ifdef CONFIG_DEBUG_FS
  364. pcm->component.debugfs_prefix = "dma";
  365. #endif
  366. if (!config)
  367. config = &snd_dmaengine_pcm_default_config;
  368. pcm->config = config;
  369. pcm->flags = flags;
  370. ret = dmaengine_pcm_request_chan_of(pcm, dev, config);
  371. if (ret)
  372. goto err_free_dma;
  373. if (config->process)
  374. driver = &dmaengine_pcm_component_process;
  375. else
  376. driver = &dmaengine_pcm_component;
  377. ret = snd_soc_component_initialize(&pcm->component, driver, dev);
  378. if (ret)
  379. goto err_free_dma;
  380. ret = snd_soc_add_component(&pcm->component, NULL, 0);
  381. if (ret)
  382. goto err_free_dma;
  383. return 0;
  384. err_free_dma:
  385. dmaengine_pcm_release_chan(pcm);
  386. kfree(pcm);
  387. return ret;
  388. }
  389. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_register);
  390. /**
  391. * snd_dmaengine_pcm_unregister - Removes a dmaengine based PCM device
  392. * @dev: Parent device the PCM was register with
  393. *
  394. * Removes a dmaengine based PCM device previously registered with
  395. * snd_dmaengine_pcm_register.
  396. */
  397. void snd_dmaengine_pcm_unregister(struct device *dev)
  398. {
  399. struct snd_soc_component *component;
  400. struct dmaengine_pcm *pcm;
  401. component = snd_soc_lookup_component(dev, SND_DMAENGINE_PCM_DRV_NAME);
  402. if (!component)
  403. return;
  404. pcm = soc_component_to_pcm(component);
  405. snd_soc_unregister_component_by_driver(dev, component->driver);
  406. dmaengine_pcm_release_chan(pcm);
  407. kfree(pcm);
  408. }
  409. EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_unregister);
  410. MODULE_LICENSE("GPL");