lpass-pcm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/dma-mapping.h>
  13. #include <linux/slab.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <mach/audio_dma_msm8k.h>
  19. #include <sound/dai.h>
  20. #include "lpass-pcm.h"
  21. static const struct snd_pcm_hardware msm_pcm_hardware = {
  22. .info = SNDRV_PCM_INFO_MMAP |
  23. SNDRV_PCM_INFO_MMAP_VALID |
  24. SNDRV_PCM_INFO_INTERLEAVED |
  25. SNDRV_PCM_INFO_PAUSE |
  26. SNDRV_PCM_INFO_RESUME,
  27. .rates = SNDRV_PCM_RATE_8000_48000,
  28. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  29. .period_bytes_min = 32,
  30. .period_bytes_max = DMASZ/4,
  31. .buffer_bytes_max = DMASZ,
  32. .rate_max = 96000,
  33. .rate_min = 8000,
  34. .channels_min = USE_CHANNELS_MIN,
  35. .channels_max = USE_CHANNELS_MAX,
  36. .periods_min = 4,
  37. .periods_max = 512,
  38. .fifo_size = 0,
  39. };
  40. struct msm_pcm_data {
  41. spinlock_t lock;
  42. int ch;
  43. };
  44. /* Conventional and unconventional sample rate supported */
  45. static unsigned int supported_sample_rates[] = {
  46. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  47. };
  48. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  49. .count = ARRAY_SIZE(supported_sample_rates),
  50. .list = supported_sample_rates,
  51. .mask = 0,
  52. };
  53. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  54. struct snd_pcm_hw_params *params)
  55. {
  56. pr_debug("%s\n", __func__);
  57. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  58. return 0;
  59. }
  60. static irqreturn_t msm_pcm_irq(int intrsrc, void *data)
  61. {
  62. struct snd_pcm_substream *substream = data;
  63. struct snd_pcm_runtime *runtime = substream->runtime;
  64. struct msm_audio *prtd = (struct msm_audio *)runtime->private_data;
  65. int dma_ch = 0;
  66. unsigned int has_xrun, pending;
  67. int ret = IRQ_NONE;
  68. if (prtd)
  69. dma_ch = prtd->dma_ch;
  70. else
  71. return ret;
  72. pr_debug("msm8660-pcm: msm_pcm_irq called\n");
  73. pending = (intrsrc
  74. & (UNDER_CH(dma_ch) | PER_CH(dma_ch) | ERR_CH(dma_ch)));
  75. has_xrun = (pending & UNDER_CH(dma_ch));
  76. if (unlikely(has_xrun) &&
  77. substream->runtime &&
  78. snd_pcm_running(substream)) {
  79. pr_err("xrun\n");
  80. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  81. ret = IRQ_HANDLED;
  82. pending &= ~UNDER_CH(dma_ch);
  83. }
  84. if (pending & PER_CH(dma_ch)) {
  85. ret = IRQ_HANDLED;
  86. if (likely(substream->runtime &&
  87. snd_pcm_running(substream))) {
  88. /* end of buffer missed? loop back */
  89. if (++prtd->period_index >= runtime->periods)
  90. prtd->period_index = 0;
  91. snd_pcm_period_elapsed(substream);
  92. pr_debug("period elapsed\n");
  93. }
  94. pending &= ~PER_CH(dma_ch);
  95. }
  96. if (unlikely(pending
  97. & (UNDER_CH(dma_ch) & PER_CH(dma_ch) & ERR_CH(dma_ch)))) {
  98. if (pending & UNDER_CH(dma_ch))
  99. pr_err("msm8660-pcm: DMA %x Underflow\n",
  100. dma_ch);
  101. if (pending & ERR_CH(dma_ch))
  102. pr_err("msm8660-pcm: DMA %x Master Error\n",
  103. dma_ch);
  104. }
  105. return ret;
  106. }
  107. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  108. {
  109. struct snd_pcm_runtime *runtime = substream->runtime;
  110. struct msm_audio *prtd = (struct msm_audio *)runtime->private_data;
  111. struct dai_dma_params dma_params;
  112. int dma_ch = 0;
  113. if (prtd)
  114. dma_ch = prtd->dma_ch;
  115. else
  116. return 0;
  117. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  118. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  119. pr_debug("%s:prtd->pcm_size = %d\n", __func__, prtd->pcm_size);
  120. pr_debug("%s:prtd->pcm_count = %d\n", __func__, prtd->pcm_count);
  121. if (prtd->enabled)
  122. return 0;
  123. dma_params.src_start = runtime->dma_addr;
  124. dma_params.buffer = (u8 *)runtime->dma_area;
  125. dma_params.buffer_size = prtd->pcm_size;
  126. dma_params.period_size = prtd->pcm_count;
  127. dma_params.channels = runtime->channels;
  128. dai_set_params(dma_ch, &dma_params);
  129. register_dma_irq_handler(dma_ch, msm_pcm_irq, (void *)substream);
  130. prtd->enabled = 1;
  131. return 0;
  132. }
  133. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  134. {
  135. struct snd_pcm_runtime *runtime = substream->runtime;
  136. struct msm_audio *prtd = (struct msm_audio *)runtime->private_data;
  137. int ret = 0;
  138. pr_debug("%s\n", __func__);
  139. switch (cmd) {
  140. case SNDRV_PCM_TRIGGER_START:
  141. case SNDRV_PCM_TRIGGER_RESUME:
  142. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  143. dai_start(prtd->dma_ch);
  144. break;
  145. case SNDRV_PCM_TRIGGER_STOP:
  146. case SNDRV_PCM_TRIGGER_SUSPEND:
  147. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  148. dai_stop(prtd->dma_ch);
  149. break;
  150. default:
  151. ret = -EINVAL;
  152. }
  153. return ret;
  154. }
  155. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  156. {
  157. struct snd_pcm_runtime *runtime = substream->runtime;
  158. struct msm_audio *prtd = (struct msm_audio *)runtime->private_data;
  159. snd_pcm_uframes_t offset = 0;
  160. pr_debug("%s: period_index =%d\n", __func__, prtd->period_index);
  161. offset = prtd->period_index * runtime->period_size;
  162. if (offset >= runtime->buffer_size)
  163. offset = 0;
  164. return offset;
  165. }
  166. static int msm_pcm_open(struct snd_pcm_substream *substream)
  167. {
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  170. struct snd_soc_dai_link *machine = rtd->dai;
  171. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  172. struct msm_audio *prtd = NULL;
  173. int ret = 0;
  174. pr_debug("%s\n", __func__);
  175. snd_soc_set_runtime_hwparams(substream, &msm_pcm_hardware);
  176. ret = snd_pcm_hw_constraint_integer(runtime,
  177. SNDRV_PCM_HW_PARAM_PERIODS);
  178. if (ret < 0) {
  179. pr_err("Error setting hw_constraint\n");
  180. goto err;
  181. }
  182. ret = snd_pcm_hw_constraint_list(runtime, 0,
  183. SNDRV_PCM_HW_PARAM_RATE,
  184. &constraints_sample_rates);
  185. if (ret < 0)
  186. pr_err("Error snd_pcm_hw_constraint_list failed\n");
  187. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  188. if (prtd == NULL) {
  189. pr_err("Error allocating prtd\n");
  190. ret = -ENOMEM;
  191. goto err;
  192. }
  193. prtd->dma_ch = cpu_dai->id;
  194. prtd->enabled = 0;
  195. runtime->dma_bytes = msm_pcm_hardware.buffer_bytes_max;
  196. runtime->private_data = prtd;
  197. err:
  198. return ret;
  199. }
  200. static int msm_pcm_close(struct snd_pcm_substream *substream)
  201. {
  202. struct snd_pcm_runtime *runtime = substream->runtime;
  203. struct msm_audio *prtd = (struct msm_audio *)runtime->private_data;
  204. int dma_ch = 0;
  205. if (prtd)
  206. dma_ch = prtd->dma_ch;
  207. else
  208. return 0;
  209. pr_debug("%s\n", __func__);
  210. unregister_dma_irq_handler(dma_ch);
  211. kfree(runtime->private_data);
  212. return 0;
  213. }
  214. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  215. struct vm_area_struct *vms)
  216. {
  217. struct snd_pcm_runtime *runtime = substream->runtime;
  218. pr_debug("%s\n", __func__);
  219. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  220. pr_debug("%s: snd_msm_audio_hw_params runtime->dma_addr 0x(%x)\n",
  221. __func__, (unsigned int)runtime->dma_addr);
  222. pr_debug("%s: snd_msm_audio_hw_params runtime->dma_area 0x(%x)\n",
  223. __func__, (unsigned int)runtime->dma_area);
  224. pr_debug("%s: snd_msm_audio_hw_params runtime->dma_bytes 0x(%x)\n",
  225. __func__, (unsigned int)runtime->dma_bytes);
  226. return dma_mmap_coherent(substream->pcm->card->dev, vms,
  227. runtime->dma_area,
  228. runtime->dma_addr,
  229. runtime->dma_bytes);
  230. }
  231. static struct snd_pcm_ops msm_pcm_ops = {
  232. .open = msm_pcm_open,
  233. .close = msm_pcm_close,
  234. .ioctl = snd_pcm_lib_ioctl,
  235. .hw_params = msm_pcm_hw_params,
  236. .prepare = msm_pcm_prepare,
  237. .trigger = msm_pcm_trigger,
  238. .pointer = msm_pcm_pointer,
  239. .mmap = msm_pcm_mmap,
  240. };
  241. static int pcm_preallocate_buffer(struct snd_pcm *pcm,
  242. int stream)
  243. {
  244. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  245. struct snd_dma_buffer *buf = &substream->dma_buffer;
  246. size_t size = msm_pcm_hardware.buffer_bytes_max;
  247. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  248. buf->dev.dev = pcm->card->dev;
  249. buf->private_data = NULL;
  250. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  251. &buf->addr, GFP_KERNEL);
  252. if (!buf->area)
  253. return -ENOMEM;
  254. buf->bytes = size;
  255. return 0;
  256. }
  257. static void msm_pcm_free_buffers(struct snd_pcm *pcm)
  258. {
  259. struct snd_pcm_substream *substream;
  260. struct snd_dma_buffer *buf;
  261. int stream;
  262. for (stream = 0; stream < 2; stream++) {
  263. substream = pcm->streams[stream].substream;
  264. if (!stream)
  265. continue;
  266. buf = &substream->dma_buffer;
  267. if (!buf->area)
  268. continue;
  269. dma_free_coherent(pcm->card->dev, buf->bytes,
  270. buf->area, buf->addr);
  271. buf->area = NULL;
  272. }
  273. }
  274. static u64 msm_pcm_dmamask = DMA_BIT_MASK(32);
  275. static int msm_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  276. struct snd_pcm *pcm)
  277. {
  278. int ret = 0;
  279. if (!card->dev->dma_mask)
  280. card->dev->dma_mask = &msm_pcm_dmamask;
  281. if (!card->dev->coherent_dma_mask)
  282. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  283. if (dai->playback.channels_min) {
  284. ret = pcm_preallocate_buffer(pcm,
  285. SNDRV_PCM_STREAM_PLAYBACK);
  286. if (ret)
  287. return ret;
  288. }
  289. if (dai->capture.channels_min) {
  290. ret = pcm_preallocate_buffer(pcm,
  291. SNDRV_PCM_STREAM_CAPTURE);
  292. if (ret)
  293. return ret;
  294. }
  295. return ret;
  296. }
  297. struct snd_soc_platform msm8660_soc_platform = {
  298. .name = "msm8660-pcm-audio",
  299. .pcm_ops = &msm_pcm_ops,
  300. .pcm_new = msm_pcm_new,
  301. .pcm_free = msm_pcm_free_buffers,
  302. };
  303. EXPORT_SYMBOL_GPL(msm8660_soc_platform);
  304. static int __init msm_soc_platform_init(void)
  305. {
  306. return snd_soc_register_platform(&msm8660_soc_platform);
  307. }
  308. static void __exit msm_soc_platform_exit(void)
  309. {
  310. snd_soc_unregister_platform(&msm8660_soc_platform);
  311. }
  312. module_init(msm_soc_platform_init);
  313. module_exit(msm_soc_platform_exit);
  314. MODULE_DESCRIPTION("MSM PCM module");
  315. MODULE_LICENSE("GPL v2");