cs5535audio_pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/core.h>
  28. #include <sound/control.h>
  29. #include <sound/initval.h>
  30. #include <sound/asoundef.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/ac97_codec.h>
  34. #include "cs5535audio.h"
  35. static struct snd_pcm_hardware snd_cs5535audio_playback =
  36. {
  37. .info = (
  38. SNDRV_PCM_INFO_MMAP |
  39. SNDRV_PCM_INFO_INTERLEAVED |
  40. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  41. SNDRV_PCM_INFO_MMAP_VALID |
  42. SNDRV_PCM_INFO_PAUSE |
  43. SNDRV_PCM_INFO_RESUME
  44. ),
  45. .formats = (
  46. SNDRV_PCM_FMTBIT_S16_LE
  47. ),
  48. .rates = (
  49. SNDRV_PCM_RATE_CONTINUOUS |
  50. SNDRV_PCM_RATE_8000_48000
  51. ),
  52. .rate_min = 4000,
  53. .rate_max = 48000,
  54. .channels_min = 2,
  55. .channels_max = 2,
  56. .buffer_bytes_max = (128*1024),
  57. .period_bytes_min = 64,
  58. .period_bytes_max = (64*1024 - 16),
  59. .periods_min = 1,
  60. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  61. .fifo_size = 0,
  62. };
  63. static struct snd_pcm_hardware snd_cs5535audio_capture =
  64. {
  65. .info = (
  66. SNDRV_PCM_INFO_MMAP |
  67. SNDRV_PCM_INFO_INTERLEAVED |
  68. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  69. SNDRV_PCM_INFO_MMAP_VALID
  70. ),
  71. .formats = (
  72. SNDRV_PCM_FMTBIT_S16_LE
  73. ),
  74. .rates = (
  75. SNDRV_PCM_RATE_CONTINUOUS |
  76. SNDRV_PCM_RATE_8000_48000
  77. ),
  78. .rate_min = 4000,
  79. .rate_max = 48000,
  80. .channels_min = 2,
  81. .channels_max = 2,
  82. .buffer_bytes_max = (128*1024),
  83. .period_bytes_min = 64,
  84. .period_bytes_max = (64*1024 - 16),
  85. .periods_min = 1,
  86. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  87. .fifo_size = 0,
  88. };
  89. static int snd_cs5535audio_playback_open(struct snd_pcm_substream *substream)
  90. {
  91. int err;
  92. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  93. struct snd_pcm_runtime *runtime = substream->runtime;
  94. runtime->hw = snd_cs5535audio_playback;
  95. runtime->hw.rates = cs5535au->ac97->rates[AC97_RATES_FRONT_DAC];
  96. snd_pcm_limit_hw_rates(runtime);
  97. cs5535au->playback_substream = substream;
  98. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK]);
  99. if ((err = snd_pcm_hw_constraint_integer(runtime,
  100. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  101. return err;
  102. return 0;
  103. }
  104. static int snd_cs5535audio_playback_close(struct snd_pcm_substream *substream)
  105. {
  106. return 0;
  107. }
  108. #define CS5535AUDIO_DESC_LIST_SIZE \
  109. PAGE_ALIGN(CS5535AUDIO_MAX_DESCRIPTORS * sizeof(struct cs5535audio_dma_desc))
  110. static int cs5535audio_build_dma_packets(struct cs5535audio *cs5535au,
  111. struct cs5535audio_dma *dma,
  112. struct snd_pcm_substream *substream,
  113. unsigned int periods,
  114. unsigned int period_bytes)
  115. {
  116. unsigned int i;
  117. u32 addr, desc_addr, jmpprd_addr;
  118. struct cs5535audio_dma_desc *lastdesc;
  119. if (periods > CS5535AUDIO_MAX_DESCRIPTORS)
  120. return -ENOMEM;
  121. if (dma->desc_buf.area == NULL) {
  122. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  123. snd_dma_pci_data(cs5535au->pci),
  124. CS5535AUDIO_DESC_LIST_SIZE+1,
  125. &dma->desc_buf) < 0)
  126. return -ENOMEM;
  127. dma->period_bytes = dma->periods = 0;
  128. }
  129. if (dma->periods == periods && dma->period_bytes == period_bytes)
  130. return 0;
  131. /* the u32 cast is okay because in snd*create we successfully told
  132. pci alloc that we're only 32 bit capable so the uppper will be 0 */
  133. addr = (u32) substream->runtime->dma_addr;
  134. desc_addr = (u32) dma->desc_buf.addr;
  135. for (i = 0; i < periods; i++) {
  136. struct cs5535audio_dma_desc *desc =
  137. &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[i];
  138. desc->addr = cpu_to_le32(addr);
  139. desc->size = cpu_to_le32(period_bytes);
  140. desc->ctlreserved = cpu_to_le32(PRD_EOP);
  141. desc_addr += sizeof(struct cs5535audio_dma_desc);
  142. addr += period_bytes;
  143. }
  144. /* we reserved one dummy descriptor at the end to do the PRD jump */
  145. lastdesc = &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[periods];
  146. lastdesc->addr = cpu_to_le32((u32) dma->desc_buf.addr);
  147. lastdesc->size = 0;
  148. lastdesc->ctlreserved = cpu_to_le32(PRD_JMP);
  149. jmpprd_addr = cpu_to_le32(lastdesc->addr +
  150. (sizeof(struct cs5535audio_dma_desc)*periods));
  151. dma->substream = substream;
  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_prd(struct cs5535audio *cs5535au)
  178. {
  179. return cs_readl(cs5535au, ACC_BM0_PRD);
  180. }
  181. static u32 cs5535audio_playback_read_dma_pntr(struct cs5535audio *cs5535au)
  182. {
  183. return cs_readl(cs5535au, ACC_BM0_PNTR);
  184. }
  185. static void cs5535audio_capture_enable_dma(struct cs5535audio *cs5535au)
  186. {
  187. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_EN);
  188. }
  189. static void cs5535audio_capture_disable_dma(struct cs5535audio *cs5535au)
  190. {
  191. cs_writeb(cs5535au, ACC_BM1_CMD, 0);
  192. }
  193. static void cs5535audio_capture_pause_dma(struct cs5535audio *cs5535au)
  194. {
  195. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_PAUSE);
  196. }
  197. static void cs5535audio_capture_setup_prd(struct cs5535audio *cs5535au,
  198. u32 prd_addr)
  199. {
  200. cs_writel(cs5535au, ACC_BM1_PRD, prd_addr);
  201. }
  202. static u32 cs5535audio_capture_read_prd(struct cs5535audio *cs5535au)
  203. {
  204. return cs_readl(cs5535au, ACC_BM1_PRD);
  205. }
  206. static u32 cs5535audio_capture_read_dma_pntr(struct cs5535audio *cs5535au)
  207. {
  208. return cs_readl(cs5535au, ACC_BM1_PNTR);
  209. }
  210. static void cs5535audio_clear_dma_packets(struct cs5535audio *cs5535au,
  211. struct cs5535audio_dma *dma,
  212. struct snd_pcm_substream *substream)
  213. {
  214. snd_dma_free_pages(&dma->desc_buf);
  215. dma->desc_buf.area = NULL;
  216. dma->substream = 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. if (!err)
  234. dma->pcm_open_flag = 1;
  235. return err;
  236. }
  237. static int snd_cs5535audio_hw_free(struct snd_pcm_substream *substream)
  238. {
  239. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  240. struct cs5535audio_dma *dma = substream->runtime->private_data;
  241. if (dma->pcm_open_flag) {
  242. if (substream == cs5535au->playback_substream)
  243. snd_ac97_update_power(cs5535au->ac97,
  244. AC97_PCM_FRONT_DAC_RATE, 0);
  245. else
  246. snd_ac97_update_power(cs5535au->ac97,
  247. AC97_PCM_LR_ADC_RATE, 0);
  248. dma->pcm_open_flag = 0;
  249. }
  250. cs5535audio_clear_dma_packets(cs5535au, dma, substream);
  251. return snd_pcm_lib_free_pages(substream);
  252. }
  253. static int snd_cs5535audio_playback_prepare(struct snd_pcm_substream *substream)
  254. {
  255. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  256. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_FRONT_DAC_RATE,
  257. substream->runtime->rate);
  258. }
  259. static int snd_cs5535audio_trigger(struct snd_pcm_substream *substream, int cmd)
  260. {
  261. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  262. struct cs5535audio_dma *dma = substream->runtime->private_data;
  263. int err = 0;
  264. spin_lock(&cs5535au->reg_lock);
  265. switch (cmd) {
  266. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  267. dma->ops->pause_dma(cs5535au);
  268. break;
  269. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  270. dma->ops->enable_dma(cs5535au);
  271. break;
  272. case SNDRV_PCM_TRIGGER_START:
  273. dma->ops->enable_dma(cs5535au);
  274. break;
  275. case SNDRV_PCM_TRIGGER_RESUME:
  276. dma->ops->enable_dma(cs5535au);
  277. break;
  278. case SNDRV_PCM_TRIGGER_STOP:
  279. dma->ops->disable_dma(cs5535au);
  280. break;
  281. case SNDRV_PCM_TRIGGER_SUSPEND:
  282. dma->ops->disable_dma(cs5535au);
  283. break;
  284. default:
  285. snd_printk(KERN_ERR "unhandled trigger\n");
  286. err = -EINVAL;
  287. break;
  288. }
  289. spin_unlock(&cs5535au->reg_lock);
  290. return err;
  291. }
  292. static snd_pcm_uframes_t snd_cs5535audio_pcm_pointer(struct snd_pcm_substream
  293. *substream)
  294. {
  295. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  296. u32 curdma;
  297. struct cs5535audio_dma *dma;
  298. dma = substream->runtime->private_data;
  299. curdma = dma->ops->read_dma_pntr(cs5535au);
  300. if (curdma < dma->buf_addr) {
  301. snd_printk(KERN_ERR "curdma=%x < %x bufaddr.\n",
  302. curdma, dma->buf_addr);
  303. return 0;
  304. }
  305. curdma -= dma->buf_addr;
  306. if (curdma >= dma->buf_bytes) {
  307. snd_printk(KERN_ERR "diff=%x >= %x buf_bytes.\n",
  308. curdma, dma->buf_bytes);
  309. return 0;
  310. }
  311. return bytes_to_frames(substream->runtime, curdma);
  312. }
  313. static int snd_cs5535audio_capture_open(struct snd_pcm_substream *substream)
  314. {
  315. int err;
  316. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  317. struct snd_pcm_runtime *runtime = substream->runtime;
  318. runtime->hw = snd_cs5535audio_capture;
  319. runtime->hw.rates = cs5535au->ac97->rates[AC97_RATES_ADC];
  320. snd_pcm_limit_hw_rates(runtime);
  321. cs5535au->capture_substream = substream;
  322. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE]);
  323. if ((err = snd_pcm_hw_constraint_integer(runtime,
  324. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  325. return err;
  326. olpc_capture_open(cs5535au->ac97);
  327. return 0;
  328. }
  329. static int snd_cs5535audio_capture_close(struct snd_pcm_substream *substream)
  330. {
  331. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  332. olpc_capture_close(cs5535au->ac97);
  333. return 0;
  334. }
  335. static int snd_cs5535audio_capture_prepare(struct snd_pcm_substream *substream)
  336. {
  337. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  338. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_LR_ADC_RATE,
  339. substream->runtime->rate);
  340. }
  341. static struct snd_pcm_ops snd_cs5535audio_playback_ops = {
  342. .open = snd_cs5535audio_playback_open,
  343. .close = snd_cs5535audio_playback_close,
  344. .ioctl = snd_pcm_lib_ioctl,
  345. .hw_params = snd_cs5535audio_hw_params,
  346. .hw_free = snd_cs5535audio_hw_free,
  347. .prepare = snd_cs5535audio_playback_prepare,
  348. .trigger = snd_cs5535audio_trigger,
  349. .pointer = snd_cs5535audio_pcm_pointer,
  350. };
  351. static struct snd_pcm_ops snd_cs5535audio_capture_ops = {
  352. .open = snd_cs5535audio_capture_open,
  353. .close = snd_cs5535audio_capture_close,
  354. .ioctl = snd_pcm_lib_ioctl,
  355. .hw_params = snd_cs5535audio_hw_params,
  356. .hw_free = snd_cs5535audio_hw_free,
  357. .prepare = snd_cs5535audio_capture_prepare,
  358. .trigger = snd_cs5535audio_trigger,
  359. .pointer = snd_cs5535audio_pcm_pointer,
  360. };
  361. static struct cs5535audio_dma_ops snd_cs5535audio_playback_dma_ops = {
  362. .type = CS5535AUDIO_DMA_PLAYBACK,
  363. .enable_dma = cs5535audio_playback_enable_dma,
  364. .disable_dma = cs5535audio_playback_disable_dma,
  365. .setup_prd = cs5535audio_playback_setup_prd,
  366. .read_prd = cs5535audio_playback_read_prd,
  367. .pause_dma = cs5535audio_playback_pause_dma,
  368. .read_dma_pntr = cs5535audio_playback_read_dma_pntr,
  369. };
  370. static struct cs5535audio_dma_ops snd_cs5535audio_capture_dma_ops = {
  371. .type = CS5535AUDIO_DMA_CAPTURE,
  372. .enable_dma = cs5535audio_capture_enable_dma,
  373. .disable_dma = cs5535audio_capture_disable_dma,
  374. .setup_prd = cs5535audio_capture_setup_prd,
  375. .read_prd = cs5535audio_capture_read_prd,
  376. .pause_dma = cs5535audio_capture_pause_dma,
  377. .read_dma_pntr = cs5535audio_capture_read_dma_pntr,
  378. };
  379. int __devinit snd_cs5535audio_pcm(struct cs5535audio *cs5535au)
  380. {
  381. struct snd_pcm *pcm;
  382. int err;
  383. err = snd_pcm_new(cs5535au->card, "CS5535 Audio", 0, 1, 1, &pcm);
  384. if (err < 0)
  385. return err;
  386. cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK].ops =
  387. &snd_cs5535audio_playback_dma_ops;
  388. cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE].ops =
  389. &snd_cs5535audio_capture_dma_ops;
  390. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  391. &snd_cs5535audio_playback_ops);
  392. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  393. &snd_cs5535audio_capture_ops);
  394. pcm->private_data = cs5535au;
  395. pcm->info_flags = 0;
  396. strcpy(pcm->name, "CS5535 Audio");
  397. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  398. snd_dma_pci_data(cs5535au->pci),
  399. 64*1024, 128*1024);
  400. cs5535au->pcm = pcm;
  401. return 0;
  402. }