au1x00.c 20 KB

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