cs5535audio_pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. ),
  46. .formats = (
  47. SNDRV_PCM_FMTBIT_S16_LE
  48. ),
  49. .rates = (
  50. SNDRV_PCM_RATE_CONTINUOUS |
  51. SNDRV_PCM_RATE_8000_48000
  52. ),
  53. .rate_min = 4000,
  54. .rate_max = 48000,
  55. .channels_min = 2,
  56. .channels_max = 2,
  57. .buffer_bytes_max = (128*1024),
  58. .period_bytes_min = 64,
  59. .period_bytes_max = (64*1024 - 16),
  60. .periods_min = 1,
  61. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  62. .fifo_size = 0,
  63. };
  64. static struct snd_pcm_hardware snd_cs5535audio_capture =
  65. {
  66. .info = (
  67. SNDRV_PCM_INFO_MMAP |
  68. SNDRV_PCM_INFO_INTERLEAVED |
  69. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  70. SNDRV_PCM_INFO_MMAP_VALID |
  71. SNDRV_PCM_INFO_SYNC_START
  72. ),
  73. .formats = (
  74. SNDRV_PCM_FMTBIT_S16_LE
  75. ),
  76. .rates = (
  77. SNDRV_PCM_RATE_CONTINUOUS |
  78. SNDRV_PCM_RATE_8000_48000
  79. ),
  80. .rate_min = 4000,
  81. .rate_max = 48000,
  82. .channels_min = 2,
  83. .channels_max = 2,
  84. .buffer_bytes_max = (128*1024),
  85. .period_bytes_min = 64,
  86. .period_bytes_max = (64*1024 - 16),
  87. .periods_min = 1,
  88. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  89. .fifo_size = 0,
  90. };
  91. static int snd_cs5535audio_playback_open(struct snd_pcm_substream *substream)
  92. {
  93. int err;
  94. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  95. struct snd_pcm_runtime *runtime = substream->runtime;
  96. runtime->hw = snd_cs5535audio_playback;
  97. cs5535au->playback_substream = substream;
  98. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK]);
  99. snd_pcm_set_sync(substream);
  100. if ((err = snd_pcm_hw_constraint_integer(runtime,
  101. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  102. return err;
  103. return 0;
  104. }
  105. static int snd_cs5535audio_playback_close(struct snd_pcm_substream *substream)
  106. {
  107. return 0;
  108. }
  109. #define CS5535AUDIO_DESC_LIST_SIZE \
  110. PAGE_ALIGN(CS5535AUDIO_MAX_DESCRIPTORS * sizeof(struct cs5535audio_dma_desc))
  111. static int cs5535audio_build_dma_packets(struct cs5535audio *cs5535au,
  112. struct cs5535audio_dma *dma,
  113. struct snd_pcm_substream *substream,
  114. unsigned int periods,
  115. unsigned int period_bytes)
  116. {
  117. unsigned int i;
  118. u32 addr, desc_addr, jmpprd_addr;
  119. struct cs5535audio_dma_desc *lastdesc;
  120. if (periods > CS5535AUDIO_MAX_DESCRIPTORS)
  121. return -ENOMEM;
  122. if (dma->desc_buf.area == NULL) {
  123. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  124. snd_dma_pci_data(cs5535au->pci),
  125. CS5535AUDIO_DESC_LIST_SIZE+1,
  126. &dma->desc_buf) < 0)
  127. return -ENOMEM;
  128. dma->period_bytes = dma->periods = 0;
  129. }
  130. if (dma->periods == periods && dma->period_bytes == period_bytes)
  131. return 0;
  132. /* the u32 cast is okay because in snd*create we succesfully told
  133. pci alloc that we're only 32 bit capable so the uppper will be 0 */
  134. addr = (u32) substream->runtime->dma_addr;
  135. desc_addr = (u32) dma->desc_buf.addr;
  136. for (i = 0; i < periods; i++) {
  137. struct cs5535audio_dma_desc *desc =
  138. &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[i];
  139. desc->addr = cpu_to_le32(addr);
  140. desc->size = cpu_to_le32(period_bytes);
  141. desc->ctlreserved = cpu_to_le32(PRD_EOP);
  142. desc_addr += sizeof(struct cs5535audio_dma_desc);
  143. addr += period_bytes;
  144. }
  145. /* we reserved one dummy descriptor at the end to do the PRD jump */
  146. lastdesc = &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[periods];
  147. lastdesc->addr = cpu_to_le32((u32) dma->desc_buf.addr);
  148. lastdesc->size = 0;
  149. lastdesc->ctlreserved = cpu_to_le32(PRD_JMP);
  150. jmpprd_addr = cpu_to_le32(lastdesc->addr +
  151. (sizeof(struct cs5535audio_dma_desc)*periods));
  152. dma->period_bytes = period_bytes;
  153. dma->periods = periods;
  154. spin_lock_irq(&cs5535au->reg_lock);
  155. dma->ops->disable_dma(cs5535au);
  156. dma->ops->setup_prd(cs5535au, jmpprd_addr);
  157. spin_unlock_irq(&cs5535au->reg_lock);
  158. return 0;
  159. }
  160. static void cs5535audio_playback_enable_dma(struct cs5535audio *cs5535au)
  161. {
  162. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_EN);
  163. }
  164. static void cs5535audio_playback_disable_dma(struct cs5535audio *cs5535au)
  165. {
  166. cs_writeb(cs5535au, ACC_BM0_CMD, 0);
  167. }
  168. static void cs5535audio_playback_pause_dma(struct cs5535audio *cs5535au)
  169. {
  170. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_PAUSE);
  171. }
  172. static void cs5535audio_playback_setup_prd(struct cs5535audio *cs5535au,
  173. u32 prd_addr)
  174. {
  175. cs_writel(cs5535au, ACC_BM0_PRD, prd_addr);
  176. }
  177. static u32 cs5535audio_playback_read_dma_pntr(struct cs5535audio *cs5535au)
  178. {
  179. return cs_readl(cs5535au, ACC_BM0_PNTR);
  180. }
  181. static void cs5535audio_capture_enable_dma(struct cs5535audio *cs5535au)
  182. {
  183. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_EN);
  184. }
  185. static void cs5535audio_capture_disable_dma(struct cs5535audio *cs5535au)
  186. {
  187. cs_writeb(cs5535au, ACC_BM1_CMD, 0);
  188. }
  189. static void cs5535audio_capture_pause_dma(struct cs5535audio *cs5535au)
  190. {
  191. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_PAUSE);
  192. }
  193. static void cs5535audio_capture_setup_prd(struct cs5535audio *cs5535au,
  194. u32 prd_addr)
  195. {
  196. cs_writel(cs5535au, ACC_BM1_PRD, prd_addr);
  197. }
  198. static u32 cs5535audio_capture_read_dma_pntr(struct cs5535audio *cs5535au)
  199. {
  200. return cs_readl(cs5535au, ACC_BM1_PNTR);
  201. }
  202. static void cs5535audio_clear_dma_packets(struct cs5535audio *cs5535au,
  203. struct cs5535audio_dma *dma,
  204. struct snd_pcm_substream *substream)
  205. {
  206. snd_dma_free_pages(&dma->desc_buf);
  207. dma->desc_buf.area = NULL;
  208. }
  209. static int snd_cs5535audio_hw_params(struct snd_pcm_substream *substream,
  210. struct snd_pcm_hw_params *hw_params)
  211. {
  212. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  213. struct cs5535audio_dma *dma = substream->runtime->private_data;
  214. int err;
  215. err = snd_pcm_lib_malloc_pages(substream,
  216. params_buffer_bytes(hw_params));
  217. if (err < 0)
  218. return err;
  219. dma->buf_addr = substream->runtime->dma_addr;
  220. dma->buf_bytes = params_buffer_bytes(hw_params);
  221. err = cs5535audio_build_dma_packets(cs5535au, dma, substream,
  222. params_periods(hw_params),
  223. params_period_bytes(hw_params));
  224. return err;
  225. }
  226. static int snd_cs5535audio_hw_free(struct snd_pcm_substream *substream)
  227. {
  228. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  229. struct cs5535audio_dma *dma = substream->runtime->private_data;
  230. cs5535audio_clear_dma_packets(cs5535au, dma, substream);
  231. return snd_pcm_lib_free_pages(substream);
  232. }
  233. static int snd_cs5535audio_playback_prepare(struct snd_pcm_substream *substream)
  234. {
  235. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  236. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_FRONT_DAC_RATE,
  237. substream->runtime->rate);
  238. }
  239. static int snd_cs5535audio_trigger(struct snd_pcm_substream *substream, int cmd)
  240. {
  241. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  242. struct cs5535audio_dma *dma = substream->runtime->private_data;
  243. int err = 0;
  244. spin_lock(&cs5535au->reg_lock);
  245. switch (cmd) {
  246. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  247. dma->ops->pause_dma(cs5535au);
  248. break;
  249. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  250. dma->ops->enable_dma(cs5535au);
  251. break;
  252. case SNDRV_PCM_TRIGGER_START:
  253. dma->ops->enable_dma(cs5535au);
  254. break;
  255. case SNDRV_PCM_TRIGGER_STOP:
  256. dma->ops->disable_dma(cs5535au);
  257. break;
  258. default:
  259. snd_printk(KERN_ERR "unhandled trigger\n");
  260. err = -EINVAL;
  261. break;
  262. }
  263. spin_unlock(&cs5535au->reg_lock);
  264. return err;
  265. }
  266. static snd_pcm_uframes_t snd_cs5535audio_pcm_pointer(struct snd_pcm_substream
  267. *substream)
  268. {
  269. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  270. u32 curdma;
  271. struct cs5535audio_dma *dma;
  272. dma = substream->runtime->private_data;
  273. curdma = dma->ops->read_dma_pntr(cs5535au);
  274. if (curdma < dma->buf_addr) {
  275. snd_printk(KERN_ERR "curdma=%x < %x bufaddr.\n",
  276. curdma, dma->buf_addr);
  277. return 0;
  278. }
  279. curdma -= dma->buf_addr;
  280. if (curdma >= dma->buf_bytes) {
  281. snd_printk(KERN_ERR "diff=%x >= %x buf_bytes.\n",
  282. curdma, dma->buf_bytes);
  283. return 0;
  284. }
  285. return bytes_to_frames(substream->runtime, curdma);
  286. }
  287. static int snd_cs5535audio_capture_open(struct snd_pcm_substream *substream)
  288. {
  289. int err;
  290. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  291. struct snd_pcm_runtime *runtime = substream->runtime;
  292. runtime->hw = snd_cs5535audio_capture;
  293. cs5535au->capture_substream = substream;
  294. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE]);
  295. snd_pcm_set_sync(substream);
  296. if ((err = snd_pcm_hw_constraint_integer(runtime,
  297. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  298. return err;
  299. return 0;
  300. }
  301. static int snd_cs5535audio_capture_close(struct snd_pcm_substream *substream)
  302. {
  303. return 0;
  304. }
  305. static int snd_cs5535audio_capture_prepare(struct snd_pcm_substream *substream)
  306. {
  307. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  308. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_LR_ADC_RATE,
  309. substream->runtime->rate);
  310. }
  311. static struct snd_pcm_ops snd_cs5535audio_playback_ops = {
  312. .open = snd_cs5535audio_playback_open,
  313. .close = snd_cs5535audio_playback_close,
  314. .ioctl = snd_pcm_lib_ioctl,
  315. .hw_params = snd_cs5535audio_hw_params,
  316. .hw_free = snd_cs5535audio_hw_free,
  317. .prepare = snd_cs5535audio_playback_prepare,
  318. .trigger = snd_cs5535audio_trigger,
  319. .pointer = snd_cs5535audio_pcm_pointer,
  320. };
  321. static struct snd_pcm_ops snd_cs5535audio_capture_ops = {
  322. .open = snd_cs5535audio_capture_open,
  323. .close = snd_cs5535audio_capture_close,
  324. .ioctl = snd_pcm_lib_ioctl,
  325. .hw_params = snd_cs5535audio_hw_params,
  326. .hw_free = snd_cs5535audio_hw_free,
  327. .prepare = snd_cs5535audio_capture_prepare,
  328. .trigger = snd_cs5535audio_trigger,
  329. .pointer = snd_cs5535audio_pcm_pointer,
  330. };
  331. static struct cs5535audio_dma_ops snd_cs5535audio_playback_dma_ops = {
  332. .type = CS5535AUDIO_DMA_PLAYBACK,
  333. .enable_dma = cs5535audio_playback_enable_dma,
  334. .disable_dma = cs5535audio_playback_disable_dma,
  335. .setup_prd = cs5535audio_playback_setup_prd,
  336. .pause_dma = cs5535audio_playback_pause_dma,
  337. .read_dma_pntr = cs5535audio_playback_read_dma_pntr,
  338. };
  339. static struct cs5535audio_dma_ops snd_cs5535audio_capture_dma_ops = {
  340. .type = CS5535AUDIO_DMA_CAPTURE,
  341. .enable_dma = cs5535audio_capture_enable_dma,
  342. .disable_dma = cs5535audio_capture_disable_dma,
  343. .setup_prd = cs5535audio_capture_setup_prd,
  344. .pause_dma = cs5535audio_capture_pause_dma,
  345. .read_dma_pntr = cs5535audio_capture_read_dma_pntr,
  346. };
  347. int __devinit snd_cs5535audio_pcm(struct cs5535audio *cs5535au)
  348. {
  349. struct snd_pcm *pcm;
  350. int err;
  351. err = snd_pcm_new(cs5535au->card, "CS5535 Audio", 0, 1, 1, &pcm);
  352. if (err < 0)
  353. return err;
  354. cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK].ops =
  355. &snd_cs5535audio_playback_dma_ops;
  356. cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE].ops =
  357. &snd_cs5535audio_capture_dma_ops;
  358. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  359. &snd_cs5535audio_playback_ops);
  360. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  361. &snd_cs5535audio_capture_ops);
  362. pcm->private_data = cs5535au;
  363. pcm->info_flags = 0;
  364. strcpy(pcm->name, "CS5535 Audio");
  365. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  366. snd_dma_pci_data(cs5535au->pci),
  367. 64*1024, 128*1024);
  368. return 0;
  369. }