davinci-pcm.c 24 KB

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