cs5535audio_pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 snd_pcm_hardware_t 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 snd_pcm_hardware_t 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(snd_pcm_substream_t *substream)
  92. {
  93. int err;
  94. cs5535audio_t *cs5535au = snd_pcm_substream_chip(substream);
  95. snd_pcm_runtime_t *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(snd_pcm_substream_t *substream)
  106. {
  107. return 0;
  108. }
  109. #define CS5535AUDIO_DESC_LIST_SIZE \
  110. PAGE_ALIGN(CS5535AUDIO_MAX_DESCRIPTORS * sizeof(cs5535audio_dma_desc_t))
  111. static int cs5535audio_build_dma_packets(cs5535audio_t *cs5535au,
  112. cs5535audio_dma_t *dma,
  113. snd_pcm_substream_t *substream,
  114. unsigned int periods,
  115. unsigned int period_bytes)
  116. {
  117. unsigned int i;
  118. u32 addr, desc_addr, jmpprd_addr;
  119. cs5535audio_dma_desc_t *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. cs5535audio_dma_desc_t *desc =
  138. &((cs5535audio_dma_desc_t *) dma->desc_buf.area)[i];
  139. desc->addr = cpu_to_le32(addr);
  140. desc->size = period_bytes;
  141. desc->ctlreserved = PRD_EOP;
  142. desc_addr += sizeof(cs5535audio_dma_desc_t);
  143. addr += period_bytes;
  144. }
  145. /* we reserved one dummy descriptor at the end to do the PRD jump */
  146. lastdesc = &((cs5535audio_dma_desc_t *) dma->desc_buf.area)[periods];
  147. lastdesc->addr = cpu_to_le32((u32) dma->desc_buf.addr);
  148. lastdesc->size = 0;
  149. lastdesc->ctlreserved = PRD_JMP;
  150. jmpprd_addr = cpu_to_le32(lastdesc->addr +
  151. (sizeof(cs5535audio_dma_desc_t)*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(cs5535audio_t *cs5535au)
  161. {
  162. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_EN);
  163. }
  164. static void cs5535audio_playback_disable_dma(cs5535audio_t *cs5535au)
  165. {
  166. cs_writeb(cs5535au, ACC_BM0_CMD, 0);
  167. }
  168. static void cs5535audio_playback_pause_dma(cs5535audio_t *cs5535au)
  169. {
  170. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_PAUSE);
  171. }
  172. static void cs5535audio_playback_setup_prd(cs5535audio_t *cs5535au,
  173. u32 prd_addr)
  174. {
  175. cs_writel(cs5535au, ACC_BM0_PRD, prd_addr);
  176. }
  177. static u32 cs5535audio_playback_read_dma_pntr(cs5535audio_t *cs5535au)
  178. {
  179. return cs_readl(cs5535au, ACC_BM0_PNTR);
  180. }
  181. static void cs5535audio_capture_enable_dma(cs5535audio_t *cs5535au)
  182. {
  183. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_EN);
  184. }
  185. static void cs5535audio_capture_disable_dma(cs5535audio_t *cs5535au)
  186. {
  187. cs_writeb(cs5535au, ACC_BM1_CMD, 0);
  188. }
  189. static void cs5535audio_capture_pause_dma(cs5535audio_t *cs5535au)
  190. {
  191. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_PAUSE);
  192. }
  193. static void cs5535audio_capture_setup_prd(cs5535audio_t *cs5535au,
  194. u32 prd_addr)
  195. {
  196. cs_writel(cs5535au, ACC_BM1_PRD, prd_addr);
  197. }
  198. static u32 cs5535audio_capture_read_dma_pntr(cs5535audio_t *cs5535au)
  199. {
  200. return cs_readl(cs5535au, ACC_BM1_PNTR);
  201. }
  202. static void cs5535audio_clear_dma_packets(cs5535audio_t *cs5535au,
  203. cs5535audio_dma_t *dma,
  204. snd_pcm_substream_t *substream)
  205. {
  206. snd_dma_free_pages(&dma->desc_buf);
  207. dma->desc_buf.area = NULL;
  208. }
  209. static int snd_cs5535audio_hw_params(snd_pcm_substream_t *substream,
  210. snd_pcm_hw_params_t *hw_params)
  211. {
  212. cs5535audio_t *cs5535au = snd_pcm_substream_chip(substream);
  213. cs5535audio_dma_t *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(snd_pcm_substream_t *substream)
  227. {
  228. cs5535audio_t *cs5535au = snd_pcm_substream_chip(substream);
  229. cs5535audio_dma_t *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(snd_pcm_substream_t *substream)
  234. {
  235. cs5535audio_t *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(snd_pcm_substream_t *substream, int cmd)
  240. {
  241. cs5535audio_t *cs5535au = snd_pcm_substream_chip(substream);
  242. cs5535audio_dma_t *dma = substream->runtime->private_data;
  243. switch (cmd) {
  244. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  245. spin_lock_irq(&cs5535au->reg_lock);
  246. dma->ops->pause_dma(cs5535au);
  247. spin_unlock_irq(&cs5535au->reg_lock);
  248. break;
  249. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  250. spin_lock_irq(&cs5535au->reg_lock);
  251. dma->ops->enable_dma(cs5535au);
  252. spin_unlock_irq(&cs5535au->reg_lock);
  253. break;
  254. case SNDRV_PCM_TRIGGER_START:
  255. spin_lock_irq(&cs5535au->reg_lock);
  256. dma->ops->enable_dma(cs5535au);
  257. spin_unlock_irq(&cs5535au->reg_lock);
  258. break;
  259. case SNDRV_PCM_TRIGGER_STOP:
  260. spin_lock_irq(&cs5535au->reg_lock);
  261. dma->ops->disable_dma(cs5535au);
  262. spin_unlock_irq(&cs5535au->reg_lock);
  263. break;
  264. default:
  265. snd_printk(KERN_ERR "unhandled trigger\n");
  266. return -EINVAL;
  267. break;
  268. }
  269. return 0;
  270. }
  271. static snd_pcm_uframes_t snd_cs5535audio_pcm_pointer(snd_pcm_substream_t
  272. *substream)
  273. {
  274. cs5535audio_t *cs5535au = snd_pcm_substream_chip(substream);
  275. u32 curdma;
  276. cs5535audio_dma_t *dma;
  277. dma = substream->runtime->private_data;
  278. curdma = dma->ops->read_dma_pntr(cs5535au);
  279. if (curdma < dma->buf_addr) {
  280. snd_printk(KERN_ERR "curdma=%x < %x bufaddr.\n",
  281. curdma, dma->buf_addr);
  282. return 0;
  283. }
  284. curdma -= dma->buf_addr;
  285. if (curdma >= dma->buf_bytes) {
  286. snd_printk(KERN_ERR "diff=%x >= %x buf_bytes.\n",
  287. curdma, dma->buf_bytes);
  288. return 0;
  289. }
  290. return bytes_to_frames(substream->runtime, curdma);
  291. }
  292. static int snd_cs5535audio_capture_open(snd_pcm_substream_t *substream)
  293. {
  294. int err;
  295. cs5535audio_t *cs5535au = snd_pcm_substream_chip(substream);
  296. snd_pcm_runtime_t *runtime = substream->runtime;
  297. runtime->hw = snd_cs5535audio_capture;
  298. cs5535au->capture_substream = substream;
  299. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE]);
  300. snd_pcm_set_sync(substream);
  301. if ((err = snd_pcm_hw_constraint_integer(runtime,
  302. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  303. return err;
  304. return 0;
  305. }
  306. static int snd_cs5535audio_capture_close(snd_pcm_substream_t *substream)
  307. {
  308. return 0;
  309. }
  310. static int snd_cs5535audio_capture_prepare(snd_pcm_substream_t *substream)
  311. {
  312. cs5535audio_t *cs5535au = snd_pcm_substream_chip(substream);
  313. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_LR_ADC_RATE,
  314. substream->runtime->rate);
  315. }
  316. static snd_pcm_ops_t snd_cs5535audio_playback_ops = {
  317. .open = snd_cs5535audio_playback_open,
  318. .close = snd_cs5535audio_playback_close,
  319. .ioctl = snd_pcm_lib_ioctl,
  320. .hw_params = snd_cs5535audio_hw_params,
  321. .hw_free = snd_cs5535audio_hw_free,
  322. .prepare = snd_cs5535audio_playback_prepare,
  323. .trigger = snd_cs5535audio_trigger,
  324. .pointer = snd_cs5535audio_pcm_pointer,
  325. };
  326. static snd_pcm_ops_t snd_cs5535audio_capture_ops = {
  327. .open = snd_cs5535audio_capture_open,
  328. .close = snd_cs5535audio_capture_close,
  329. .ioctl = snd_pcm_lib_ioctl,
  330. .hw_params = snd_cs5535audio_hw_params,
  331. .hw_free = snd_cs5535audio_hw_free,
  332. .prepare = snd_cs5535audio_capture_prepare,
  333. .trigger = snd_cs5535audio_trigger,
  334. .pointer = snd_cs5535audio_pcm_pointer,
  335. };
  336. static void snd_cs5535audio_pcm_free(snd_pcm_t *pcm)
  337. {
  338. snd_pcm_lib_preallocate_free_for_all(pcm);
  339. }
  340. static cs5535audio_dma_ops_t snd_cs5535audio_playback_dma_ops = {
  341. .type = CS5535AUDIO_DMA_PLAYBACK,
  342. .enable_dma = cs5535audio_playback_enable_dma,
  343. .disable_dma = cs5535audio_playback_disable_dma,
  344. .setup_prd = cs5535audio_playback_setup_prd,
  345. .pause_dma = cs5535audio_playback_pause_dma,
  346. .read_dma_pntr = cs5535audio_playback_read_dma_pntr,
  347. };
  348. static cs5535audio_dma_ops_t snd_cs5535audio_capture_dma_ops = {
  349. .type = CS5535AUDIO_DMA_CAPTURE,
  350. .enable_dma = cs5535audio_capture_enable_dma,
  351. .disable_dma = cs5535audio_capture_disable_dma,
  352. .setup_prd = cs5535audio_capture_setup_prd,
  353. .pause_dma = cs5535audio_capture_pause_dma,
  354. .read_dma_pntr = cs5535audio_capture_read_dma_pntr,
  355. };
  356. int __devinit snd_cs5535audio_pcm(cs5535audio_t *cs5535au)
  357. {
  358. snd_pcm_t *pcm;
  359. int err;
  360. err = snd_pcm_new(cs5535au->card, "CS5535 Audio", 0, 1, 1, &pcm);
  361. if (err < 0)
  362. return err;
  363. cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK].ops =
  364. &snd_cs5535audio_playback_dma_ops;
  365. cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE].ops =
  366. &snd_cs5535audio_capture_dma_ops;
  367. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  368. &snd_cs5535audio_playback_ops);
  369. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  370. &snd_cs5535audio_capture_ops);
  371. pcm->private_data = cs5535au;
  372. pcm->private_free = snd_cs5535audio_pcm_free;
  373. pcm->info_flags = 0;
  374. strcpy(pcm->name, "CS5535 Audio");
  375. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  376. snd_dma_pci_data(cs5535au->pci),
  377. 64*1024, 128*1024);
  378. return 0;
  379. }