davinci-pcm.c 25 KB

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