au1x00.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * BRIEF MODULE DESCRIPTION
  3. * Driver for AMD Au1000 MIPS Processor, AC'97 Sound Port
  4. *
  5. * Copyright 2004 Cooper Street Innovations Inc.
  6. * Author: Charles Eidsness <charles@cooper-street.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  16. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. * History:
  29. *
  30. * 2004-09-09 Charles Eidsness -- Original verion -- based on
  31. * sa11xx-uda1341.c ALSA driver and the
  32. * au1000.c OSS driver.
  33. * 2004-09-09 Matt Porter -- Added support for ALSA 1.0.6
  34. *
  35. */
  36. #include <linux/ioport.h>
  37. #include <linux/interrupt.h>
  38. #include <sound/driver.h>
  39. #include <linux/init.h>
  40. #include <linux/slab.h>
  41. #include <linux/version.h>
  42. #include <sound/core.h>
  43. #include <sound/initval.h>
  44. #include <sound/pcm.h>
  45. #include <sound/ac97_codec.h>
  46. #include <asm/mach-au1x00/au1000.h>
  47. #include <asm/mach-au1x00/au1000_dma.h>
  48. MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>");
  49. MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver");
  50. MODULE_LICENSE("GPL");
  51. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
  52. MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}");
  53. #else
  54. MODULE_CLASSES("{sound}");
  55. MODULE_DEVICES("{{AMD,Au1000 AC'97}}");
  56. #endif
  57. #define chip_t au1000_t
  58. #define PLAYBACK 0
  59. #define CAPTURE 1
  60. #define AC97_SLOT_3 0x01
  61. #define AC97_SLOT_4 0x02
  62. #define AC97_SLOT_6 0x08
  63. #define AC97_CMD_IRQ 31
  64. #define READ 0
  65. #define WRITE 1
  66. #define READ_WAIT 2
  67. #define RW_DONE 3
  68. DECLARE_WAIT_QUEUE_HEAD(ac97_command_wq);
  69. typedef struct au1000_period au1000_period_t;
  70. struct au1000_period
  71. {
  72. u32 start;
  73. u32 relative_end; /*realtive to start of buffer*/
  74. au1000_period_t * next;
  75. };
  76. /*Au1000 AC97 Port Control Reisters*/
  77. typedef struct au1000_ac97_reg au1000_ac97_reg_t;
  78. struct au1000_ac97_reg {
  79. u32 volatile config;
  80. u32 volatile status;
  81. u32 volatile data;
  82. u32 volatile cmd;
  83. u32 volatile cntrl;
  84. };
  85. typedef struct audio_stream audio_stream_t;
  86. struct audio_stream {
  87. snd_pcm_substream_t * substream;
  88. int dma;
  89. spinlock_t dma_lock;
  90. au1000_period_t * buffer;
  91. unsigned long period_size;
  92. };
  93. typedef struct snd_card_au1000 {
  94. snd_card_t *card;
  95. au1000_ac97_reg_t volatile *ac97_ioport;
  96. struct resource *ac97_res_port;
  97. spinlock_t ac97_lock;
  98. ac97_t *ac97;
  99. snd_pcm_t *pcm;
  100. audio_stream_t *stream[2]; /* playback & capture */
  101. } au1000_t;
  102. static au1000_t *au1000 = NULL;
  103. /*--------------------------- Local Functions --------------------------------*/
  104. static void
  105. au1000_set_ac97_xmit_slots(long xmit_slots)
  106. {
  107. u32 volatile ac97_config;
  108. spin_lock(&au1000->ac97_lock);
  109. ac97_config = au1000->ac97_ioport->config;
  110. ac97_config = ac97_config & ~AC97C_XMIT_SLOTS_MASK;
  111. ac97_config |= (xmit_slots << AC97C_XMIT_SLOTS_BIT);
  112. au1000->ac97_ioport->config = ac97_config;
  113. spin_unlock(&au1000->ac97_lock);
  114. }
  115. static void
  116. au1000_set_ac97_recv_slots(long recv_slots)
  117. {
  118. u32 volatile ac97_config;
  119. spin_lock(&au1000->ac97_lock);
  120. ac97_config = au1000->ac97_ioport->config;
  121. ac97_config = ac97_config & ~AC97C_RECV_SLOTS_MASK;
  122. ac97_config |= (recv_slots << AC97C_RECV_SLOTS_BIT);
  123. au1000->ac97_ioport->config = ac97_config;
  124. spin_unlock(&au1000->ac97_lock);
  125. }
  126. static void
  127. au1000_dma_stop(audio_stream_t *stream)
  128. {
  129. unsigned long flags;
  130. au1000_period_t * pointer;
  131. au1000_period_t * pointer_next;
  132. if (stream->buffer != NULL) {
  133. spin_lock_irqsave(&stream->dma_lock, flags);
  134. disable_dma(stream->dma);
  135. spin_unlock_irqrestore(&stream->dma_lock, flags);
  136. pointer = stream->buffer;
  137. pointer_next = stream->buffer->next;
  138. do {
  139. kfree(pointer);
  140. pointer = pointer_next;
  141. pointer_next = pointer->next;
  142. } while (pointer != stream->buffer);
  143. stream->buffer = NULL;
  144. }
  145. }
  146. static void
  147. au1000_dma_start(audio_stream_t *stream)
  148. {
  149. snd_pcm_substream_t *substream = stream->substream;
  150. snd_pcm_runtime_t *runtime = substream->runtime;
  151. unsigned long flags, dma_start;
  152. int i;
  153. au1000_period_t * pointer;
  154. if (stream->buffer == NULL) {
  155. dma_start = virt_to_phys(runtime->dma_area);
  156. stream->period_size = frames_to_bytes(runtime,
  157. runtime->period_size);
  158. stream->buffer = kmalloc(sizeof(au1000_period_t), GFP_KERNEL);
  159. pointer = stream->buffer;
  160. for (i = 0 ; i < runtime->periods ; i++) {
  161. pointer->start = (u32)(dma_start +
  162. (i * stream->period_size));
  163. pointer->relative_end = (u32)
  164. (((i+1) * stream->period_size) - 0x1);
  165. if ( i < runtime->periods - 1) {
  166. pointer->next = kmalloc(sizeof(au1000_period_t)
  167. , GFP_KERNEL);
  168. pointer = pointer->next;
  169. }
  170. }
  171. pointer->next = stream->buffer;
  172. spin_lock_irqsave(&stream->dma_lock, flags);
  173. init_dma(stream->dma);
  174. if (get_dma_active_buffer(stream->dma) == 0) {
  175. clear_dma_done0(stream->dma);
  176. set_dma_addr0(stream->dma, stream->buffer->start);
  177. set_dma_count0(stream->dma, stream->period_size >> 1);
  178. set_dma_addr1(stream->dma, stream->buffer->next->start);
  179. set_dma_count1(stream->dma, stream->period_size >> 1);
  180. } else {
  181. clear_dma_done1(stream->dma);
  182. set_dma_addr1(stream->dma, stream->buffer->start);
  183. set_dma_count1(stream->dma, stream->period_size >> 1);
  184. set_dma_addr0(stream->dma, stream->buffer->next->start);
  185. set_dma_count0(stream->dma, stream->period_size >> 1);
  186. }
  187. enable_dma_buffers(stream->dma);
  188. start_dma(stream->dma);
  189. spin_unlock_irqrestore(&stream->dma_lock, flags);
  190. }
  191. }
  192. static irqreturn_t
  193. au1000_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  194. {
  195. audio_stream_t *stream = (audio_stream_t *) dev_id;
  196. snd_pcm_substream_t *substream = stream->substream;
  197. spin_lock(&stream->dma_lock);
  198. switch (get_dma_buffer_done(stream->dma)) {
  199. case DMA_D0:
  200. stream->buffer = stream->buffer->next;
  201. clear_dma_done0(stream->dma);
  202. set_dma_addr0(stream->dma, stream->buffer->next->start);
  203. set_dma_count0(stream->dma, stream->period_size >> 1);
  204. enable_dma_buffer0(stream->dma);
  205. break;
  206. case DMA_D1:
  207. stream->buffer = stream->buffer->next;
  208. clear_dma_done1(stream->dma);
  209. set_dma_addr1(stream->dma, stream->buffer->next->start);
  210. set_dma_count1(stream->dma, stream->period_size >> 1);
  211. enable_dma_buffer1(stream->dma);
  212. break;
  213. case (DMA_D0 | DMA_D1):
  214. spin_unlock(&stream->dma_lock);
  215. printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma);
  216. au1000_dma_stop(stream);
  217. au1000_dma_start(stream);
  218. spin_lock(&stream->dma_lock);
  219. break;
  220. case (~DMA_D0 & ~DMA_D1):
  221. printk(KERN_ERR "DMA %d empty irq.\n",stream->dma);
  222. }
  223. spin_unlock(&stream->dma_lock);
  224. snd_pcm_period_elapsed(substream);
  225. return IRQ_HANDLED;
  226. }
  227. /*-------------------------- PCM Audio Streams -------------------------------*/
  228. static unsigned int rates[] = {8000, 11025, 16000, 22050};
  229. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  230. .count = sizeof(rates) / sizeof(rates[0]),
  231. .list = rates,
  232. .mask = 0,
  233. };
  234. static snd_pcm_hardware_t snd_au1000 =
  235. {
  236. .info = (SNDRV_PCM_INFO_INTERLEAVED | \
  237. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  238. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  239. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
  240. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050),
  241. .rate_min = 8000,
  242. .rate_max = 22050,
  243. .channels_min = 1,
  244. .channels_max = 2,
  245. .buffer_bytes_max = 128*1024,
  246. .period_bytes_min = 32,
  247. .period_bytes_max = 16*1024,
  248. .periods_min = 8,
  249. .periods_max = 255,
  250. .fifo_size = 16,
  251. };
  252. static int
  253. snd_au1000_playback_open(snd_pcm_substream_t * substream)
  254. {
  255. au1000->stream[PLAYBACK]->substream = substream;
  256. au1000->stream[PLAYBACK]->buffer = NULL;
  257. substream->private_data = au1000->stream[PLAYBACK];
  258. substream->runtime->hw = snd_au1000;
  259. return (snd_pcm_hw_constraint_list(substream->runtime, 0,
  260. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
  261. }
  262. static int
  263. snd_au1000_capture_open(snd_pcm_substream_t * substream)
  264. {
  265. au1000->stream[CAPTURE]->substream = substream;
  266. au1000->stream[CAPTURE]->buffer = NULL;
  267. substream->private_data = au1000->stream[CAPTURE];
  268. substream->runtime->hw = snd_au1000;
  269. return (snd_pcm_hw_constraint_list(substream->runtime, 0,
  270. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
  271. }
  272. static int
  273. snd_au1000_playback_close(snd_pcm_substream_t * substream)
  274. {
  275. au1000->stream[PLAYBACK]->substream = NULL;
  276. return 0;
  277. }
  278. static int
  279. snd_au1000_capture_close(snd_pcm_substream_t * substream)
  280. {
  281. au1000->stream[CAPTURE]->substream = NULL;
  282. return 0;
  283. }
  284. static int
  285. snd_au1000_hw_params(snd_pcm_substream_t * substream,
  286. snd_pcm_hw_params_t * hw_params)
  287. {
  288. return snd_pcm_lib_malloc_pages(substream,
  289. params_buffer_bytes(hw_params));
  290. }
  291. static int
  292. snd_au1000_hw_free(snd_pcm_substream_t * substream)
  293. {
  294. return snd_pcm_lib_free_pages(substream);
  295. }
  296. static int
  297. snd_au1000_playback_prepare(snd_pcm_substream_t * substream)
  298. {
  299. snd_pcm_runtime_t *runtime = substream->runtime;
  300. if (runtime->channels == 1 )
  301. au1000_set_ac97_xmit_slots(AC97_SLOT_4);
  302. else
  303. au1000_set_ac97_xmit_slots(AC97_SLOT_3 | AC97_SLOT_4);
  304. snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  305. return 0;
  306. }
  307. static int
  308. snd_au1000_capture_prepare(snd_pcm_substream_t * substream)
  309. {
  310. snd_pcm_runtime_t *runtime = substream->runtime;
  311. if (runtime->channels == 1 )
  312. au1000_set_ac97_recv_slots(AC97_SLOT_4);
  313. else
  314. au1000_set_ac97_recv_slots(AC97_SLOT_3 | AC97_SLOT_4);
  315. snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  316. return 0;
  317. }
  318. static int
  319. snd_au1000_trigger(snd_pcm_substream_t * substream, int cmd)
  320. {
  321. audio_stream_t *stream = substream->private_data;
  322. int err = 0;
  323. switch (cmd) {
  324. case SNDRV_PCM_TRIGGER_START:
  325. au1000_dma_start(stream);
  326. break;
  327. case SNDRV_PCM_TRIGGER_STOP:
  328. au1000_dma_stop(stream);
  329. break;
  330. default:
  331. err = -EINVAL;
  332. break;
  333. }
  334. return err;
  335. }
  336. static snd_pcm_uframes_t
  337. snd_au1000_pointer(snd_pcm_substream_t * substream)
  338. {
  339. audio_stream_t *stream = substream->private_data;
  340. snd_pcm_runtime_t *runtime = substream->runtime;
  341. unsigned long flags;
  342. long location;
  343. spin_lock_irqsave(&stream->dma_lock, flags);
  344. location = get_dma_residue(stream->dma);
  345. spin_unlock_irqrestore(&stream->dma_lock, flags);
  346. location = stream->buffer->relative_end - location;
  347. if (location == -1)
  348. location = 0;
  349. return bytes_to_frames(runtime,location);
  350. }
  351. static snd_pcm_ops_t snd_card_au1000_playback_ops = {
  352. .open = snd_au1000_playback_open,
  353. .close = snd_au1000_playback_close,
  354. .ioctl = snd_pcm_lib_ioctl,
  355. .hw_params = snd_au1000_hw_params,
  356. .hw_free = snd_au1000_hw_free,
  357. .prepare = snd_au1000_playback_prepare,
  358. .trigger = snd_au1000_trigger,
  359. .pointer = snd_au1000_pointer,
  360. };
  361. static snd_pcm_ops_t snd_card_au1000_capture_ops = {
  362. .open = snd_au1000_capture_open,
  363. .close = snd_au1000_capture_close,
  364. .ioctl = snd_pcm_lib_ioctl,
  365. .hw_params = snd_au1000_hw_params,
  366. .hw_free = snd_au1000_hw_free,
  367. .prepare = snd_au1000_capture_prepare,
  368. .trigger = snd_au1000_trigger,
  369. .pointer = snd_au1000_pointer,
  370. };
  371. static int __devinit
  372. snd_au1000_pcm_new(void)
  373. {
  374. snd_pcm_t *pcm;
  375. int err;
  376. unsigned long flags;
  377. if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0)
  378. return err;
  379. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  380. snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024);
  381. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  382. &snd_card_au1000_playback_ops);
  383. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  384. &snd_card_au1000_capture_ops);
  385. pcm->private_data = au1000;
  386. pcm->info_flags = 0;
  387. strcpy(pcm->name, "Au1000 AC97 PCM");
  388. flags = claim_dma_lock();
  389. if ((au1000->stream[PLAYBACK]->dma = request_au1000_dma(DMA_ID_AC97C_TX,
  390. "AC97 TX", au1000_dma_interrupt, SA_INTERRUPT,
  391. au1000->stream[PLAYBACK])) < 0) {
  392. release_dma_lock(flags);
  393. return -EBUSY;
  394. }
  395. if ((au1000->stream[CAPTURE]->dma = request_au1000_dma(DMA_ID_AC97C_RX,
  396. "AC97 RX", au1000_dma_interrupt, SA_INTERRUPT,
  397. au1000->stream[CAPTURE])) < 0){
  398. release_dma_lock(flags);
  399. return -EBUSY;
  400. }
  401. /* enable DMA coherency in read/write DMA channels */
  402. set_dma_mode(au1000->stream[PLAYBACK]->dma,
  403. get_dma_mode(au1000->stream[PLAYBACK]->dma) & ~DMA_NC);
  404. set_dma_mode(au1000->stream[CAPTURE]->dma,
  405. get_dma_mode(au1000->stream[CAPTURE]->dma) & ~DMA_NC);
  406. release_dma_lock(flags);
  407. spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock);
  408. spin_lock_init(&au1000->stream[CAPTURE]->dma_lock);
  409. au1000->pcm = pcm;
  410. return 0;
  411. }
  412. /*-------------------------- AC97 CODEC Control ------------------------------*/
  413. static unsigned short
  414. snd_au1000_ac97_read(ac97_t *ac97, unsigned short reg)
  415. {
  416. u32 volatile cmd;
  417. u16 volatile data;
  418. int i;
  419. spin_lock(au1000->ac97_lock);
  420. /* would rather use the interupt than this polling but it works and I can't
  421. get the interupt driven case to work efficiently */
  422. for (i = 0; i < 0x5000; i++)
  423. if (!(au1000->ac97_ioport->status & AC97C_CP))
  424. break;
  425. if (i == 0x5000)
  426. printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
  427. cmd = (u32) reg & AC97C_INDEX_MASK;
  428. cmd |= AC97C_READ;
  429. au1000->ac97_ioport->cmd = cmd;
  430. /* now wait for the data */
  431. for (i = 0; i < 0x5000; i++)
  432. if (!(au1000->ac97_ioport->status & AC97C_CP))
  433. break;
  434. if (i == 0x5000) {
  435. printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
  436. return 0;
  437. }
  438. data = au1000->ac97_ioport->cmd & 0xffff;
  439. spin_unlock(au1000->ac97_lock);
  440. return data;
  441. }
  442. static void
  443. snd_au1000_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
  444. {
  445. u32 cmd;
  446. int i;
  447. spin_lock(au1000->ac97_lock);
  448. /* would rather use the interupt than this polling but it works and I can't
  449. get the interupt driven case to work efficiently */
  450. for (i = 0; i < 0x5000; i++)
  451. if (!(au1000->ac97_ioport->status & AC97C_CP))
  452. break;
  453. if (i == 0x5000)
  454. printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n");
  455. cmd = (u32) reg & AC97C_INDEX_MASK;
  456. cmd &= ~AC97C_READ;
  457. cmd |= ((u32) val << AC97C_WD_BIT);
  458. au1000->ac97_ioport->cmd = cmd;
  459. spin_unlock(au1000->ac97_lock);
  460. }
  461. static void
  462. snd_au1000_ac97_free(ac97_t *ac97)
  463. {
  464. au1000->ac97 = NULL;
  465. }
  466. static int __devinit
  467. snd_au1000_ac97_new(void)
  468. {
  469. int err;
  470. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
  471. ac97_bus_t *pbus;
  472. ac97_template_t ac97;
  473. static ac97_bus_ops_t ops = {
  474. .write = snd_au1000_ac97_write,
  475. .read = snd_au1000_ac97_read,
  476. };
  477. #else
  478. ac97_bus_t bus, *pbus;
  479. ac97_t ac97;
  480. #endif
  481. if ((au1000->ac97_res_port = request_region(AC97C_CONFIG,
  482. sizeof(au1000_ac97_reg_t), "Au1x00 AC97")) == NULL) {
  483. snd_printk(KERN_ERR "ALSA AC97: can't grap AC97 port\n");
  484. return -EBUSY;
  485. }
  486. au1000->ac97_ioport = (au1000_ac97_reg_t *) au1000->ac97_res_port->start;
  487. spin_lock_init(&au1000->ac97_lock);
  488. spin_lock(&au1000->ac97_lock);
  489. /* configure pins for AC'97
  490. TODO: move to board_setup.c */
  491. au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
  492. /* Initialise Au1000's AC'97 Control Block */
  493. au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE;
  494. udelay(10);
  495. au1000->ac97_ioport->cntrl = AC97C_CE;
  496. udelay(10);
  497. /* Initialise External CODEC -- cold reset */
  498. au1000->ac97_ioport->config = AC97C_RESET;
  499. udelay(10);
  500. au1000->ac97_ioport->config = 0x0;
  501. mdelay(5);
  502. spin_unlock(&au1000->ac97_lock);
  503. /* Initialise AC97 middle-layer */
  504. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,8)
  505. if ((err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus)) < 0)
  506. return err;
  507. #else
  508. memset(&bus, 0, sizeof(bus));
  509. bus.write = snd_au1000_ac97_write;
  510. bus.read = snd_au1000_ac97_read;
  511. if ((err = snd_ac97_bus(au1000->card, &bus, &pbus)) < 0)
  512. return err;
  513. #endif
  514. memset(&ac97, 0, sizeof(ac97));
  515. ac97.private_data = au1000;
  516. ac97.private_free = snd_au1000_ac97_free;
  517. if ((err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97)) < 0)
  518. return err;
  519. return 0;
  520. }
  521. /*------------------------------ Setup / Destroy ----------------------------*/
  522. void
  523. snd_au1000_free(snd_card_t *card)
  524. {
  525. if (au1000->ac97_res_port) {
  526. /* put internal AC97 block into reset */
  527. au1000->ac97_ioport->cntrl = AC97C_RS;
  528. au1000->ac97_ioport = NULL;
  529. release_resource(au1000->ac97_res_port);
  530. kfree_nocheck(au1000->ac97_res_port);
  531. }
  532. if (au1000->stream[PLAYBACK]->dma >= 0)
  533. free_au1000_dma(au1000->stream[PLAYBACK]->dma);
  534. if (au1000->stream[CAPTURE]->dma >= 0)
  535. free_au1000_dma(au1000->stream[CAPTURE]->dma);
  536. kfree(au1000->stream[PLAYBACK]);
  537. au1000->stream[PLAYBACK] = NULL;
  538. kfree(au1000->stream[CAPTURE]);
  539. au1000->stream[CAPTURE] = NULL;
  540. kfree(au1000);
  541. au1000 = NULL;
  542. }
  543. static int __init
  544. au1000_init(void)
  545. {
  546. int err;
  547. au1000 = kmalloc(sizeof(au1000_t), GFP_KERNEL);
  548. if (au1000 == NULL)
  549. return -ENOMEM;
  550. au1000->stream[PLAYBACK] = kmalloc(sizeof(audio_stream_t), GFP_KERNEL);
  551. if (au1000->stream[PLAYBACK] == NULL)
  552. return -ENOMEM;
  553. au1000->stream[CAPTURE] = kmalloc(sizeof(audio_stream_t), GFP_KERNEL);
  554. if (au1000->stream[CAPTURE] == NULL)
  555. return -ENOMEM;
  556. /* so that snd_au1000_free will work as intended */
  557. au1000->stream[PLAYBACK]->dma = -1;
  558. au1000->stream[CAPTURE]->dma = -1;
  559. au1000->ac97_res_port = NULL;
  560. au1000->card = snd_card_new(-1, "AC97", THIS_MODULE, sizeof(au1000_t));
  561. if (au1000->card == NULL) {
  562. snd_au1000_free(au1000->card);
  563. return -ENOMEM;
  564. }
  565. au1000->card->private_data = (au1000_t *)au1000;
  566. au1000->card->private_free = snd_au1000_free;
  567. if ((err = snd_au1000_ac97_new()) < 0 ) {
  568. snd_card_free(au1000->card);
  569. return err;
  570. }
  571. if ((err = snd_au1000_pcm_new()) < 0) {
  572. snd_card_free(au1000->card);
  573. return err;
  574. }
  575. strcpy(au1000->card->driver, "AMD-Au1000-AC97");
  576. strcpy(au1000->card->shortname, "Au1000-AC97");
  577. sprintf(au1000->card->longname, "AMD Au1000--AC97 ALSA Driver");
  578. if ((err = snd_card_register(au1000->card)) < 0) {
  579. snd_card_free(au1000->card);
  580. return err;
  581. }
  582. printk( KERN_INFO "ALSA AC97: Driver Initialized\n" );
  583. return 0;
  584. }
  585. static void __exit au1000_exit(void)
  586. {
  587. snd_card_free(au1000->card);
  588. }
  589. module_init(au1000_init);
  590. module_exit(au1000_exit);