au1x00.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <sound/core.h>
  41. #include <sound/initval.h>
  42. #include <sound/pcm.h>
  43. #include <sound/pcm_params.h>
  44. #include <sound/ac97_codec.h>
  45. #include <asm/mach-au1x00/au1000.h>
  46. #include <asm/mach-au1x00/au1000_dma.h>
  47. MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>");
  48. MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver");
  49. MODULE_LICENSE("GPL");
  50. MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}");
  51. #define PLAYBACK 0
  52. #define CAPTURE 1
  53. #define AC97_SLOT_3 0x01
  54. #define AC97_SLOT_4 0x02
  55. #define AC97_SLOT_6 0x08
  56. #define AC97_CMD_IRQ 31
  57. #define READ 0
  58. #define WRITE 1
  59. #define READ_WAIT 2
  60. #define RW_DONE 3
  61. struct au1000_period
  62. {
  63. u32 start;
  64. u32 relative_end; /*realtive to start of buffer*/
  65. struct au1000_period * next;
  66. };
  67. /*Au1000 AC97 Port Control Reisters*/
  68. struct au1000_ac97_reg {
  69. u32 volatile config;
  70. u32 volatile status;
  71. u32 volatile data;
  72. u32 volatile cmd;
  73. u32 volatile cntrl;
  74. };
  75. struct audio_stream {
  76. struct snd_pcm_substream *substream;
  77. int dma;
  78. spinlock_t dma_lock;
  79. struct au1000_period * buffer;
  80. unsigned int period_size;
  81. unsigned int periods;
  82. };
  83. struct snd_au1000 {
  84. struct snd_card *card;
  85. struct au1000_ac97_reg volatile *ac97_ioport;
  86. struct resource *ac97_res_port;
  87. spinlock_t ac97_lock;
  88. struct snd_ac97 *ac97;
  89. struct snd_pcm *pcm;
  90. struct audio_stream *stream[2]; /* playback & capture */
  91. };
  92. /*--------------------------- Local Functions --------------------------------*/
  93. static void
  94. au1000_set_ac97_xmit_slots(struct snd_au1000 *au1000, long xmit_slots)
  95. {
  96. u32 volatile ac97_config;
  97. spin_lock(&au1000->ac97_lock);
  98. ac97_config = au1000->ac97_ioport->config;
  99. ac97_config = ac97_config & ~AC97C_XMIT_SLOTS_MASK;
  100. ac97_config |= (xmit_slots << AC97C_XMIT_SLOTS_BIT);
  101. au1000->ac97_ioport->config = ac97_config;
  102. spin_unlock(&au1000->ac97_lock);
  103. }
  104. static void
  105. au1000_set_ac97_recv_slots(struct snd_au1000 *au1000, long recv_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_RECV_SLOTS_MASK;
  111. ac97_config |= (recv_slots << AC97C_RECV_SLOTS_BIT);
  112. au1000->ac97_ioport->config = ac97_config;
  113. spin_unlock(&au1000->ac97_lock);
  114. }
  115. static void
  116. au1000_release_dma_link(struct audio_stream *stream)
  117. {
  118. struct au1000_period * pointer;
  119. struct au1000_period * pointer_next;
  120. stream->period_size = 0;
  121. stream->periods = 0;
  122. pointer = stream->buffer;
  123. if (! pointer)
  124. return;
  125. do {
  126. pointer_next = pointer->next;
  127. kfree(pointer);
  128. pointer = pointer_next;
  129. } while (pointer != stream->buffer);
  130. stream->buffer = NULL;
  131. }
  132. static int
  133. au1000_setup_dma_link(struct audio_stream *stream, unsigned int period_bytes,
  134. unsigned int periods)
  135. {
  136. struct snd_pcm_substream *substream = stream->substream;
  137. struct snd_pcm_runtime *runtime = substream->runtime;
  138. struct au1000_period *pointer;
  139. unsigned long dma_start;
  140. int i;
  141. dma_start = virt_to_phys(runtime->dma_area);
  142. if (stream->period_size == period_bytes &&
  143. stream->periods == periods)
  144. return 0; /* not changed */
  145. au1000_release_dma_link(stream);
  146. stream->period_size = period_bytes;
  147. stream->periods = periods;
  148. stream->buffer = kmalloc(sizeof(struct au1000_period), GFP_KERNEL);
  149. if (! stream->buffer)
  150. return -ENOMEM;
  151. pointer = stream->buffer;
  152. for (i = 0; i < periods; i++) {
  153. pointer->start = (u32)(dma_start + (i * period_bytes));
  154. pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1);
  155. if (i < periods - 1) {
  156. pointer->next = kmalloc(sizeof(struct au1000_period), GFP_KERNEL);
  157. if (! pointer->next) {
  158. au1000_release_dma_link(stream);
  159. return -ENOMEM;
  160. }
  161. pointer = pointer->next;
  162. }
  163. }
  164. pointer->next = stream->buffer;
  165. return 0;
  166. }
  167. static void
  168. au1000_dma_stop(struct audio_stream *stream)
  169. {
  170. if (snd_BUG_ON(!stream->buffer))
  171. return;
  172. disable_dma(stream->dma);
  173. }
  174. static void
  175. au1000_dma_start(struct audio_stream *stream)
  176. {
  177. if (snd_BUG_ON(!stream->buffer))
  178. return;
  179. init_dma(stream->dma);
  180. if (get_dma_active_buffer(stream->dma) == 0) {
  181. clear_dma_done0(stream->dma);
  182. set_dma_addr0(stream->dma, stream->buffer->start);
  183. set_dma_count0(stream->dma, stream->period_size >> 1);
  184. set_dma_addr1(stream->dma, stream->buffer->next->start);
  185. set_dma_count1(stream->dma, stream->period_size >> 1);
  186. } else {
  187. clear_dma_done1(stream->dma);
  188. set_dma_addr1(stream->dma, stream->buffer->start);
  189. set_dma_count1(stream->dma, stream->period_size >> 1);
  190. set_dma_addr0(stream->dma, stream->buffer->next->start);
  191. set_dma_count0(stream->dma, stream->period_size >> 1);
  192. }
  193. enable_dma_buffers(stream->dma);
  194. start_dma(stream->dma);
  195. }
  196. static irqreturn_t
  197. au1000_dma_interrupt(int irq, void *dev_id)
  198. {
  199. struct audio_stream *stream = (struct audio_stream *) dev_id;
  200. struct snd_pcm_substream *substream = stream->substream;
  201. spin_lock(&stream->dma_lock);
  202. switch (get_dma_buffer_done(stream->dma)) {
  203. case DMA_D0:
  204. stream->buffer = stream->buffer->next;
  205. clear_dma_done0(stream->dma);
  206. set_dma_addr0(stream->dma, stream->buffer->next->start);
  207. set_dma_count0(stream->dma, stream->period_size >> 1);
  208. enable_dma_buffer0(stream->dma);
  209. break;
  210. case DMA_D1:
  211. stream->buffer = stream->buffer->next;
  212. clear_dma_done1(stream->dma);
  213. set_dma_addr1(stream->dma, stream->buffer->next->start);
  214. set_dma_count1(stream->dma, stream->period_size >> 1);
  215. enable_dma_buffer1(stream->dma);
  216. break;
  217. case (DMA_D0 | DMA_D1):
  218. printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma);
  219. au1000_dma_stop(stream);
  220. au1000_dma_start(stream);
  221. break;
  222. case (~DMA_D0 & ~DMA_D1):
  223. printk(KERN_ERR "DMA %d empty irq.\n",stream->dma);
  224. }
  225. spin_unlock(&stream->dma_lock);
  226. snd_pcm_period_elapsed(substream);
  227. return IRQ_HANDLED;
  228. }
  229. /*-------------------------- PCM Audio Streams -------------------------------*/
  230. static unsigned int rates[] = {8000, 11025, 16000, 22050};
  231. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  232. .count = ARRAY_SIZE(rates),
  233. .list = rates,
  234. .mask = 0,
  235. };
  236. static struct snd_pcm_hardware snd_au1000_hw =
  237. {
  238. .info = (SNDRV_PCM_INFO_INTERLEAVED | \
  239. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  240. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  241. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
  242. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050),
  243. .rate_min = 8000,
  244. .rate_max = 22050,
  245. .channels_min = 1,
  246. .channels_max = 2,
  247. .buffer_bytes_max = 128*1024,
  248. .period_bytes_min = 32,
  249. .period_bytes_max = 16*1024,
  250. .periods_min = 8,
  251. .periods_max = 255,
  252. .fifo_size = 16,
  253. };
  254. static int
  255. snd_au1000_playback_open(struct snd_pcm_substream *substream)
  256. {
  257. struct snd_au1000 *au1000 = substream->pcm->private_data;
  258. au1000->stream[PLAYBACK]->substream = substream;
  259. au1000->stream[PLAYBACK]->buffer = NULL;
  260. substream->private_data = au1000->stream[PLAYBACK];
  261. substream->runtime->hw = snd_au1000_hw;
  262. return (snd_pcm_hw_constraint_list(substream->runtime, 0,
  263. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
  264. }
  265. static int
  266. snd_au1000_capture_open(struct snd_pcm_substream *substream)
  267. {
  268. struct snd_au1000 *au1000 = substream->pcm->private_data;
  269. au1000->stream[CAPTURE]->substream = substream;
  270. au1000->stream[CAPTURE]->buffer = NULL;
  271. substream->private_data = au1000->stream[CAPTURE];
  272. substream->runtime->hw = snd_au1000_hw;
  273. return (snd_pcm_hw_constraint_list(substream->runtime, 0,
  274. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0);
  275. }
  276. static int
  277. snd_au1000_playback_close(struct snd_pcm_substream *substream)
  278. {
  279. struct snd_au1000 *au1000 = substream->pcm->private_data;
  280. au1000->stream[PLAYBACK]->substream = NULL;
  281. return 0;
  282. }
  283. static int
  284. snd_au1000_capture_close(struct snd_pcm_substream *substream)
  285. {
  286. struct snd_au1000 *au1000 = substream->pcm->private_data;
  287. au1000->stream[CAPTURE]->substream = NULL;
  288. return 0;
  289. }
  290. static int
  291. snd_au1000_hw_params(struct snd_pcm_substream *substream,
  292. struct snd_pcm_hw_params *hw_params)
  293. {
  294. struct audio_stream *stream = substream->private_data;
  295. int err;
  296. err = snd_pcm_lib_malloc_pages(substream,
  297. params_buffer_bytes(hw_params));
  298. if (err < 0)
  299. return err;
  300. return au1000_setup_dma_link(stream,
  301. params_period_bytes(hw_params),
  302. params_periods(hw_params));
  303. }
  304. static int
  305. snd_au1000_hw_free(struct snd_pcm_substream *substream)
  306. {
  307. struct audio_stream *stream = substream->private_data;
  308. au1000_release_dma_link(stream);
  309. return snd_pcm_lib_free_pages(substream);
  310. }
  311. static int
  312. snd_au1000_playback_prepare(struct snd_pcm_substream *substream)
  313. {
  314. struct snd_au1000 *au1000 = substream->pcm->private_data;
  315. struct snd_pcm_runtime *runtime = substream->runtime;
  316. if (runtime->channels == 1)
  317. au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_4);
  318. else
  319. au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
  320. snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  321. return 0;
  322. }
  323. static int
  324. snd_au1000_capture_prepare(struct snd_pcm_substream *substream)
  325. {
  326. struct snd_au1000 *au1000 = substream->pcm->private_data;
  327. struct snd_pcm_runtime *runtime = substream->runtime;
  328. if (runtime->channels == 1)
  329. au1000_set_ac97_recv_slots(au1000, AC97_SLOT_4);
  330. else
  331. au1000_set_ac97_recv_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4);
  332. snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  333. return 0;
  334. }
  335. static int
  336. snd_au1000_trigger(struct snd_pcm_substream *substream, int cmd)
  337. {
  338. struct audio_stream *stream = substream->private_data;
  339. int err = 0;
  340. spin_lock(&stream->dma_lock);
  341. switch (cmd) {
  342. case SNDRV_PCM_TRIGGER_START:
  343. au1000_dma_start(stream);
  344. break;
  345. case SNDRV_PCM_TRIGGER_STOP:
  346. au1000_dma_stop(stream);
  347. break;
  348. default:
  349. err = -EINVAL;
  350. break;
  351. }
  352. spin_unlock(&stream->dma_lock);
  353. return err;
  354. }
  355. static snd_pcm_uframes_t
  356. snd_au1000_pointer(struct snd_pcm_substream *substream)
  357. {
  358. struct audio_stream *stream = substream->private_data;
  359. struct snd_pcm_runtime *runtime = substream->runtime;
  360. long location;
  361. spin_lock(&stream->dma_lock);
  362. location = get_dma_residue(stream->dma);
  363. spin_unlock(&stream->dma_lock);
  364. location = stream->buffer->relative_end - location;
  365. if (location == -1)
  366. location = 0;
  367. return bytes_to_frames(runtime,location);
  368. }
  369. static struct snd_pcm_ops snd_card_au1000_playback_ops = {
  370. .open = snd_au1000_playback_open,
  371. .close = snd_au1000_playback_close,
  372. .ioctl = snd_pcm_lib_ioctl,
  373. .hw_params = snd_au1000_hw_params,
  374. .hw_free = snd_au1000_hw_free,
  375. .prepare = snd_au1000_playback_prepare,
  376. .trigger = snd_au1000_trigger,
  377. .pointer = snd_au1000_pointer,
  378. };
  379. static struct snd_pcm_ops snd_card_au1000_capture_ops = {
  380. .open = snd_au1000_capture_open,
  381. .close = snd_au1000_capture_close,
  382. .ioctl = snd_pcm_lib_ioctl,
  383. .hw_params = snd_au1000_hw_params,
  384. .hw_free = snd_au1000_hw_free,
  385. .prepare = snd_au1000_capture_prepare,
  386. .trigger = snd_au1000_trigger,
  387. .pointer = snd_au1000_pointer,
  388. };
  389. static int __devinit
  390. snd_au1000_pcm_new(struct snd_au1000 *au1000)
  391. {
  392. struct snd_pcm *pcm;
  393. int err;
  394. unsigned long flags;
  395. if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0)
  396. return err;
  397. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  398. snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024);
  399. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  400. &snd_card_au1000_playback_ops);
  401. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  402. &snd_card_au1000_capture_ops);
  403. pcm->private_data = au1000;
  404. pcm->info_flags = 0;
  405. strcpy(pcm->name, "Au1000 AC97 PCM");
  406. spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock);
  407. spin_lock_init(&au1000->stream[CAPTURE]->dma_lock);
  408. flags = claim_dma_lock();
  409. if ((au1000->stream[PLAYBACK]->dma = request_au1000_dma(DMA_ID_AC97C_TX,
  410. "AC97 TX", au1000_dma_interrupt, IRQF_DISABLED,
  411. au1000->stream[PLAYBACK])) < 0) {
  412. release_dma_lock(flags);
  413. return -EBUSY;
  414. }
  415. if ((au1000->stream[CAPTURE]->dma = request_au1000_dma(DMA_ID_AC97C_RX,
  416. "AC97 RX", au1000_dma_interrupt, IRQF_DISABLED,
  417. au1000->stream[CAPTURE])) < 0){
  418. release_dma_lock(flags);
  419. return -EBUSY;
  420. }
  421. /* enable DMA coherency in read/write DMA channels */
  422. set_dma_mode(au1000->stream[PLAYBACK]->dma,
  423. get_dma_mode(au1000->stream[PLAYBACK]->dma) & ~DMA_NC);
  424. set_dma_mode(au1000->stream[CAPTURE]->dma,
  425. get_dma_mode(au1000->stream[CAPTURE]->dma) & ~DMA_NC);
  426. release_dma_lock(flags);
  427. au1000->pcm = pcm;
  428. return 0;
  429. }
  430. /*-------------------------- AC97 CODEC Control ------------------------------*/
  431. static unsigned short
  432. snd_au1000_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  433. {
  434. struct snd_au1000 *au1000 = ac97->private_data;
  435. u32 volatile cmd;
  436. u16 volatile data;
  437. int i;
  438. spin_lock(&au1000->ac97_lock);
  439. /* would rather use the interrupt than this polling but it works and I can't
  440. get the interrupt driven case to work efficiently */
  441. for (i = 0; i < 0x5000; i++)
  442. if (!(au1000->ac97_ioport->status & AC97C_CP))
  443. break;
  444. if (i == 0x5000)
  445. printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n");
  446. cmd = (u32) reg & AC97C_INDEX_MASK;
  447. cmd |= AC97C_READ;
  448. au1000->ac97_ioport->cmd = cmd;
  449. /* now wait for the data */
  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 read timeout\n");
  455. spin_unlock(&au1000->ac97_lock);
  456. return 0;
  457. }
  458. data = au1000->ac97_ioport->cmd & 0xffff;
  459. spin_unlock(&au1000->ac97_lock);
  460. return data;
  461. }
  462. static void
  463. snd_au1000_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
  464. {
  465. struct snd_au1000 *au1000 = ac97->private_data;
  466. u32 cmd;
  467. int i;
  468. spin_lock(&au1000->ac97_lock);
  469. /* would rather use the interrupt than this polling but it works and I can't
  470. get the interrupt driven case to work efficiently */
  471. for (i = 0; i < 0x5000; i++)
  472. if (!(au1000->ac97_ioport->status & AC97C_CP))
  473. break;
  474. if (i == 0x5000)
  475. printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n");
  476. cmd = (u32) reg & AC97C_INDEX_MASK;
  477. cmd &= ~AC97C_READ;
  478. cmd |= ((u32) val << AC97C_WD_BIT);
  479. au1000->ac97_ioport->cmd = cmd;
  480. spin_unlock(&au1000->ac97_lock);
  481. }
  482. static int __devinit
  483. snd_au1000_ac97_new(struct snd_au1000 *au1000)
  484. {
  485. int err;
  486. struct snd_ac97_bus *pbus;
  487. struct snd_ac97_template ac97;
  488. static struct snd_ac97_bus_ops ops = {
  489. .write = snd_au1000_ac97_write,
  490. .read = snd_au1000_ac97_read,
  491. };
  492. if ((au1000->ac97_res_port = request_mem_region(CPHYSADDR(AC97C_CONFIG),
  493. 0x100000, "Au1x00 AC97")) == NULL) {
  494. snd_printk(KERN_ERR "ALSA AC97: can't grap AC97 port\n");
  495. return -EBUSY;
  496. }
  497. au1000->ac97_ioport = (struct au1000_ac97_reg *)
  498. KSEG1ADDR(au1000->ac97_res_port->start);
  499. spin_lock_init(&au1000->ac97_lock);
  500. /* configure pins for AC'97
  501. TODO: move to board_setup.c */
  502. au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
  503. /* Initialise Au1000's AC'97 Control Block */
  504. au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE;
  505. udelay(10);
  506. au1000->ac97_ioport->cntrl = AC97C_CE;
  507. udelay(10);
  508. /* Initialise External CODEC -- cold reset */
  509. au1000->ac97_ioport->config = AC97C_RESET;
  510. udelay(10);
  511. au1000->ac97_ioport->config = 0x0;
  512. mdelay(5);
  513. /* Initialise AC97 middle-layer */
  514. if ((err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus)) < 0)
  515. return err;
  516. memset(&ac97, 0, sizeof(ac97));
  517. ac97.private_data = au1000;
  518. if ((err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97)) < 0)
  519. return err;
  520. return 0;
  521. }
  522. /*------------------------------ Setup / Destroy ----------------------------*/
  523. void
  524. snd_au1000_free(struct snd_card *card)
  525. {
  526. struct snd_au1000 *au1000 = card->private_data;
  527. if (au1000->ac97_res_port) {
  528. /* put internal AC97 block into reset */
  529. au1000->ac97_ioport->cntrl = AC97C_RS;
  530. au1000->ac97_ioport = NULL;
  531. release_and_free_resource(au1000->ac97_res_port);
  532. }
  533. if (au1000->stream[PLAYBACK]) {
  534. if (au1000->stream[PLAYBACK]->dma >= 0)
  535. free_au1000_dma(au1000->stream[PLAYBACK]->dma);
  536. kfree(au1000->stream[PLAYBACK]);
  537. }
  538. if (au1000->stream[CAPTURE]) {
  539. if (au1000->stream[CAPTURE]->dma >= 0)
  540. free_au1000_dma(au1000->stream[CAPTURE]->dma);
  541. kfree(au1000->stream[CAPTURE]);
  542. }
  543. }
  544. static struct snd_card *au1000_card;
  545. static int __init
  546. au1000_init(void)
  547. {
  548. int err;
  549. struct snd_card *card;
  550. struct snd_au1000 *au1000;
  551. err = snd_card_create(-1, "AC97", THIS_MODULE,
  552. sizeof(struct snd_au1000), &card);
  553. if (err < 0)
  554. return err;
  555. card->private_free = snd_au1000_free;
  556. au1000 = card->private_data;
  557. au1000->card = card;
  558. au1000->stream[PLAYBACK] = kmalloc(sizeof(struct audio_stream), GFP_KERNEL);
  559. au1000->stream[CAPTURE ] = kmalloc(sizeof(struct audio_stream), GFP_KERNEL);
  560. /* so that snd_au1000_free will work as intended */
  561. au1000->ac97_res_port = NULL;
  562. if (au1000->stream[PLAYBACK])
  563. au1000->stream[PLAYBACK]->dma = -1;
  564. if (au1000->stream[CAPTURE ])
  565. au1000->stream[CAPTURE ]->dma = -1;
  566. if (au1000->stream[PLAYBACK] == NULL ||
  567. au1000->stream[CAPTURE ] == NULL) {
  568. snd_card_free(card);
  569. return -ENOMEM;
  570. }
  571. if ((err = snd_au1000_ac97_new(au1000)) < 0 ) {
  572. snd_card_free(card);
  573. return err;
  574. }
  575. if ((err = snd_au1000_pcm_new(au1000)) < 0) {
  576. snd_card_free(card);
  577. return err;
  578. }
  579. strcpy(card->driver, "Au1000-AC97");
  580. strcpy(card->shortname, "AMD Au1000-AC97");
  581. sprintf(card->longname, "AMD Au1000--AC97 ALSA Driver");
  582. if ((err = snd_card_register(card)) < 0) {
  583. snd_card_free(card);
  584. return err;
  585. }
  586. printk(KERN_INFO "ALSA AC97: Driver Initialized\n");
  587. au1000_card = card;
  588. return 0;
  589. }
  590. static void __exit au1000_exit(void)
  591. {
  592. snd_card_free(au1000_card);
  593. }
  594. module_init(au1000_init);
  595. module_exit(au1000_exit);