davinci-pcm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * ALSA PCM interface for the TI DAVINCI processor
  3. *
  4. * Author: Vladimir Barinov, <vbarinov@embeddedalley.com>
  5. * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  6. * added SRAM ping/pong (C) 2008 Troy Kisky <troy.kisky@boundarydevices.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/kernel.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <asm/dma.h>
  23. #include <mach/edma.h>
  24. #include <mach/sram.h>
  25. #include "davinci-pcm.h"
  26. #ifdef DEBUG
  27. static void print_buf_info(int slot, char *name)
  28. {
  29. struct edmacc_param p;
  30. if (slot < 0)
  31. return;
  32. edma_read_slot(slot, &p);
  33. printk(KERN_DEBUG "%s: 0x%x, opt=%x, src=%x, a_b_cnt=%x dst=%x\n",
  34. name, slot, p.opt, p.src, p.a_b_cnt, p.dst);
  35. printk(KERN_DEBUG " src_dst_bidx=%x link_bcntrld=%x src_dst_cidx=%x ccnt=%x\n",
  36. p.src_dst_bidx, p.link_bcntrld, p.src_dst_cidx, p.ccnt);
  37. }
  38. #else
  39. static void print_buf_info(int slot, char *name)
  40. {
  41. }
  42. #endif
  43. #define DAVINCI_PCM_FMTBITS (\
  44. SNDRV_PCM_FMTBIT_S8 |\
  45. SNDRV_PCM_FMTBIT_U8 |\
  46. SNDRV_PCM_FMTBIT_S16_LE |\
  47. SNDRV_PCM_FMTBIT_S16_BE |\
  48. SNDRV_PCM_FMTBIT_U16_LE |\
  49. SNDRV_PCM_FMTBIT_U16_BE |\
  50. SNDRV_PCM_FMTBIT_S24_LE |\
  51. SNDRV_PCM_FMTBIT_S24_BE |\
  52. SNDRV_PCM_FMTBIT_U24_LE |\
  53. SNDRV_PCM_FMTBIT_U24_BE |\
  54. SNDRV_PCM_FMTBIT_S32_LE |\
  55. SNDRV_PCM_FMTBIT_S32_BE |\
  56. SNDRV_PCM_FMTBIT_U32_LE |\
  57. SNDRV_PCM_FMTBIT_U32_BE)
  58. static struct snd_pcm_hardware pcm_hardware_playback = {
  59. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  60. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  61. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME|
  62. SNDRV_PCM_INFO_BATCH),
  63. .formats = DAVINCI_PCM_FMTBITS,
  64. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  65. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
  66. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  67. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
  68. SNDRV_PCM_RATE_KNOT),
  69. .rate_min = 8000,
  70. .rate_max = 96000,
  71. .channels_min = 2,
  72. .channels_max = 384,
  73. .buffer_bytes_max = 128 * 1024,
  74. .period_bytes_min = 32,
  75. .period_bytes_max = 8 * 1024,
  76. .periods_min = 16,
  77. .periods_max = 255,
  78. .fifo_size = 0,
  79. };
  80. static struct snd_pcm_hardware pcm_hardware_capture = {
  81. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  82. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  83. SNDRV_PCM_INFO_PAUSE |
  84. SNDRV_PCM_INFO_BATCH),
  85. .formats = DAVINCI_PCM_FMTBITS,
  86. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  87. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
  88. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  89. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
  90. SNDRV_PCM_RATE_KNOT),
  91. .rate_min = 8000,
  92. .rate_max = 96000,
  93. .channels_min = 2,
  94. .channels_max = 384,
  95. .buffer_bytes_max = 128 * 1024,
  96. .period_bytes_min = 32,
  97. .period_bytes_max = 8 * 1024,
  98. .periods_min = 16,
  99. .periods_max = 255,
  100. .fifo_size = 0,
  101. };
  102. /*
  103. * How ping/pong works....
  104. *
  105. * Playback:
  106. * ram_params - copys 2*ping_size from start of SDRAM to iram,
  107. * links to ram_link2
  108. * ram_link2 - copys rest of SDRAM to iram in ping_size units,
  109. * links to ram_link
  110. * ram_link - copys entire SDRAM to iram in ping_size uints,
  111. * links to self
  112. *
  113. * asp_params - same as asp_link[0]
  114. * asp_link[0] - copys from lower half of iram to asp port
  115. * links to asp_link[1], triggers iram copy event on completion
  116. * asp_link[1] - copys from upper half of iram to asp port
  117. * links to asp_link[0], triggers iram copy event on completion
  118. * triggers interrupt only needed to let upper SOC levels update position
  119. * in stream on completion
  120. *
  121. * When playback is started:
  122. * ram_params started
  123. * asp_params started
  124. *
  125. * Capture:
  126. * ram_params - same as ram_link,
  127. * links to ram_link
  128. * ram_link - same as playback
  129. * links to self
  130. *
  131. * asp_params - same as playback
  132. * asp_link[0] - same as playback
  133. * asp_link[1] - same as playback
  134. *
  135. * When capture is started:
  136. * asp_params started
  137. */
  138. struct davinci_runtime_data {
  139. spinlock_t lock;
  140. int period; /* current DMA period */
  141. int asp_channel; /* Master DMA channel */
  142. int asp_link[2]; /* asp parameter link channel, ping/pong */
  143. struct davinci_pcm_dma_params *params; /* DMA params */
  144. int ram_channel;
  145. int ram_link;
  146. int ram_link2;
  147. struct edmacc_param asp_params;
  148. struct edmacc_param ram_params;
  149. };
  150. static void davinci_pcm_period_elapsed(struct snd_pcm_substream *substream)
  151. {
  152. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  153. struct snd_pcm_runtime *runtime = substream->runtime;
  154. prtd->period++;
  155. if (unlikely(prtd->period >= runtime->periods))
  156. prtd->period = 0;
  157. }
  158. static void davinci_pcm_period_reset(struct snd_pcm_substream *substream)
  159. {
  160. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  161. prtd->period = 0;
  162. }
  163. /*
  164. * Not used with ping/pong
  165. */
  166. static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
  167. {
  168. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  169. struct snd_pcm_runtime *runtime = substream->runtime;
  170. int link = prtd->asp_link[0];
  171. unsigned int period_size;
  172. unsigned int dma_offset;
  173. dma_addr_t dma_pos;
  174. dma_addr_t src, dst;
  175. unsigned short src_bidx, dst_bidx;
  176. unsigned short src_cidx, dst_cidx;
  177. unsigned int data_type;
  178. unsigned short acnt;
  179. unsigned int count;
  180. unsigned int fifo_level;
  181. period_size = snd_pcm_lib_period_bytes(substream);
  182. dma_offset = prtd->period * period_size;
  183. dma_pos = runtime->dma_addr + dma_offset;
  184. fifo_level = prtd->params->fifo_level;
  185. pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
  186. "dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
  187. data_type = prtd->params->data_type;
  188. count = period_size / data_type;
  189. if (fifo_level)
  190. count /= fifo_level;
  191. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  192. src = dma_pos;
  193. dst = prtd->params->dma_addr;
  194. src_bidx = data_type;
  195. dst_bidx = 0;
  196. src_cidx = data_type * fifo_level;
  197. dst_cidx = 0;
  198. } else {
  199. src = prtd->params->dma_addr;
  200. dst = dma_pos;
  201. src_bidx = 0;
  202. dst_bidx = data_type;
  203. src_cidx = 0;
  204. dst_cidx = data_type * fifo_level;
  205. }
  206. acnt = prtd->params->acnt;
  207. edma_set_src(link, src, INCR, W8BIT);
  208. edma_set_dest(link, dst, INCR, W8BIT);
  209. edma_set_src_index(link, src_bidx, src_cidx);
  210. edma_set_dest_index(link, dst_bidx, dst_cidx);
  211. if (!fifo_level)
  212. edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
  213. else
  214. edma_set_transfer_params(link, acnt, fifo_level, count,
  215. fifo_level, ABSYNC);
  216. }
  217. static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
  218. {
  219. struct snd_pcm_substream *substream = data;
  220. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  221. print_buf_info(prtd->ram_channel, "i ram_channel");
  222. pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
  223. if (unlikely(ch_status != DMA_COMPLETE))
  224. return;
  225. if (snd_pcm_running(substream)) {
  226. spin_lock(&prtd->lock);
  227. if (prtd->ram_channel < 0) {
  228. /* No ping/pong must fix up link dma data*/
  229. davinci_pcm_enqueue_dma(substream);
  230. }
  231. davinci_pcm_period_elapsed(substream);
  232. spin_unlock(&prtd->lock);
  233. snd_pcm_period_elapsed(substream);
  234. }
  235. }
  236. static int allocate_sram(struct snd_pcm_substream *substream, unsigned size,
  237. struct snd_pcm_hardware *ppcm)
  238. {
  239. struct snd_dma_buffer *buf = &substream->dma_buffer;
  240. struct snd_dma_buffer *iram_dma = NULL;
  241. dma_addr_t iram_phys = 0;
  242. void *iram_virt = NULL;
  243. if (buf->private_data || !size)
  244. return 0;
  245. ppcm->period_bytes_max = size;
  246. iram_virt = sram_alloc(size, &iram_phys);
  247. if (!iram_virt)
  248. goto exit1;
  249. iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL);
  250. if (!iram_dma)
  251. goto exit2;
  252. iram_dma->area = iram_virt;
  253. iram_dma->addr = iram_phys;
  254. memset(iram_dma->area, 0, size);
  255. iram_dma->bytes = size;
  256. buf->private_data = iram_dma;
  257. return 0;
  258. exit2:
  259. if (iram_virt)
  260. sram_free(iram_virt, size);
  261. exit1:
  262. return -ENOMEM;
  263. }
  264. /*
  265. * Only used with ping/pong.
  266. * This is called after runtime->dma_addr, period_bytes and data_type are valid
  267. */
  268. static int ping_pong_dma_setup(struct snd_pcm_substream *substream)
  269. {
  270. unsigned short ram_src_cidx, ram_dst_cidx;
  271. struct snd_pcm_runtime *runtime = substream->runtime;
  272. struct davinci_runtime_data *prtd = runtime->private_data;
  273. struct snd_dma_buffer *iram_dma =
  274. (struct snd_dma_buffer *)substream->dma_buffer.private_data;
  275. struct davinci_pcm_dma_params *params = prtd->params;
  276. unsigned int data_type = params->data_type;
  277. unsigned int acnt = params->acnt;
  278. /* divide by 2 for ping/pong */
  279. unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1;
  280. int link = prtd->asp_link[1];
  281. unsigned int fifo_level = prtd->params->fifo_level;
  282. unsigned int count;
  283. if ((data_type == 0) || (data_type > 4)) {
  284. printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type);
  285. return -EINVAL;
  286. }
  287. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  288. dma_addr_t asp_src_pong = iram_dma->addr + ping_size;
  289. ram_src_cidx = ping_size;
  290. ram_dst_cidx = -ping_size;
  291. edma_set_src(link, asp_src_pong, INCR, W8BIT);
  292. link = prtd->asp_link[0];
  293. edma_set_src_index(link, data_type, data_type * fifo_level);
  294. link = prtd->asp_link[1];
  295. edma_set_src_index(link, data_type, data_type * fifo_level);
  296. link = prtd->ram_link;
  297. edma_set_src(link, runtime->dma_addr, INCR, W32BIT);
  298. } else {
  299. dma_addr_t asp_dst_pong = iram_dma->addr + ping_size;
  300. ram_src_cidx = -ping_size;
  301. ram_dst_cidx = ping_size;
  302. edma_set_dest(link, asp_dst_pong, INCR, W8BIT);
  303. link = prtd->asp_link[0];
  304. edma_set_dest_index(link, data_type, data_type * fifo_level);
  305. link = prtd->asp_link[1];
  306. edma_set_dest_index(link, data_type, data_type * fifo_level);
  307. link = prtd->ram_link;
  308. edma_set_dest(link, runtime->dma_addr, INCR, W32BIT);
  309. }
  310. if (!fifo_level) {
  311. count = ping_size / data_type;
  312. edma_set_transfer_params(prtd->asp_link[0], acnt, count,
  313. 1, 0, ASYNC);
  314. edma_set_transfer_params(prtd->asp_link[1], acnt, count,
  315. 1, 0, ASYNC);
  316. } else {
  317. count = ping_size / (data_type * fifo_level);
  318. edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
  319. count, fifo_level, ABSYNC);
  320. edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level,
  321. count, fifo_level, ABSYNC);
  322. }
  323. link = prtd->ram_link;
  324. edma_set_src_index(link, ping_size, ram_src_cidx);
  325. edma_set_dest_index(link, ping_size, ram_dst_cidx);
  326. edma_set_transfer_params(link, ping_size, 2,
  327. runtime->periods, 2, ASYNC);
  328. /* init master params */
  329. edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
  330. edma_read_slot(prtd->ram_link, &prtd->ram_params);
  331. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  332. struct edmacc_param p_ram;
  333. /* Copy entire iram buffer before playback started */
  334. prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1);
  335. /* 0 dst_bidx */
  336. prtd->ram_params.src_dst_bidx = (ping_size << 1);
  337. /* 0 dst_cidx */
  338. prtd->ram_params.src_dst_cidx = (ping_size << 1);
  339. prtd->ram_params.ccnt = 1;
  340. /* Skip 1st period */
  341. edma_read_slot(prtd->ram_link, &p_ram);
  342. p_ram.src += (ping_size << 1);
  343. p_ram.ccnt -= 1;
  344. edma_write_slot(prtd->ram_link2, &p_ram);
  345. /*
  346. * When 1st started, ram -> iram dma channel will fill the
  347. * entire iram. Then, whenever a ping/pong asp buffer finishes,
  348. * 1/2 iram will be filled.
  349. */
  350. prtd->ram_params.link_bcntrld =
  351. EDMA_CHAN_SLOT(prtd->ram_link2) << 5;
  352. }
  353. return 0;
  354. }
  355. /* 1 asp tx or rx channel using 2 parameter channels
  356. * 1 ram to/from iram channel using 1 parameter channel
  357. *
  358. * Playback
  359. * ram copy channel kicks off first,
  360. * 1st ram copy of entire iram buffer completion kicks off asp channel
  361. * asp tcc always kicks off ram copy of 1/2 iram buffer
  362. *
  363. * Record
  364. * asp channel starts, tcc kicks off ram copy
  365. */
  366. static int request_ping_pong(struct snd_pcm_substream *substream,
  367. struct davinci_runtime_data *prtd,
  368. struct snd_dma_buffer *iram_dma)
  369. {
  370. dma_addr_t asp_src_ping;
  371. dma_addr_t asp_dst_ping;
  372. int link;
  373. struct davinci_pcm_dma_params *params = prtd->params;
  374. /* Request ram master channel */
  375. link = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY,
  376. davinci_pcm_dma_irq, substream,
  377. prtd->params->ram_chan_q);
  378. if (link < 0)
  379. goto exit1;
  380. /* Request ram link channel */
  381. link = prtd->ram_link = edma_alloc_slot(
  382. EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
  383. if (link < 0)
  384. goto exit2;
  385. link = prtd->asp_link[1] = edma_alloc_slot(
  386. EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
  387. if (link < 0)
  388. goto exit3;
  389. prtd->ram_link2 = -1;
  390. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  391. link = prtd->ram_link2 = edma_alloc_slot(
  392. EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
  393. if (link < 0)
  394. goto exit4;
  395. }
  396. /* circle ping-pong buffers */
  397. edma_link(prtd->asp_link[0], prtd->asp_link[1]);
  398. edma_link(prtd->asp_link[1], prtd->asp_link[0]);
  399. /* circle ram buffers */
  400. edma_link(prtd->ram_link, prtd->ram_link);
  401. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  402. asp_src_ping = iram_dma->addr;
  403. asp_dst_ping = params->dma_addr; /* fifo */
  404. } else {
  405. asp_src_ping = params->dma_addr; /* fifo */
  406. asp_dst_ping = iram_dma->addr;
  407. }
  408. /* ping */
  409. link = prtd->asp_link[0];
  410. edma_set_src(link, asp_src_ping, INCR, W16BIT);
  411. edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
  412. edma_set_src_index(link, 0, 0);
  413. edma_set_dest_index(link, 0, 0);
  414. edma_read_slot(link, &prtd->asp_params);
  415. prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN);
  416. prtd->asp_params.opt |= TCCHEN |
  417. EDMA_TCC(prtd->ram_channel & 0x3f);
  418. edma_write_slot(link, &prtd->asp_params);
  419. /* pong */
  420. link = prtd->asp_link[1];
  421. edma_set_src(link, asp_src_ping, INCR, W16BIT);
  422. edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
  423. edma_set_src_index(link, 0, 0);
  424. edma_set_dest_index(link, 0, 0);
  425. edma_read_slot(link, &prtd->asp_params);
  426. prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
  427. /* interrupt after every pong completion */
  428. prtd->asp_params.opt |= TCINTEN | TCCHEN |
  429. EDMA_TCC(prtd->ram_channel & 0x3f);
  430. edma_write_slot(link, &prtd->asp_params);
  431. /* ram */
  432. link = prtd->ram_link;
  433. edma_set_src(link, iram_dma->addr, INCR, W32BIT);
  434. edma_set_dest(link, iram_dma->addr, INCR, W32BIT);
  435. pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
  436. "for asp:%u %u %u\n", __func__,
  437. prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
  438. prtd->asp_channel, prtd->asp_link[0],
  439. prtd->asp_link[1]);
  440. return 0;
  441. exit4:
  442. edma_free_channel(prtd->asp_link[1]);
  443. prtd->asp_link[1] = -1;
  444. exit3:
  445. edma_free_channel(prtd->ram_link);
  446. prtd->ram_link = -1;
  447. exit2:
  448. edma_free_channel(prtd->ram_channel);
  449. prtd->ram_channel = -1;
  450. exit1:
  451. return link;
  452. }
  453. static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
  454. {
  455. struct snd_dma_buffer *iram_dma;
  456. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  457. struct davinci_pcm_dma_params *params = prtd->params;
  458. int link;
  459. if (!params)
  460. return -ENODEV;
  461. /* Request asp master DMA channel */
  462. link = prtd->asp_channel = edma_alloc_channel(params->channel,
  463. davinci_pcm_dma_irq, substream,
  464. prtd->params->asp_chan_q);
  465. if (link < 0)
  466. goto exit1;
  467. /* Request asp link channels */
  468. link = prtd->asp_link[0] = edma_alloc_slot(
  469. EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
  470. if (link < 0)
  471. goto exit2;
  472. iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
  473. if (iram_dma) {
  474. if (request_ping_pong(substream, prtd, iram_dma) == 0)
  475. return 0;
  476. printk(KERN_WARNING "%s: dma channel allocation failed,"
  477. "not using sram\n", __func__);
  478. }
  479. /* Issue transfer completion IRQ when the channel completes a
  480. * transfer, then always reload from the same slot (by a kind
  481. * of loopback link). The completion IRQ handler will update
  482. * the reload slot with a new buffer.
  483. *
  484. * REVISIT save p_ram here after setting up everything except
  485. * the buffer and its length (ccnt) ... use it as a template
  486. * so davinci_pcm_enqueue_dma() takes less time in IRQ.
  487. */
  488. edma_read_slot(link, &prtd->asp_params);
  489. prtd->asp_params.opt |= TCINTEN |
  490. EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
  491. prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5;
  492. edma_write_slot(link, &prtd->asp_params);
  493. return 0;
  494. exit2:
  495. edma_free_channel(prtd->asp_channel);
  496. prtd->asp_channel = -1;
  497. exit1:
  498. return link;
  499. }
  500. static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  501. {
  502. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  503. int ret = 0;
  504. spin_lock(&prtd->lock);
  505. switch (cmd) {
  506. case SNDRV_PCM_TRIGGER_START:
  507. edma_start(prtd->asp_channel);
  508. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  509. prtd->ram_channel >= 0) {
  510. /* copy 1st iram buffer */
  511. edma_start(prtd->ram_channel);
  512. }
  513. break;
  514. case SNDRV_PCM_TRIGGER_RESUME:
  515. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  516. edma_resume(prtd->asp_channel);
  517. break;
  518. case SNDRV_PCM_TRIGGER_STOP:
  519. case SNDRV_PCM_TRIGGER_SUSPEND:
  520. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  521. edma_pause(prtd->asp_channel);
  522. break;
  523. default:
  524. ret = -EINVAL;
  525. break;
  526. }
  527. spin_unlock(&prtd->lock);
  528. return ret;
  529. }
  530. static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
  531. {
  532. struct davinci_runtime_data *prtd = substream->runtime->private_data;
  533. davinci_pcm_period_reset(substream);
  534. if (prtd->ram_channel >= 0) {
  535. int ret = ping_pong_dma_setup(substream);
  536. if (ret < 0)
  537. return ret;
  538. edma_write_slot(prtd->ram_channel, &prtd->ram_params);
  539. edma_write_slot(prtd->asp_channel, &prtd->asp_params);
  540. print_buf_info(prtd->ram_channel, "ram_channel");
  541. print_buf_info(prtd->ram_link, "ram_link");
  542. print_buf_info(prtd->ram_link2, "ram_link2");
  543. print_buf_info(prtd->asp_channel, "asp_channel");
  544. print_buf_info(prtd->asp_link[0], "asp_link[0]");
  545. print_buf_info(prtd->asp_link[1], "asp_link[1]");
  546. /*
  547. * There is a phase offset of 2 periods between the position
  548. * used by dma setup and the position reported in the pointer
  549. * function.
  550. *
  551. * The phase offset, when not using ping-pong buffers, is due to
  552. * the two consecutive calls to davinci_pcm_enqueue_dma() below.
  553. *
  554. * Whereas here, with ping-pong buffers, the phase is due to
  555. * there being an entire buffer transfer complete before the
  556. * first dma completion event triggers davinci_pcm_dma_irq().
  557. */
  558. davinci_pcm_period_elapsed(substream);
  559. davinci_pcm_period_elapsed(substream);
  560. return 0;
  561. }
  562. davinci_pcm_enqueue_dma(substream);
  563. davinci_pcm_period_elapsed(substream);
  564. /* Copy self-linked parameter RAM entry into master channel */
  565. edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
  566. edma_write_slot(prtd->asp_channel, &prtd->asp_params);
  567. davinci_pcm_enqueue_dma(substream);
  568. davinci_pcm_period_elapsed(substream);
  569. return 0;
  570. }
  571. static snd_pcm_uframes_t
  572. davinci_pcm_pointer(struct snd_pcm_substream *substream)
  573. {
  574. struct snd_pcm_runtime *runtime = substream->runtime;
  575. struct davinci_runtime_data *prtd = runtime->private_data;
  576. unsigned int offset;
  577. int asp_count;
  578. unsigned int period_size = snd_pcm_lib_period_bytes(substream);
  579. /*
  580. * There is a phase offset of 2 periods between the position used by dma
  581. * setup and the position reported in the pointer function. Either +2 in
  582. * the dma setup or -2 here in the pointer function (with wrapping,
  583. * both) accounts for this offset -- choose the latter since it makes
  584. * the first-time setup clearer.
  585. */
  586. spin_lock(&prtd->lock);
  587. asp_count = prtd->period - 2;
  588. spin_unlock(&prtd->lock);
  589. if (asp_count < 0)
  590. asp_count += runtime->periods;
  591. asp_count *= period_size;
  592. offset = bytes_to_frames(runtime, asp_count);
  593. if (offset >= runtime->buffer_size)
  594. offset = 0;
  595. return offset;
  596. }
  597. static int davinci_pcm_open(struct snd_pcm_substream *substream)
  598. {
  599. struct snd_pcm_runtime *runtime = substream->runtime;
  600. struct davinci_runtime_data *prtd;
  601. struct snd_pcm_hardware *ppcm;
  602. int ret = 0;
  603. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  604. struct davinci_pcm_dma_params *pa;
  605. struct davinci_pcm_dma_params *params;
  606. pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  607. if (!pa)
  608. return -ENODEV;
  609. params = &pa[substream->stream];
  610. ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  611. &pcm_hardware_playback : &pcm_hardware_capture;
  612. allocate_sram(substream, params->sram_size, ppcm);
  613. snd_soc_set_runtime_hwparams(substream, ppcm);
  614. /* ensure that buffer size is a multiple of period size */
  615. ret = snd_pcm_hw_constraint_integer(runtime,
  616. SNDRV_PCM_HW_PARAM_PERIODS);
  617. if (ret < 0)
  618. return ret;
  619. prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
  620. if (prtd == NULL)
  621. return -ENOMEM;
  622. spin_lock_init(&prtd->lock);
  623. prtd->params = params;
  624. prtd->asp_channel = -1;
  625. prtd->asp_link[0] = prtd->asp_link[1] = -1;
  626. prtd->ram_channel = -1;
  627. prtd->ram_link = -1;
  628. prtd->ram_link2 = -1;
  629. runtime->private_data = prtd;
  630. ret = davinci_pcm_dma_request(substream);
  631. if (ret) {
  632. printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
  633. kfree(prtd);
  634. }
  635. return ret;
  636. }
  637. static int davinci_pcm_close(struct snd_pcm_substream *substream)
  638. {
  639. struct snd_pcm_runtime *runtime = substream->runtime;
  640. struct davinci_runtime_data *prtd = runtime->private_data;
  641. if (prtd->ram_channel >= 0)
  642. edma_stop(prtd->ram_channel);
  643. if (prtd->asp_channel >= 0)
  644. edma_stop(prtd->asp_channel);
  645. if (prtd->asp_link[0] >= 0)
  646. edma_unlink(prtd->asp_link[0]);
  647. if (prtd->asp_link[1] >= 0)
  648. edma_unlink(prtd->asp_link[1]);
  649. if (prtd->ram_link >= 0)
  650. edma_unlink(prtd->ram_link);
  651. if (prtd->asp_link[0] >= 0)
  652. edma_free_slot(prtd->asp_link[0]);
  653. if (prtd->asp_link[1] >= 0)
  654. edma_free_slot(prtd->asp_link[1]);
  655. if (prtd->asp_channel >= 0)
  656. edma_free_channel(prtd->asp_channel);
  657. if (prtd->ram_link >= 0)
  658. edma_free_slot(prtd->ram_link);
  659. if (prtd->ram_link2 >= 0)
  660. edma_free_slot(prtd->ram_link2);
  661. if (prtd->ram_channel >= 0)
  662. edma_free_channel(prtd->ram_channel);
  663. kfree(prtd);
  664. return 0;
  665. }
  666. static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
  667. struct snd_pcm_hw_params *hw_params)
  668. {
  669. return snd_pcm_lib_malloc_pages(substream,
  670. params_buffer_bytes(hw_params));
  671. }
  672. static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
  673. {
  674. return snd_pcm_lib_free_pages(substream);
  675. }
  676. static int davinci_pcm_mmap(struct snd_pcm_substream *substream,
  677. struct vm_area_struct *vma)
  678. {
  679. struct snd_pcm_runtime *runtime = substream->runtime;
  680. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  681. runtime->dma_area,
  682. runtime->dma_addr,
  683. runtime->dma_bytes);
  684. }
  685. static struct snd_pcm_ops davinci_pcm_ops = {
  686. .open = davinci_pcm_open,
  687. .close = davinci_pcm_close,
  688. .ioctl = snd_pcm_lib_ioctl,
  689. .hw_params = davinci_pcm_hw_params,
  690. .hw_free = davinci_pcm_hw_free,
  691. .prepare = davinci_pcm_prepare,
  692. .trigger = davinci_pcm_trigger,
  693. .pointer = davinci_pcm_pointer,
  694. .mmap = davinci_pcm_mmap,
  695. };
  696. static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
  697. size_t size)
  698. {
  699. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  700. struct snd_dma_buffer *buf = &substream->dma_buffer;
  701. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  702. buf->dev.dev = pcm->card->dev;
  703. buf->private_data = NULL;
  704. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  705. &buf->addr, GFP_KERNEL);
  706. pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
  707. "size=%d\n", (void *) buf->area, (void *) buf->addr, size);
  708. if (!buf->area)
  709. return -ENOMEM;
  710. buf->bytes = size;
  711. return 0;
  712. }
  713. static void davinci_pcm_free(struct snd_pcm *pcm)
  714. {
  715. struct snd_pcm_substream *substream;
  716. struct snd_dma_buffer *buf;
  717. int stream;
  718. for (stream = 0; stream < 2; stream++) {
  719. struct snd_dma_buffer *iram_dma;
  720. substream = pcm->streams[stream].substream;
  721. if (!substream)
  722. continue;
  723. buf = &substream->dma_buffer;
  724. if (!buf->area)
  725. continue;
  726. dma_free_writecombine(pcm->card->dev, buf->bytes,
  727. buf->area, buf->addr);
  728. buf->area = NULL;
  729. iram_dma = buf->private_data;
  730. if (iram_dma) {
  731. sram_free(iram_dma->area, iram_dma->bytes);
  732. kfree(iram_dma);
  733. }
  734. }
  735. }
  736. static u64 davinci_pcm_dmamask = 0xffffffff;
  737. static int davinci_pcm_new(struct snd_soc_pcm_runtime *rtd)
  738. {
  739. struct snd_card *card = rtd->card->snd_card;
  740. struct snd_soc_dai *dai = rtd->cpu_dai;
  741. struct snd_pcm *pcm = rtd->pcm;
  742. int ret;
  743. if (!card->dev->dma_mask)
  744. card->dev->dma_mask = &davinci_pcm_dmamask;
  745. if (!card->dev->coherent_dma_mask)
  746. card->dev->coherent_dma_mask = 0xffffffff;
  747. if (dai->driver->playback.channels_min) {
  748. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  749. SNDRV_PCM_STREAM_PLAYBACK,
  750. pcm_hardware_playback.buffer_bytes_max);
  751. if (ret)
  752. return ret;
  753. }
  754. if (dai->driver->capture.channels_min) {
  755. ret = davinci_pcm_preallocate_dma_buffer(pcm,
  756. SNDRV_PCM_STREAM_CAPTURE,
  757. pcm_hardware_capture.buffer_bytes_max);
  758. if (ret)
  759. return ret;
  760. }
  761. return 0;
  762. }
  763. static struct snd_soc_platform_driver davinci_soc_platform = {
  764. .ops = &davinci_pcm_ops,
  765. .pcm_new = davinci_pcm_new,
  766. .pcm_free = davinci_pcm_free,
  767. };
  768. static int __devinit davinci_soc_platform_probe(struct platform_device *pdev)
  769. {
  770. return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform);
  771. }
  772. static int __devexit davinci_soc_platform_remove(struct platform_device *pdev)
  773. {
  774. snd_soc_unregister_platform(&pdev->dev);
  775. return 0;
  776. }
  777. static struct platform_driver davinci_pcm_driver = {
  778. .driver = {
  779. .name = "davinci-pcm-audio",
  780. .owner = THIS_MODULE,
  781. },
  782. .probe = davinci_soc_platform_probe,
  783. .remove = __devexit_p(davinci_soc_platform_remove),
  784. };
  785. static int __init snd_davinci_pcm_init(void)
  786. {
  787. return platform_driver_register(&davinci_pcm_driver);
  788. }
  789. module_init(snd_davinci_pcm_init);
  790. static void __exit snd_davinci_pcm_exit(void)
  791. {
  792. platform_driver_unregister(&davinci_pcm_driver);
  793. }
  794. module_exit(snd_davinci_pcm_exit);
  795. MODULE_AUTHOR("Vladimir Barinov");
  796. MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
  797. MODULE_LICENSE("GPL");