pxa-ac97.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:9k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/drivers/sound/pxa-ac97.c -- AC97 interface for the Cotula chip
  3.  *
  4.  *  Author: Nicolas Pitre
  5.  *  Created: Aug 15, 2001
  6.  *  Copyright: MontaVista Software Inc.
  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/init.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/pci.h>
  17. #include <linux/completion.h>
  18. #include <linux/delay.h>
  19. #include <linux/poll.h>
  20. #include <linux/sound.h>
  21. #include <linux/soundcard.h>
  22. #include <linux/ac97_codec.h>
  23. #include <asm/hardware.h>
  24. #include <asm/irq.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/semaphore.h>
  27. #include <asm/dma.h>
  28. #include "pxa-audio.h"
  29. /*
  30.  * The codec register read operation requires 3 read cycles on PXA250 in order
  31.  * to guarrantee that good read data can be returned.
  32.  *            _             _              _            _
  33.  *sync: _____| |_addr______| |_data1______| |__data2___| |__data_n__
  34.  *SDONE:__            _              _              _______________
  35.  *        |_addr1____| |__addr2_____| |__addr_n____|
  36.  *       ^           
  37.  *       First read begins
  38.  *                   ^ SDONE usually goes true in the latter half of AC link frame
  39.  *                     ^ Second read begins, but data from codec hasn't arrived yet!
  40.  *                                  ^ second read ends, from 1 to 3 frames AFTER frame
  41.  *                                    in which the address goes out!
  42.  *                                    ^ Third read begins from one to 3 frames after the
  43.  *                                      initial frame, data from codec guarranteed to be 
  44.  *                                      available by this time.
  45.  *                                                 ^ read cycle ends.
  46.  * Note how reads can be pipelined, possibly useful for reading touch panel
  47.  * control registers or rapid sampling of codec gpio lines. 
  48.  */
  49. static struct completion CAR_completion;
  50. static DECLARE_MUTEX(CAR_mutex);
  51. static u16 pxa_ac97_read(struct ac97_codec *codec, u8 reg)
  52. {
  53. u16 val = -1;
  54. down(&CAR_mutex);
  55. if (!(CAR & CAR_CAIP)) {
  56. volatile u32 *reg_addr = (u32 *)&PAC_REG_BASE + (reg >> 1);
  57. init_completion(&CAR_completion);
  58. (void)*reg_addr;
  59. wait_for_completion(&CAR_completion);
  60. init_completion(&CAR_completion);
  61. (void)*reg_addr; // This initiates second read cycle, but codec data isn't here yet...
  62. wait_for_completion(&CAR_completion);
  63. if (GSR & GSR_RDCS) {
  64. GSR |= GSR_RDCS;
  65. printk(KERN_CRIT __FUNCTION__": read codec register timeout.n");
  66. }
  67. init_completion(&CAR_completion);
  68. val = *reg_addr; // ahh, that's better. But we've just started another cycle...
  69. wait_for_completion(&CAR_completion);  //this, along with trailing delay, avoids hassle with CAR_CAIP bit
  70. udelay(20);  //don't come back too soon...
  71. } else {
  72. printk(KERN_CRIT __FUNCTION__": CAR_CAIP already setn");
  73. }
  74. up(&CAR_mutex);
  75. //printk("%s(0x%02x) = 0x%04xn", __FUNCTION__, reg, val);
  76. return val;
  77. }
  78. static void pxa_ac97_write(struct ac97_codec *codec, u8 reg, u16 val)
  79. {
  80. down(&CAR_mutex);
  81. if (!(CAR & CAR_CAIP)) {
  82. volatile u32 *reg_addr = (u32 *)&PAC_REG_BASE + (reg >> 1);
  83. init_completion(&CAR_completion);
  84. *reg_addr = val;
  85. wait_for_completion(&CAR_completion);
  86. } else {
  87. printk(KERN_CRIT __FUNCTION__": CAR_CAIP already setn");
  88. }
  89. up(&CAR_mutex);
  90. //printk("%s(0x%02x, 0x%04x)n", __FUNCTION__, reg, val);
  91. }
  92. static void pxa_ac97_irq(int irq, void *dev_id, struct pt_regs *regs)
  93. {
  94. if (GSR & (GSR_SDONE|GSR_CDONE)) {
  95. GSR = GSR_SDONE|GSR_CDONE;
  96. complete(&CAR_completion);
  97. }
  98. }
  99. static struct ac97_codec pxa_ac97_codec = {
  100. codec_read: pxa_ac97_read,
  101. codec_write: pxa_ac97_write,
  102. };
  103. static DECLARE_MUTEX(pxa_ac97_mutex);
  104. static int pxa_ac97_refcount;
  105. int pxa_ac97_get(struct ac97_codec **codec)
  106. {
  107. int ret;
  108. *codec = NULL;
  109. down(&pxa_ac97_mutex);
  110. if (!pxa_ac97_refcount) {
  111. ret = request_irq(IRQ_AC97, pxa_ac97_irq, 0, "AC97", NULL);
  112. if (ret)
  113. return ret;
  114. CKEN |= CKEN2_AC97; 
  115. set_GPIO_mode(GPIO31_SYNC_AC97_MD);
  116. set_GPIO_mode(GPIO30_SDATA_OUT_AC97_MD);
  117. set_GPIO_mode(GPIO28_BITCLK_AC97_MD);
  118. set_GPIO_mode(GPIO29_SDATA_IN_AC97_MD);
  119. GCR = 0;
  120. udelay(10);
  121. GCR = GCR_COLD_RST|GCR_CDONE_IE|GCR_SDONE_IE;
  122. while (!(GSR & GSR_PCR)) {
  123. schedule();
  124. }
  125. ret = ac97_probe_codec(&pxa_ac97_codec);
  126. if (ret != 1) {
  127. free_irq(IRQ_AC97, NULL);
  128. GCR = GCR_ACLINK_OFF;
  129. CKEN &= ~CKEN2_AC97; 
  130. return ret;
  131. }
  132. // need little hack for UCB1400 (should be moved elsewhere)
  133. pxa_ac97_write(&pxa_ac97_codec,AC97_EXTENDED_STATUS,1);
  134. //pxa_ac97_write(&pxa_ac97_codec, 0x6a, 0x1ff7);
  135. pxa_ac97_write(&pxa_ac97_codec, 0x6a, 0x0050);
  136. pxa_ac97_write(&pxa_ac97_codec, 0x6c, 0x0030);
  137. }
  138. pxa_ac97_refcount++;
  139. up(&pxa_ac97_mutex);
  140. *codec = &pxa_ac97_codec;
  141. return 0;
  142. }
  143. void pxa_ac97_put(void)
  144. {
  145. down(&pxa_ac97_mutex);
  146. pxa_ac97_refcount--;
  147. if (!pxa_ac97_refcount) {
  148. GCR = GCR_ACLINK_OFF;
  149. CKEN &= ~CKEN2_AC97; 
  150. free_irq(IRQ_AC97, NULL);
  151. }
  152. up(&pxa_ac97_mutex);
  153. }
  154. EXPORT_SYMBOL(pxa_ac97_get);
  155. EXPORT_SYMBOL(pxa_ac97_put);
  156. /*
  157.  * Audio Mixer stuff
  158.  */
  159. static audio_state_t ac97_audio_state;
  160. static audio_stream_t ac97_audio_in;
  161. static int mixer_ioctl( struct inode *inode, struct file *file,
  162. unsigned int cmd, unsigned long arg)
  163. {
  164. int ret, val;
  165. ret = pxa_ac97_codec.mixer_ioctl(&pxa_ac97_codec, cmd, arg);
  166. if (ret)
  167. return ret;
  168. /* We must snoop for some commands to provide our own extra processing */
  169. switch (cmd) {
  170. case SOUND_MIXER_WRITE_RECSRC:
  171. /*
  172.  * According to the PXA250 spec, mic-in should use different
  173.  * DRCMR and different AC97 FIFO.
  174.  * Unfortunately current UCB1400 versions (up to ver 2A) don't
  175.  * produce slot 6 for the audio input frame, therefore the PXA
  176.  * AC97 mic-in FIFO is always starved.
  177.  */
  178. #if 0
  179. ret = get_user(val, (int *)arg);
  180. if (ret)
  181. return ret;
  182. pxa_audio_clear_buf(&ac97_audio_in);
  183. *ac97_audio_in.drcmr = 0;
  184. if (val & (1 << SOUND_MIXER_MIC)) {
  185. ac97_audio_in.dcmd = DCMD_RXMCDR;
  186. ac97_audio_in.drcmr = &DRCMRRXMCDR;
  187. ac97_audio_in.dev_addr = __PREG(MCDR);
  188. } else {
  189. ac97_audio_in.dcmd = DCMD_RXPCDR;
  190. ac97_audio_in.drcmr = &DRCMRRXPCDR;
  191. ac97_audio_in.dev_addr = __PREG(PCDR);
  192. }
  193. if (ac97_audio_state.rd_ref)
  194. *ac97_audio_in.drcmr =
  195. ac97_audio_in.dma_ch | DRCMR_MAPVLD;
  196. #endif
  197. break;
  198. }
  199. return 0;
  200. }
  201. static struct file_operations mixer_fops = {
  202. ioctl: mixer_ioctl,
  203. llseek: no_llseek,
  204. owner: THIS_MODULE
  205. };
  206. /*
  207.  * AC97 codec ioctls
  208.  */
  209. static int codec_adc_rate = 48000;
  210. static int codec_dac_rate = 48000;
  211. static int ac97_ioctl(struct inode *inode, struct file *file,
  212.       unsigned int cmd, unsigned long arg)
  213. {
  214. int ret;
  215. long val;
  216. switch(cmd) {
  217. case SNDCTL_DSP_STEREO:
  218. ret = get_user(val, (int *) arg);
  219. if (ret)
  220. return ret;
  221. /* FIXME: do we support mono? */
  222. ret = (val == 0) ? -EINVAL : 1;
  223. return put_user(ret, (int *) arg);
  224. case SNDCTL_DSP_CHANNELS:
  225. case SOUND_PCM_READ_CHANNELS:
  226. /* FIXME: do we support mono? */
  227. return put_user(2, (long *) arg);
  228. case SNDCTL_DSP_SPEED:
  229. ret = get_user(val, (long *) arg);
  230. if (ret)
  231. return ret;
  232. if (file->f_mode & FMODE_READ)
  233. codec_adc_rate = ac97_set_adc_rate(&pxa_ac97_codec, val);
  234. if (file->f_mode & FMODE_WRITE)
  235. codec_dac_rate = ac97_set_dac_rate(&pxa_ac97_codec, val);
  236. /* fall through */
  237. case SOUND_PCM_READ_RATE:
  238. if (file->f_mode & FMODE_READ)
  239. val = codec_adc_rate;
  240. if (file->f_mode & FMODE_WRITE)
  241. val = codec_dac_rate;
  242. return put_user(val, (long *) arg);
  243. case SNDCTL_DSP_SETFMT:
  244. case SNDCTL_DSP_GETFMTS:
  245. /* FIXME: can we do other fmts? */
  246. return put_user(AFMT_S16_LE, (long *) arg);
  247. default:
  248. /* Maybe this is meant for the mixer (As per OSS Docs) */
  249. return mixer_ioctl(inode, file, cmd, arg);
  250. }
  251. return 0;
  252. }
  253. /*
  254.  * Audio stuff
  255.  */
  256. static audio_stream_t ac97_audio_out = {
  257. name: "AC97 audio out",
  258. dcmd: DCMD_TXPCDR,
  259. drcmr: &DRCMRTXPCDR,
  260. dev_addr: __PREG(PCDR),
  261. };
  262. static audio_stream_t ac97_audio_in = {
  263. name: "AC97 audio in",
  264. dcmd: DCMD_RXPCDR,
  265. drcmr: &DRCMRRXPCDR,
  266. dev_addr: __PREG(PCDR),
  267. };
  268. static audio_state_t ac97_audio_state = {
  269. output_stream: &ac97_audio_out,
  270. input_stream: &ac97_audio_in,
  271. client_ioctl: ac97_ioctl,
  272. sem: __MUTEX_INITIALIZER(ac97_audio_state.sem),
  273. };
  274. static int ac97_audio_open(struct inode *inode, struct file *file)
  275. {
  276. return pxa_audio_attach(inode, file, &ac97_audio_state);
  277. }
  278. /*
  279.  * Missing fields of this structure will be patched with the call
  280.  * to pxa_audio_attach().
  281.  */
  282. static struct file_operations ac97_audio_fops = {
  283. open: ac97_audio_open,
  284. owner: THIS_MODULE
  285. };
  286. static int __init pxa_ac97_init(void)
  287. {
  288. int ret;
  289. struct ac97_codec *dummy;
  290. ret = pxa_ac97_get(&dummy);
  291. if (ret)
  292. return ret;
  293. ac97_audio_state.dev_dsp = register_sound_dsp(&ac97_audio_fops, -1);
  294. pxa_ac97_codec.dev_mixer = register_sound_mixer(&mixer_fops, -1);
  295. return 0;
  296. }
  297. static void __exit pxa_ac97_exit(void)
  298. {
  299. unregister_sound_dsp(ac97_audio_state.dev_dsp);
  300. unregister_sound_mixer(pxa_ac97_codec.dev_mixer);
  301. pxa_ac97_put();
  302. }
  303. module_init(pxa_ac97_init);
  304. module_exit(pxa_ac97_exit);