cs5535audio_pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Driver for audio on multifunction CS5535 companion device
  3. * Copyright (C) Jaya Kumar
  4. *
  5. * Based on Jaroslav Kysela and Takashi Iwai's examples.
  6. * This work was sponsored by CIS(M) Sdn Bhd.
  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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * todo: add be fmt support, spdif, pm
  23. */
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/pci.h>
  27. #include <sound/driver.h>
  28. #include <sound/core.h>
  29. #include <sound/control.h>
  30. #include <sound/initval.h>
  31. #include <sound/asoundef.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/ac97_codec.h>
  35. #include "cs5535audio.h"
  36. static struct snd_pcm_hardware snd_cs5535audio_playback =
  37. {
  38. .info = (
  39. SNDRV_PCM_INFO_MMAP |
  40. SNDRV_PCM_INFO_INTERLEAVED |
  41. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  42. SNDRV_PCM_INFO_MMAP_VALID |
  43. SNDRV_PCM_INFO_PAUSE |
  44. SNDRV_PCM_INFO_SYNC_START |
  45. SNDRV_PCM_INFO_RESUME
  46. ),
  47. .formats = (
  48. SNDRV_PCM_FMTBIT_S16_LE
  49. ),
  50. .rates = (
  51. SNDRV_PCM_RATE_CONTINUOUS |
  52. SNDRV_PCM_RATE_8000_48000
  53. ),
  54. .rate_min = 4000,
  55. .rate_max = 48000,
  56. .channels_min = 2,
  57. .channels_max = 2,
  58. .buffer_bytes_max = (128*1024),
  59. .period_bytes_min = 64,
  60. .period_bytes_max = (64*1024 - 16),
  61. .periods_min = 1,
  62. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  63. .fifo_size = 0,
  64. };
  65. static struct snd_pcm_hardware snd_cs5535audio_capture =
  66. {
  67. .info = (
  68. SNDRV_PCM_INFO_MMAP |
  69. SNDRV_PCM_INFO_INTERLEAVED |
  70. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  71. SNDRV_PCM_INFO_MMAP_VALID |
  72. SNDRV_PCM_INFO_SYNC_START
  73. ),
  74. .formats = (
  75. SNDRV_PCM_FMTBIT_S16_LE
  76. ),
  77. .rates = (
  78. SNDRV_PCM_RATE_CONTINUOUS |
  79. SNDRV_PCM_RATE_8000_48000
  80. ),
  81. .rate_min = 4000,
  82. .rate_max = 48000,
  83. .channels_min = 2,
  84. .channels_max = 2,
  85. .buffer_bytes_max = (128*1024),
  86. .period_bytes_min = 64,
  87. .period_bytes_max = (64*1024 - 16),
  88. .periods_min = 1,
  89. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  90. .fifo_size = 0,
  91. };
  92. static int snd_cs5535audio_playback_open(struct snd_pcm_substream *substream)
  93. {
  94. int err;
  95. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  96. struct snd_pcm_runtime *runtime = substream->runtime;
  97. runtime->hw = snd_cs5535audio_playback;
  98. cs5535au->playback_substream = substream;
  99. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK]);
  100. snd_pcm_set_sync(substream);
  101. if ((err = snd_pcm_hw_constraint_integer(runtime,
  102. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  103. return err;
  104. return 0;
  105. }
  106. static int snd_cs5535audio_playback_close(struct snd_pcm_substream *substream)
  107. {
  108. return 0;
  109. }
  110. #define CS5535AUDIO_DESC_LIST_SIZE \
  111. PAGE_ALIGN(CS5535AUDIO_MAX_DESCRIPTORS * sizeof(struct cs5535audio_dma_desc))
  112. static int cs5535audio_build_dma_packets(struct cs5535audio *cs5535au,
  113. struct cs5535audio_dma *dma,
  114. struct snd_pcm_substream *substream,
  115. unsigned int periods,
  116. unsigned int period_bytes)
  117. {
  118. unsigned int i;
  119. u32 addr, desc_addr, jmpprd_addr;
  120. struct cs5535audio_dma_desc *lastdesc;
  121. if (periods > CS5535AUDIO_MAX_DESCRIPTORS)
  122. return -ENOMEM;
  123. if (dma->desc_buf.area == NULL) {
  124. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  125. snd_dma_pci_data(cs5535au->pci),
  126. CS5535AUDIO_DESC_LIST_SIZE+1,
  127. &dma->desc_buf) < 0)
  128. return -ENOMEM;
  129. dma->period_bytes = dma->periods = 0;
  130. }
  131. if (dma->periods == periods && dma->period_bytes == period_bytes)
  132. return 0;
  133. /* the u32 cast is okay because in snd*create we successfully told
  134. pci alloc that we're only 32 bit capable so the uppper will be 0 */
  135. addr = (u32) substream->runtime->dma_addr;
  136. desc_addr = (u32) dma->desc_buf.addr;
  137. for (i = 0; i < periods; i++) {
  138. struct cs5535audio_dma_desc *desc =
  139. &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[i];
  140. desc->addr = cpu_to_le32(addr);
  141. desc->size = cpu_to_le32(period_bytes);
  142. desc->ctlreserved = cpu_to_le32(PRD_EOP);
  143. desc_addr += sizeof(struct cs5535audio_dma_desc);
  144. addr += period_bytes;
  145. }
  146. /* we reserved one dummy descriptor at the end to do the PRD jump */
  147. lastdesc = &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[periods];
  148. lastdesc->addr = cpu_to_le32((u32) dma->desc_buf.addr);
  149. lastdesc->size = 0;
  150. lastdesc->ctlreserved = cpu_to_le32(PRD_JMP);
  151. jmpprd_addr = cpu_to_le32(lastdesc->addr +
  152. (sizeof(struct cs5535audio_dma_desc)*periods));
  153. dma->period_bytes = period_bytes;
  154. dma->periods = periods;
  155. spin_lock_irq(&cs5535au->reg_lock);
  156. dma->ops->disable_dma(cs5535au);
  157. dma->ops->setup_prd(cs5535au, jmpprd_addr);
  158. spin_unlock_irq(&cs5535au->reg_lock);
  159. return 0;
  160. }
  161. static void cs5535audio_playback_enable_dma(struct cs5535audio *cs5535au)
  162. {
  163. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_EN);
  164. }
  165. static void cs5535audio_playback_disable_dma(struct cs5535audio *cs5535au)
  166. {
  167. cs_writeb(cs5535au, ACC_BM0_CMD, 0);
  168. }
  169. static void cs5535audio_playback_pause_dma(struct cs5535audio *cs5535au)
  170. {
  171. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_PAUSE);
  172. }
  173. static void cs5535audio_playback_setup_prd(struct cs5535audio *cs5535au,
  174. u32 prd_addr)
  175. {
  176. cs_writel(cs5535au, ACC_BM0_PRD, prd_addr);
  177. }
  178. static u32 cs5535audio_playback_read_prd(struct cs5535audio *cs5535au)
  179. {
  180. return cs_readl(cs5535au, ACC_BM0_PRD);
  181. }
  182. static u32 cs5535audio_playback_read_dma_pntr(struct cs5535audio *cs5535au)
  183. {
  184. return cs_readl(cs5535au, ACC_BM0_PNTR);
  185. }
  186. static void cs5535audio_capture_enable_dma(struct cs5535audio *cs5535au)
  187. {
  188. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_EN);
  189. }
  190. static void cs5535audio_capture_disable_dma(struct cs5535audio *cs5535au)
  191. {
  192. cs_writeb(cs5535au, ACC_BM1_CMD, 0);
  193. }
  194. static void cs5535audio_capture_pause_dma(struct cs5535audio *cs5535au)
  195. {
  196. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_PAUSE);
  197. }
  198. static void cs5535audio_capture_setup_prd(struct cs5535audio *cs5535au,
  199. u32 prd_addr)
  200. {
  201. cs_writel(cs5535au, ACC_BM1_PRD, prd_addr);
  202. }
  203. static u32 cs5535audio_capture_read_prd(struct cs5535audio *cs5535au)
  204. {
  205. return cs_readl(cs5535au, ACC_BM1_PRD);
  206. }
  207. static u32 cs5535audio_capture_read_dma_pntr(struct cs5535audio *cs5535au)
  208. {
  209. return cs_readl(cs5535au, ACC_BM1_PNTR);
  210. }
  211. static void cs5535audio_clear_dma_packets(struct cs5535audio *cs5535au,
  212. struct cs5535audio_dma *dma,
  213. struct snd_pcm_substream *substream)
  214. {
  215. snd_dma_free_pages(&dma->desc_buf);
  216. dma->desc_buf.area = NULL;
  217. }
  218. static int snd_cs5535audio_hw_params(struct snd_pcm_substream *substream,
  219. struct snd_pcm_hw_params *hw_params)
  220. {
  221. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  222. struct cs5535audio_dma *dma = substream->runtime->private_data;
  223. int err;
  224. err = snd_pcm_lib_malloc_pages(substream,
  225. params_buffer_bytes(hw_params));
  226. if (err < 0)
  227. return err;
  228. dma->buf_addr = substream->runtime->dma_addr;
  229. dma->buf_bytes = params_buffer_bytes(hw_params);
  230. err = cs5535audio_build_dma_packets(cs5535au, dma, substream,
  231. params_periods(hw_params),
  232. params_period_bytes(hw_params));
  233. return err;
  234. }
  235. static int snd_cs5535audio_hw_free(struct snd_pcm_substream *substream)
  236. {
  237. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  238. struct cs5535audio_dma *dma = substream->runtime->private_data;
  239. cs5535audio_clear_dma_packets(cs5535au, dma, substream);
  240. return snd_pcm_lib_free_pages(substream);
  241. }
  242. static int snd_cs5535audio_playback_prepare(struct snd_pcm_substream *substream)
  243. {
  244. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  245. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_FRONT_DAC_RATE,
  246. substream->runtime->rate);
  247. }
  248. static int snd_cs5535audio_trigger(struct snd_pcm_substream *substream, int cmd)
  249. {
  250. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  251. struct cs5535audio_dma *dma = substream->runtime->private_data;
  252. int err = 0;
  253. spin_lock(&cs5535au->reg_lock);
  254. switch (cmd) {
  255. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  256. dma->ops->pause_dma(cs5535au);
  257. break;
  258. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  259. dma->ops->enable_dma(cs5535au);
  260. break;
  261. case SNDRV_PCM_TRIGGER_START:
  262. dma->ops->enable_dma(cs5535au);
  263. break;
  264. case SNDRV_PCM_TRIGGER_RESUME:
  265. dma->ops->enable_dma(cs5535au);
  266. dma->suspended = 0;
  267. break;
  268. case SNDRV_PCM_TRIGGER_STOP:
  269. dma->ops->disable_dma(cs5535au);
  270. break;
  271. case SNDRV_PCM_TRIGGER_SUSPEND:
  272. dma->ops->disable_dma(cs5535au);
  273. dma->suspended = 1;
  274. break;
  275. default:
  276. snd_printk(KERN_ERR "unhandled trigger\n");
  277. err = -EINVAL;
  278. break;
  279. }
  280. spin_unlock(&cs5535au->reg_lock);
  281. return err;
  282. }
  283. static snd_pcm_uframes_t snd_cs5535audio_pcm_pointer(struct snd_pcm_substream
  284. *substream)
  285. {
  286. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  287. u32 curdma;
  288. struct cs5535audio_dma *dma;
  289. dma = substream->runtime->private_data;
  290. curdma = dma->ops->read_dma_pntr(cs5535au);
  291. if (curdma < dma->buf_addr) {
  292. snd_printk(KERN_ERR "curdma=%x < %x bufaddr.\n",
  293. curdma, dma->buf_addr);
  294. return 0;
  295. }
  296. curdma -= dma->buf_addr;
  297. if (curdma >= dma->buf_bytes) {
  298. snd_printk(KERN_ERR "diff=%x >= %x buf_bytes.\n",
  299. curdma, dma->buf_bytes);
  300. return 0;
  301. }
  302. return bytes_to_frames(substream->runtime, curdma);
  303. }
  304. static int snd_cs5535audio_capture_open(struct snd_pcm_substream *substream)
  305. {
  306. int err;
  307. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  308. struct snd_pcm_runtime *runtime = substream->runtime;
  309. runtime->hw = snd_cs5535audio_capture;
  310. cs5535au->capture_substream = substream;
  311. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE]);
  312. snd_pcm_set_sync(substream);
  313. if ((err = snd_pcm_hw_constraint_integer(runtime,
  314. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  315. return err;
  316. return 0;
  317. }
  318. static int snd_cs5535audio_capture_close(struct snd_pcm_substream *substream)
  319. {
  320. return 0;
  321. }
  322. static int snd_cs5535audio_capture_prepare(struct snd_pcm_substream *substream)
  323. {
  324. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  325. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_LR_ADC_RATE,
  326. substream->runtime->rate);
  327. }
  328. static struct snd_pcm_ops snd_cs5535audio_playback_ops = {
  329. .open = snd_cs5535audio_playback_open,
  330. .close = snd_cs5535audio_playback_close,
  331. .ioctl = snd_pcm_lib_ioctl,
  332. .hw_params = snd_cs5535audio_hw_params,
  333. .hw_free = snd_cs5535audio_hw_free,
  334. .prepare = snd_cs5535audio_playback_prepare,
  335. .trigger = snd_cs5535audio_trigger,
  336. .pointer = snd_cs5535audio_pcm_pointer,
  337. };
  338. static struct snd_pcm_ops snd_cs5535audio_capture_ops = {
  339. .open = snd_cs5535audio_capture_open,
  340. .close = snd_cs5535audio_capture_close,
  341. .ioctl = snd_pcm_lib_ioctl,
  342. .hw_params = snd_cs5535audio_hw_params,
  343. .hw_free = snd_cs5535audio_hw_free,
  344. .prepare = snd_cs5535audio_capture_prepare,
  345. .trigger = snd_cs5535audio_trigger,
  346. .pointer = snd_cs5535audio_pcm_pointer,
  347. };
  348. static struct cs5535audio_dma_ops snd_cs5535audio_playback_dma_ops = {
  349. .type = CS5535AUDIO_DMA_PLAYBACK,
  350. .enable_dma = cs5535audio_playback_enable_dma,
  351. .disable_dma = cs5535audio_playback_disable_dma,
  352. .setup_prd = cs5535audio_playback_setup_prd,
  353. .read_prd = cs5535audio_playback_read_prd,
  354. .pause_dma = cs5535audio_playback_pause_dma,
  355. .read_dma_pntr = cs5535audio_playback_read_dma_pntr,
  356. };
  357. static struct cs5535audio_dma_ops snd_cs5535audio_capture_dma_ops = {
  358. .type = CS5535AUDIO_DMA_CAPTURE,
  359. .enable_dma = cs5535audio_capture_enable_dma,
  360. .disable_dma = cs5535audio_capture_disable_dma,
  361. .setup_prd = cs5535audio_capture_setup_prd,
  362. .read_prd = cs5535audio_capture_read_prd,
  363. .pause_dma = cs5535audio_capture_pause_dma,
  364. .read_dma_pntr = cs5535audio_capture_read_dma_pntr,
  365. };
  366. int __devinit snd_cs5535audio_pcm(struct cs5535audio *cs5535au)
  367. {
  368. struct snd_pcm *pcm;
  369. int err;
  370. err = snd_pcm_new(cs5535au->card, "CS5535 Audio", 0, 1, 1, &pcm);
  371. if (err < 0)
  372. return err;
  373. cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK].ops =
  374. &snd_cs5535audio_playback_dma_ops;
  375. cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE].ops =
  376. &snd_cs5535audio_capture_dma_ops;
  377. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  378. &snd_cs5535audio_playback_ops);
  379. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  380. &snd_cs5535audio_capture_ops);
  381. pcm->private_data = cs5535au;
  382. pcm->info_flags = 0;
  383. strcpy(pcm->name, "CS5535 Audio");
  384. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  385. snd_dma_pci_data(cs5535au->pci),
  386. 64*1024, 128*1024);
  387. cs5535au->pcm = pcm;
  388. return 0;
  389. }