atiixp.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * ALSA driver for ATI IXP 150/200/250/300 AC97 controllers
  3. *
  4. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include <linux/moduleparam.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/info.h>
  33. #include <sound/ac97_codec.h>
  34. #include <sound/initval.h>
  35. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  36. MODULE_DESCRIPTION("ATI IXP AC97 controller");
  37. MODULE_LICENSE("GPL");
  38. MODULE_SUPPORTED_DEVICE("{{ATI,IXP150/200/250/300/400}}");
  39. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  40. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  41. static int ac97_clock = 48000;
  42. static char *ac97_quirk;
  43. static int spdif_aclink = 1;
  44. module_param(index, int, 0444);
  45. MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
  46. module_param(id, charp, 0444);
  47. MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
  48. module_param(ac97_clock, int, 0444);
  49. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  50. module_param(ac97_quirk, charp, 0444);
  51. MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  52. module_param(spdif_aclink, bool, 0444);
  53. MODULE_PARM_DESC(spdif_aclink, "S/PDIF over AC-link.");
  54. /* just for backward compatibility */
  55. static int enable;
  56. module_param(enable, bool, 0444);
  57. /*
  58. */
  59. #define ATI_REG_ISR 0x00 /* interrupt source */
  60. #define ATI_REG_ISR_IN_XRUN (1U<<0)
  61. #define ATI_REG_ISR_IN_STATUS (1U<<1)
  62. #define ATI_REG_ISR_OUT_XRUN (1U<<2)
  63. #define ATI_REG_ISR_OUT_STATUS (1U<<3)
  64. #define ATI_REG_ISR_SPDF_XRUN (1U<<4)
  65. #define ATI_REG_ISR_SPDF_STATUS (1U<<5)
  66. #define ATI_REG_ISR_PHYS_INTR (1U<<8)
  67. #define ATI_REG_ISR_PHYS_MISMATCH (1U<<9)
  68. #define ATI_REG_ISR_CODEC0_NOT_READY (1U<<10)
  69. #define ATI_REG_ISR_CODEC1_NOT_READY (1U<<11)
  70. #define ATI_REG_ISR_CODEC2_NOT_READY (1U<<12)
  71. #define ATI_REG_ISR_NEW_FRAME (1U<<13)
  72. #define ATI_REG_IER 0x04 /* interrupt enable */
  73. #define ATI_REG_IER_IN_XRUN_EN (1U<<0)
  74. #define ATI_REG_IER_IO_STATUS_EN (1U<<1)
  75. #define ATI_REG_IER_OUT_XRUN_EN (1U<<2)
  76. #define ATI_REG_IER_OUT_XRUN_COND (1U<<3)
  77. #define ATI_REG_IER_SPDF_XRUN_EN (1U<<4)
  78. #define ATI_REG_IER_SPDF_STATUS_EN (1U<<5)
  79. #define ATI_REG_IER_PHYS_INTR_EN (1U<<8)
  80. #define ATI_REG_IER_PHYS_MISMATCH_EN (1U<<9)
  81. #define ATI_REG_IER_CODEC0_INTR_EN (1U<<10)
  82. #define ATI_REG_IER_CODEC1_INTR_EN (1U<<11)
  83. #define ATI_REG_IER_CODEC2_INTR_EN (1U<<12)
  84. #define ATI_REG_IER_NEW_FRAME_EN (1U<<13) /* (RO */
  85. #define ATI_REG_IER_SET_BUS_BUSY (1U<<14) /* (WO) audio is running */
  86. #define ATI_REG_CMD 0x08 /* command */
  87. #define ATI_REG_CMD_POWERDOWN (1U<<0)
  88. #define ATI_REG_CMD_RECEIVE_EN (1U<<1)
  89. #define ATI_REG_CMD_SEND_EN (1U<<2)
  90. #define ATI_REG_CMD_STATUS_MEM (1U<<3)
  91. #define ATI_REG_CMD_SPDF_OUT_EN (1U<<4)
  92. #define ATI_REG_CMD_SPDF_STATUS_MEM (1U<<5)
  93. #define ATI_REG_CMD_SPDF_THRESHOLD (3U<<6)
  94. #define ATI_REG_CMD_SPDF_THRESHOLD_SHIFT 6
  95. #define ATI_REG_CMD_IN_DMA_EN (1U<<8)
  96. #define ATI_REG_CMD_OUT_DMA_EN (1U<<9)
  97. #define ATI_REG_CMD_SPDF_DMA_EN (1U<<10)
  98. #define ATI_REG_CMD_SPDF_OUT_STOPPED (1U<<11)
  99. #define ATI_REG_CMD_SPDF_CONFIG_MASK (7U<<12)
  100. #define ATI_REG_CMD_SPDF_CONFIG_34 (1U<<12)
  101. #define ATI_REG_CMD_SPDF_CONFIG_78 (2U<<12)
  102. #define ATI_REG_CMD_SPDF_CONFIG_69 (3U<<12)
  103. #define ATI_REG_CMD_SPDF_CONFIG_01 (4U<<12)
  104. #define ATI_REG_CMD_INTERLEAVE_SPDF (1U<<16)
  105. #define ATI_REG_CMD_AUDIO_PRESENT (1U<<20)
  106. #define ATI_REG_CMD_INTERLEAVE_IN (1U<<21)
  107. #define ATI_REG_CMD_INTERLEAVE_OUT (1U<<22)
  108. #define ATI_REG_CMD_LOOPBACK_EN (1U<<23)
  109. #define ATI_REG_CMD_PACKED_DIS (1U<<24)
  110. #define ATI_REG_CMD_BURST_EN (1U<<25)
  111. #define ATI_REG_CMD_PANIC_EN (1U<<26)
  112. #define ATI_REG_CMD_MODEM_PRESENT (1U<<27)
  113. #define ATI_REG_CMD_ACLINK_ACTIVE (1U<<28)
  114. #define ATI_REG_CMD_AC_SOFT_RESET (1U<<29)
  115. #define ATI_REG_CMD_AC_SYNC (1U<<30)
  116. #define ATI_REG_CMD_AC_RESET (1U<<31)
  117. #define ATI_REG_PHYS_OUT_ADDR 0x0c
  118. #define ATI_REG_PHYS_OUT_CODEC_MASK (3U<<0)
  119. #define ATI_REG_PHYS_OUT_RW (1U<<2)
  120. #define ATI_REG_PHYS_OUT_ADDR_EN (1U<<8)
  121. #define ATI_REG_PHYS_OUT_ADDR_SHIFT 9
  122. #define ATI_REG_PHYS_OUT_DATA_SHIFT 16
  123. #define ATI_REG_PHYS_IN_ADDR 0x10
  124. #define ATI_REG_PHYS_IN_READ_FLAG (1U<<8)
  125. #define ATI_REG_PHYS_IN_ADDR_SHIFT 9
  126. #define ATI_REG_PHYS_IN_DATA_SHIFT 16
  127. #define ATI_REG_SLOTREQ 0x14
  128. #define ATI_REG_COUNTER 0x18
  129. #define ATI_REG_COUNTER_SLOT (3U<<0) /* slot # */
  130. #define ATI_REG_COUNTER_BITCLOCK (31U<<8)
  131. #define ATI_REG_IN_FIFO_THRESHOLD 0x1c
  132. #define ATI_REG_IN_DMA_LINKPTR 0x20
  133. #define ATI_REG_IN_DMA_DT_START 0x24 /* RO */
  134. #define ATI_REG_IN_DMA_DT_NEXT 0x28 /* RO */
  135. #define ATI_REG_IN_DMA_DT_CUR 0x2c /* RO */
  136. #define ATI_REG_IN_DMA_DT_SIZE 0x30
  137. #define ATI_REG_OUT_DMA_SLOT 0x34
  138. #define ATI_REG_OUT_DMA_SLOT_BIT(x) (1U << ((x) - 3))
  139. #define ATI_REG_OUT_DMA_SLOT_MASK 0x1ff
  140. #define ATI_REG_OUT_DMA_THRESHOLD_MASK 0xf800
  141. #define ATI_REG_OUT_DMA_THRESHOLD_SHIFT 11
  142. #define ATI_REG_OUT_DMA_LINKPTR 0x38
  143. #define ATI_REG_OUT_DMA_DT_START 0x3c /* RO */
  144. #define ATI_REG_OUT_DMA_DT_NEXT 0x40 /* RO */
  145. #define ATI_REG_OUT_DMA_DT_CUR 0x44 /* RO */
  146. #define ATI_REG_OUT_DMA_DT_SIZE 0x48
  147. #define ATI_REG_SPDF_CMD 0x4c
  148. #define ATI_REG_SPDF_CMD_LFSR (1U<<4)
  149. #define ATI_REG_SPDF_CMD_SINGLE_CH (1U<<5)
  150. #define ATI_REG_SPDF_CMD_LFSR_ACC (0xff<<8) /* RO */
  151. #define ATI_REG_SPDF_DMA_LINKPTR 0x50
  152. #define ATI_REG_SPDF_DMA_DT_START 0x54 /* RO */
  153. #define ATI_REG_SPDF_DMA_DT_NEXT 0x58 /* RO */
  154. #define ATI_REG_SPDF_DMA_DT_CUR 0x5c /* RO */
  155. #define ATI_REG_SPDF_DMA_DT_SIZE 0x60
  156. #define ATI_REG_MODEM_MIRROR 0x7c
  157. #define ATI_REG_AUDIO_MIRROR 0x80
  158. #define ATI_REG_6CH_REORDER 0x84 /* reorder slots for 6ch */
  159. #define ATI_REG_6CH_REORDER_EN (1U<<0) /* 3,4,7,8,6,9 -> 3,4,6,9,7,8 */
  160. #define ATI_REG_FIFO_FLUSH 0x88
  161. #define ATI_REG_FIFO_OUT_FLUSH (1U<<0)
  162. #define ATI_REG_FIFO_IN_FLUSH (1U<<1)
  163. /* LINKPTR */
  164. #define ATI_REG_LINKPTR_EN (1U<<0)
  165. /* [INT|OUT|SPDIF]_DMA_DT_SIZE */
  166. #define ATI_REG_DMA_DT_SIZE (0xffffU<<0)
  167. #define ATI_REG_DMA_FIFO_USED (0x1fU<<16)
  168. #define ATI_REG_DMA_FIFO_FREE (0x1fU<<21)
  169. #define ATI_REG_DMA_STATE (7U<<26)
  170. #define ATI_MAX_DESCRIPTORS 256 /* max number of descriptor packets */
  171. /*
  172. */
  173. typedef struct snd_atiixp atiixp_t;
  174. typedef struct snd_atiixp_dma atiixp_dma_t;
  175. typedef struct snd_atiixp_dma_ops atiixp_dma_ops_t;
  176. /*
  177. * DMA packate descriptor
  178. */
  179. typedef struct atiixp_dma_desc {
  180. u32 addr; /* DMA buffer address */
  181. u16 status; /* status bits */
  182. u16 size; /* size of the packet in dwords */
  183. u32 next; /* address of the next packet descriptor */
  184. } atiixp_dma_desc_t;
  185. /*
  186. * stream enum
  187. */
  188. enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, ATI_DMA_SPDIF, NUM_ATI_DMAS }; /* DMAs */
  189. enum { ATI_PCM_OUT, ATI_PCM_IN, ATI_PCM_SPDIF, NUM_ATI_PCMS }; /* AC97 pcm slots */
  190. enum { ATI_PCMDEV_ANALOG, ATI_PCMDEV_DIGITAL, NUM_ATI_PCMDEVS }; /* pcm devices */
  191. #define NUM_ATI_CODECS 3
  192. /*
  193. * constants and callbacks for each DMA type
  194. */
  195. struct snd_atiixp_dma_ops {
  196. int type; /* ATI_DMA_XXX */
  197. unsigned int llp_offset; /* LINKPTR offset */
  198. unsigned int dt_cur; /* DT_CUR offset */
  199. void (*enable_dma)(atiixp_t *chip, int on); /* called from open callback */
  200. void (*enable_transfer)(atiixp_t *chip, int on); /* called from trigger (START/STOP) */
  201. void (*flush_dma)(atiixp_t *chip); /* called from trigger (STOP only) */
  202. };
  203. /*
  204. * DMA stream
  205. */
  206. struct snd_atiixp_dma {
  207. const atiixp_dma_ops_t *ops;
  208. struct snd_dma_buffer desc_buf;
  209. snd_pcm_substream_t *substream; /* assigned PCM substream */
  210. unsigned int buf_addr, buf_bytes; /* DMA buffer address, bytes */
  211. unsigned int period_bytes, periods;
  212. int opened;
  213. int running;
  214. int suspended;
  215. int pcm_open_flag;
  216. int ac97_pcm_type; /* index # of ac97_pcm to access, -1 = not used */
  217. unsigned int saved_curptr;
  218. };
  219. /*
  220. * ATI IXP chip
  221. */
  222. struct snd_atiixp {
  223. snd_card_t *card;
  224. struct pci_dev *pci;
  225. unsigned long addr;
  226. void __iomem *remap_addr;
  227. int irq;
  228. ac97_bus_t *ac97_bus;
  229. ac97_t *ac97[NUM_ATI_CODECS];
  230. spinlock_t reg_lock;
  231. atiixp_dma_t dmas[NUM_ATI_DMAS];
  232. struct ac97_pcm *pcms[NUM_ATI_PCMS];
  233. snd_pcm_t *pcmdevs[NUM_ATI_PCMDEVS];
  234. int max_channels; /* max. channels for PCM out */
  235. unsigned int codec_not_ready_bits; /* for codec detection */
  236. int spdif_over_aclink; /* passed from the module option */
  237. struct semaphore open_mutex; /* playback open mutex */
  238. };
  239. /*
  240. */
  241. static struct pci_device_id snd_atiixp_ids[] = {
  242. { 0x1002, 0x4341, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB200 */
  243. { 0x1002, 0x4361, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB300 */
  244. { 0x1002, 0x4370, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB400 */
  245. { 0, }
  246. };
  247. MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
  248. /*
  249. * lowlevel functions
  250. */
  251. /*
  252. * update the bits of the given register.
  253. * return 1 if the bits changed.
  254. */
  255. static int snd_atiixp_update_bits(atiixp_t *chip, unsigned int reg,
  256. unsigned int mask, unsigned int value)
  257. {
  258. void __iomem *addr = chip->remap_addr + reg;
  259. unsigned int data, old_data;
  260. old_data = data = readl(addr);
  261. data &= ~mask;
  262. data |= value;
  263. if (old_data == data)
  264. return 0;
  265. writel(data, addr);
  266. return 1;
  267. }
  268. /*
  269. * macros for easy use
  270. */
  271. #define atiixp_write(chip,reg,value) \
  272. writel(value, chip->remap_addr + ATI_REG_##reg)
  273. #define atiixp_read(chip,reg) \
  274. readl(chip->remap_addr + ATI_REG_##reg)
  275. #define atiixp_update(chip,reg,mask,val) \
  276. snd_atiixp_update_bits(chip, ATI_REG_##reg, mask, val)
  277. /* delay for one tick */
  278. #define do_delay() do { \
  279. schedule_timeout_uninterruptible(1); \
  280. } while (0)
  281. /*
  282. * handling DMA packets
  283. *
  284. * we allocate a linear buffer for the DMA, and split it to each packet.
  285. * in a future version, a scatter-gather buffer should be implemented.
  286. */
  287. #define ATI_DESC_LIST_SIZE \
  288. PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(atiixp_dma_desc_t))
  289. /*
  290. * build packets ring for the given buffer size.
  291. *
  292. * IXP handles the buffer descriptors, which are connected as a linked
  293. * list. although we can change the list dynamically, in this version,
  294. * a static RING of buffer descriptors is used.
  295. *
  296. * the ring is built in this function, and is set up to the hardware.
  297. */
  298. static int atiixp_build_dma_packets(atiixp_t *chip, atiixp_dma_t *dma,
  299. snd_pcm_substream_t *substream,
  300. unsigned int periods,
  301. unsigned int period_bytes)
  302. {
  303. unsigned int i;
  304. u32 addr, desc_addr;
  305. unsigned long flags;
  306. if (periods > ATI_MAX_DESCRIPTORS)
  307. return -ENOMEM;
  308. if (dma->desc_buf.area == NULL) {
  309. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  310. ATI_DESC_LIST_SIZE, &dma->desc_buf) < 0)
  311. return -ENOMEM;
  312. dma->period_bytes = dma->periods = 0; /* clear */
  313. }
  314. if (dma->periods == periods && dma->period_bytes == period_bytes)
  315. return 0;
  316. /* reset DMA before changing the descriptor table */
  317. spin_lock_irqsave(&chip->reg_lock, flags);
  318. writel(0, chip->remap_addr + dma->ops->llp_offset);
  319. dma->ops->enable_dma(chip, 0);
  320. dma->ops->enable_dma(chip, 1);
  321. spin_unlock_irqrestore(&chip->reg_lock, flags);
  322. /* fill the entries */
  323. addr = (u32)substream->runtime->dma_addr;
  324. desc_addr = (u32)dma->desc_buf.addr;
  325. for (i = 0; i < periods; i++) {
  326. atiixp_dma_desc_t *desc = &((atiixp_dma_desc_t *)dma->desc_buf.area)[i];
  327. desc->addr = cpu_to_le32(addr);
  328. desc->status = 0;
  329. desc->size = period_bytes >> 2; /* in dwords */
  330. desc_addr += sizeof(atiixp_dma_desc_t);
  331. if (i == periods - 1)
  332. desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
  333. else
  334. desc->next = cpu_to_le32(desc_addr);
  335. addr += period_bytes;
  336. }
  337. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  338. chip->remap_addr + dma->ops->llp_offset);
  339. dma->period_bytes = period_bytes;
  340. dma->periods = periods;
  341. return 0;
  342. }
  343. /*
  344. * remove the ring buffer and release it if assigned
  345. */
  346. static void atiixp_clear_dma_packets(atiixp_t *chip, atiixp_dma_t *dma, snd_pcm_substream_t *substream)
  347. {
  348. if (dma->desc_buf.area) {
  349. writel(0, chip->remap_addr + dma->ops->llp_offset);
  350. snd_dma_free_pages(&dma->desc_buf);
  351. dma->desc_buf.area = NULL;
  352. }
  353. }
  354. /*
  355. * AC97 interface
  356. */
  357. static int snd_atiixp_acquire_codec(atiixp_t *chip)
  358. {
  359. int timeout = 1000;
  360. while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
  361. if (! timeout--) {
  362. snd_printk(KERN_WARNING "atiixp: codec acquire timeout\n");
  363. return -EBUSY;
  364. }
  365. udelay(1);
  366. }
  367. return 0;
  368. }
  369. static unsigned short snd_atiixp_codec_read(atiixp_t *chip, unsigned short codec, unsigned short reg)
  370. {
  371. unsigned int data;
  372. int timeout;
  373. if (snd_atiixp_acquire_codec(chip) < 0)
  374. return 0xffff;
  375. data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  376. ATI_REG_PHYS_OUT_ADDR_EN |
  377. ATI_REG_PHYS_OUT_RW |
  378. codec;
  379. atiixp_write(chip, PHYS_OUT_ADDR, data);
  380. if (snd_atiixp_acquire_codec(chip) < 0)
  381. return 0xffff;
  382. timeout = 1000;
  383. do {
  384. data = atiixp_read(chip, PHYS_IN_ADDR);
  385. if (data & ATI_REG_PHYS_IN_READ_FLAG)
  386. return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
  387. udelay(1);
  388. } while (--timeout);
  389. /* time out may happen during reset */
  390. if (reg < 0x7c)
  391. snd_printk(KERN_WARNING "atiixp: codec read timeout (reg %x)\n", reg);
  392. return 0xffff;
  393. }
  394. static void snd_atiixp_codec_write(atiixp_t *chip, unsigned short codec, unsigned short reg, unsigned short val)
  395. {
  396. unsigned int data;
  397. if (snd_atiixp_acquire_codec(chip) < 0)
  398. return;
  399. data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
  400. ((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  401. ATI_REG_PHYS_OUT_ADDR_EN | codec;
  402. atiixp_write(chip, PHYS_OUT_ADDR, data);
  403. }
  404. static unsigned short snd_atiixp_ac97_read(ac97_t *ac97, unsigned short reg)
  405. {
  406. atiixp_t *chip = ac97->private_data;
  407. return snd_atiixp_codec_read(chip, ac97->num, reg);
  408. }
  409. static void snd_atiixp_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
  410. {
  411. atiixp_t *chip = ac97->private_data;
  412. snd_atiixp_codec_write(chip, ac97->num, reg, val);
  413. }
  414. /*
  415. * reset AC link
  416. */
  417. static int snd_atiixp_aclink_reset(atiixp_t *chip)
  418. {
  419. int timeout;
  420. /* reset powerdoewn */
  421. if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
  422. udelay(10);
  423. /* perform a software reset */
  424. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
  425. atiixp_read(chip, CMD);
  426. udelay(10);
  427. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
  428. timeout = 10;
  429. while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
  430. /* do a hard reset */
  431. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  432. ATI_REG_CMD_AC_SYNC);
  433. atiixp_read(chip, CMD);
  434. do_delay();
  435. atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
  436. if (--timeout) {
  437. snd_printk(KERN_ERR "atiixp: codec reset timeout\n");
  438. break;
  439. }
  440. }
  441. /* deassert RESET and assert SYNC to make sure */
  442. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  443. ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
  444. return 0;
  445. }
  446. #ifdef CONFIG_PM
  447. static int snd_atiixp_aclink_down(atiixp_t *chip)
  448. {
  449. // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
  450. // return -EBUSY;
  451. atiixp_update(chip, CMD,
  452. ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
  453. ATI_REG_CMD_POWERDOWN);
  454. return 0;
  455. }
  456. #endif
  457. /*
  458. * auto-detection of codecs
  459. *
  460. * the IXP chip can generate interrupts for the non-existing codecs.
  461. * NEW_FRAME interrupt is used to make sure that the interrupt is generated
  462. * even if all three codecs are connected.
  463. */
  464. #define ALL_CODEC_NOT_READY \
  465. (ATI_REG_ISR_CODEC0_NOT_READY |\
  466. ATI_REG_ISR_CODEC1_NOT_READY |\
  467. ATI_REG_ISR_CODEC2_NOT_READY)
  468. #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
  469. static int snd_atiixp_codec_detect(atiixp_t *chip)
  470. {
  471. int timeout;
  472. chip->codec_not_ready_bits = 0;
  473. atiixp_write(chip, IER, CODEC_CHECK_BITS);
  474. /* wait for the interrupts */
  475. timeout = HZ / 10;
  476. while (timeout-- > 0) {
  477. do_delay();
  478. if (chip->codec_not_ready_bits)
  479. break;
  480. }
  481. atiixp_write(chip, IER, 0); /* disable irqs */
  482. if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
  483. snd_printk(KERN_ERR "atiixp: no codec detected!\n");
  484. return -ENXIO;
  485. }
  486. return 0;
  487. }
  488. /*
  489. * enable DMA and irqs
  490. */
  491. static int snd_atiixp_chip_start(atiixp_t *chip)
  492. {
  493. unsigned int reg;
  494. /* set up spdif, enable burst mode */
  495. reg = atiixp_read(chip, CMD);
  496. reg |= 0x02 << ATI_REG_CMD_SPDF_THRESHOLD_SHIFT;
  497. reg |= ATI_REG_CMD_BURST_EN;
  498. atiixp_write(chip, CMD, reg);
  499. reg = atiixp_read(chip, SPDF_CMD);
  500. reg &= ~(ATI_REG_SPDF_CMD_LFSR|ATI_REG_SPDF_CMD_SINGLE_CH);
  501. atiixp_write(chip, SPDF_CMD, reg);
  502. /* clear all interrupt source */
  503. atiixp_write(chip, ISR, 0xffffffff);
  504. /* enable irqs */
  505. atiixp_write(chip, IER,
  506. ATI_REG_IER_IO_STATUS_EN |
  507. ATI_REG_IER_IN_XRUN_EN |
  508. ATI_REG_IER_OUT_XRUN_EN |
  509. ATI_REG_IER_SPDF_XRUN_EN |
  510. ATI_REG_IER_SPDF_STATUS_EN);
  511. return 0;
  512. }
  513. /*
  514. * disable DMA and IRQs
  515. */
  516. static int snd_atiixp_chip_stop(atiixp_t *chip)
  517. {
  518. /* clear interrupt source */
  519. atiixp_write(chip, ISR, atiixp_read(chip, ISR));
  520. /* disable irqs */
  521. atiixp_write(chip, IER, 0);
  522. return 0;
  523. }
  524. /*
  525. * PCM section
  526. */
  527. /*
  528. * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
  529. * position. when SG-buffer is implemented, the offset must be calculated
  530. * correctly...
  531. */
  532. static snd_pcm_uframes_t snd_atiixp_pcm_pointer(snd_pcm_substream_t *substream)
  533. {
  534. atiixp_t *chip = snd_pcm_substream_chip(substream);
  535. snd_pcm_runtime_t *runtime = substream->runtime;
  536. atiixp_dma_t *dma = (atiixp_dma_t *)runtime->private_data;
  537. unsigned int curptr;
  538. int timeout = 1000;
  539. while (timeout--) {
  540. curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  541. if (curptr < dma->buf_addr)
  542. continue;
  543. curptr -= dma->buf_addr;
  544. if (curptr >= dma->buf_bytes)
  545. continue;
  546. return bytes_to_frames(runtime, curptr);
  547. }
  548. snd_printd("atiixp: invalid DMA pointer read 0x%x (buf=%x)\n",
  549. readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
  550. return 0;
  551. }
  552. /*
  553. * XRUN detected, and stop the PCM substream
  554. */
  555. static void snd_atiixp_xrun_dma(atiixp_t *chip, atiixp_dma_t *dma)
  556. {
  557. if (! dma->substream || ! dma->running)
  558. return;
  559. snd_printdd("atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
  560. snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
  561. }
  562. /*
  563. * the period ack. update the substream.
  564. */
  565. static void snd_atiixp_update_dma(atiixp_t *chip, atiixp_dma_t *dma)
  566. {
  567. if (! dma->substream || ! dma->running)
  568. return;
  569. snd_pcm_period_elapsed(dma->substream);
  570. }
  571. /* set BUS_BUSY interrupt bit if any DMA is running */
  572. /* call with spinlock held */
  573. static void snd_atiixp_check_bus_busy(atiixp_t *chip)
  574. {
  575. unsigned int bus_busy;
  576. if (atiixp_read(chip, CMD) & (ATI_REG_CMD_SEND_EN |
  577. ATI_REG_CMD_RECEIVE_EN |
  578. ATI_REG_CMD_SPDF_OUT_EN))
  579. bus_busy = ATI_REG_IER_SET_BUS_BUSY;
  580. else
  581. bus_busy = 0;
  582. atiixp_update(chip, IER, ATI_REG_IER_SET_BUS_BUSY, bus_busy);
  583. }
  584. /* common trigger callback
  585. * calling the lowlevel callbacks in it
  586. */
  587. static int snd_atiixp_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  588. {
  589. atiixp_t *chip = snd_pcm_substream_chip(substream);
  590. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  591. int err = 0;
  592. snd_assert(dma->ops->enable_transfer && dma->ops->flush_dma, return -EINVAL);
  593. spin_lock(&chip->reg_lock);
  594. switch (cmd) {
  595. case SNDRV_PCM_TRIGGER_START:
  596. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  597. case SNDRV_PCM_TRIGGER_RESUME:
  598. dma->ops->enable_transfer(chip, 1);
  599. dma->running = 1;
  600. dma->suspended = 0;
  601. break;
  602. case SNDRV_PCM_TRIGGER_STOP:
  603. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  604. case SNDRV_PCM_TRIGGER_SUSPEND:
  605. dma->ops->enable_transfer(chip, 0);
  606. dma->running = 0;
  607. dma->suspended = cmd == SNDRV_PCM_TRIGGER_SUSPEND;
  608. break;
  609. default:
  610. err = -EINVAL;
  611. break;
  612. }
  613. if (! err) {
  614. snd_atiixp_check_bus_busy(chip);
  615. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  616. dma->ops->flush_dma(chip);
  617. snd_atiixp_check_bus_busy(chip);
  618. }
  619. }
  620. spin_unlock(&chip->reg_lock);
  621. return err;
  622. }
  623. /*
  624. * lowlevel callbacks for each DMA type
  625. *
  626. * every callback is supposed to be called in chip->reg_lock spinlock
  627. */
  628. /* flush FIFO of analog OUT DMA */
  629. static void atiixp_out_flush_dma(atiixp_t *chip)
  630. {
  631. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_OUT_FLUSH);
  632. }
  633. /* enable/disable analog OUT DMA */
  634. static void atiixp_out_enable_dma(atiixp_t *chip, int on)
  635. {
  636. unsigned int data;
  637. data = atiixp_read(chip, CMD);
  638. if (on) {
  639. if (data & ATI_REG_CMD_OUT_DMA_EN)
  640. return;
  641. atiixp_out_flush_dma(chip);
  642. data |= ATI_REG_CMD_OUT_DMA_EN;
  643. } else
  644. data &= ~ATI_REG_CMD_OUT_DMA_EN;
  645. atiixp_write(chip, CMD, data);
  646. }
  647. /* start/stop transfer over OUT DMA */
  648. static void atiixp_out_enable_transfer(atiixp_t *chip, int on)
  649. {
  650. atiixp_update(chip, CMD, ATI_REG_CMD_SEND_EN,
  651. on ? ATI_REG_CMD_SEND_EN : 0);
  652. }
  653. /* enable/disable analog IN DMA */
  654. static void atiixp_in_enable_dma(atiixp_t *chip, int on)
  655. {
  656. atiixp_update(chip, CMD, ATI_REG_CMD_IN_DMA_EN,
  657. on ? ATI_REG_CMD_IN_DMA_EN : 0);
  658. }
  659. /* start/stop analog IN DMA */
  660. static void atiixp_in_enable_transfer(atiixp_t *chip, int on)
  661. {
  662. if (on) {
  663. unsigned int data = atiixp_read(chip, CMD);
  664. if (! (data & ATI_REG_CMD_RECEIVE_EN)) {
  665. data |= ATI_REG_CMD_RECEIVE_EN;
  666. #if 0 /* FIXME: this causes the endless loop */
  667. /* wait until slot 3/4 are finished */
  668. while ((atiixp_read(chip, COUNTER) &
  669. ATI_REG_COUNTER_SLOT) != 5)
  670. ;
  671. #endif
  672. atiixp_write(chip, CMD, data);
  673. }
  674. } else
  675. atiixp_update(chip, CMD, ATI_REG_CMD_RECEIVE_EN, 0);
  676. }
  677. /* flush FIFO of analog IN DMA */
  678. static void atiixp_in_flush_dma(atiixp_t *chip)
  679. {
  680. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_IN_FLUSH);
  681. }
  682. /* enable/disable SPDIF OUT DMA */
  683. static void atiixp_spdif_enable_dma(atiixp_t *chip, int on)
  684. {
  685. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_DMA_EN,
  686. on ? ATI_REG_CMD_SPDF_DMA_EN : 0);
  687. }
  688. /* start/stop SPDIF OUT DMA */
  689. static void atiixp_spdif_enable_transfer(atiixp_t *chip, int on)
  690. {
  691. unsigned int data;
  692. data = atiixp_read(chip, CMD);
  693. if (on)
  694. data |= ATI_REG_CMD_SPDF_OUT_EN;
  695. else
  696. data &= ~ATI_REG_CMD_SPDF_OUT_EN;
  697. atiixp_write(chip, CMD, data);
  698. }
  699. /* flush FIFO of SPDIF OUT DMA */
  700. static void atiixp_spdif_flush_dma(atiixp_t *chip)
  701. {
  702. int timeout;
  703. /* DMA off, transfer on */
  704. atiixp_spdif_enable_dma(chip, 0);
  705. atiixp_spdif_enable_transfer(chip, 1);
  706. timeout = 100;
  707. do {
  708. if (! (atiixp_read(chip, SPDF_DMA_DT_SIZE) & ATI_REG_DMA_FIFO_USED))
  709. break;
  710. udelay(1);
  711. } while (timeout-- > 0);
  712. atiixp_spdif_enable_transfer(chip, 0);
  713. }
  714. /* set up slots and formats for SPDIF OUT */
  715. static int snd_atiixp_spdif_prepare(snd_pcm_substream_t *substream)
  716. {
  717. atiixp_t *chip = snd_pcm_substream_chip(substream);
  718. spin_lock_irq(&chip->reg_lock);
  719. if (chip->spdif_over_aclink) {
  720. unsigned int data;
  721. /* enable slots 10/11 */
  722. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK,
  723. ATI_REG_CMD_SPDF_CONFIG_01);
  724. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  725. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  726. ATI_REG_OUT_DMA_SLOT_BIT(11);
  727. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  728. atiixp_write(chip, OUT_DMA_SLOT, data);
  729. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  730. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  731. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  732. } else {
  733. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
  734. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
  735. }
  736. spin_unlock_irq(&chip->reg_lock);
  737. return 0;
  738. }
  739. /* set up slots and formats for analog OUT */
  740. static int snd_atiixp_playback_prepare(snd_pcm_substream_t *substream)
  741. {
  742. atiixp_t *chip = snd_pcm_substream_chip(substream);
  743. unsigned int data;
  744. spin_lock_irq(&chip->reg_lock);
  745. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  746. switch (substream->runtime->channels) {
  747. case 8:
  748. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  749. ATI_REG_OUT_DMA_SLOT_BIT(11);
  750. /* fallthru */
  751. case 6:
  752. data |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
  753. ATI_REG_OUT_DMA_SLOT_BIT(8);
  754. /* fallthru */
  755. case 4:
  756. data |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
  757. ATI_REG_OUT_DMA_SLOT_BIT(9);
  758. /* fallthru */
  759. default:
  760. data |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
  761. ATI_REG_OUT_DMA_SLOT_BIT(4);
  762. break;
  763. }
  764. /* set output threshold */
  765. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  766. atiixp_write(chip, OUT_DMA_SLOT, data);
  767. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  768. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  769. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  770. /*
  771. * enable 6 channel re-ordering bit if needed
  772. */
  773. atiixp_update(chip, 6CH_REORDER, ATI_REG_6CH_REORDER_EN,
  774. substream->runtime->channels >= 6 ? ATI_REG_6CH_REORDER_EN: 0);
  775. spin_unlock_irq(&chip->reg_lock);
  776. return 0;
  777. }
  778. /* set up slots and formats for analog IN */
  779. static int snd_atiixp_capture_prepare(snd_pcm_substream_t *substream)
  780. {
  781. atiixp_t *chip = snd_pcm_substream_chip(substream);
  782. spin_lock_irq(&chip->reg_lock);
  783. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_IN,
  784. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  785. ATI_REG_CMD_INTERLEAVE_IN : 0);
  786. spin_unlock_irq(&chip->reg_lock);
  787. return 0;
  788. }
  789. /*
  790. * hw_params - allocate the buffer and set up buffer descriptors
  791. */
  792. static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
  793. snd_pcm_hw_params_t *hw_params)
  794. {
  795. atiixp_t *chip = snd_pcm_substream_chip(substream);
  796. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  797. int err;
  798. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  799. if (err < 0)
  800. return err;
  801. dma->buf_addr = substream->runtime->dma_addr;
  802. dma->buf_bytes = params_buffer_bytes(hw_params);
  803. err = atiixp_build_dma_packets(chip, dma, substream,
  804. params_periods(hw_params),
  805. params_period_bytes(hw_params));
  806. if (err < 0)
  807. return err;
  808. if (dma->ac97_pcm_type >= 0) {
  809. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  810. /* PCM is bound to AC97 codec(s)
  811. * set up the AC97 codecs
  812. */
  813. if (dma->pcm_open_flag) {
  814. snd_ac97_pcm_close(pcm);
  815. dma->pcm_open_flag = 0;
  816. }
  817. err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
  818. params_channels(hw_params),
  819. pcm->r[0].slots);
  820. if (err >= 0)
  821. dma->pcm_open_flag = 1;
  822. }
  823. return err;
  824. }
  825. static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
  826. {
  827. atiixp_t *chip = snd_pcm_substream_chip(substream);
  828. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  829. if (dma->pcm_open_flag) {
  830. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  831. snd_ac97_pcm_close(pcm);
  832. dma->pcm_open_flag = 0;
  833. }
  834. atiixp_clear_dma_packets(chip, dma, substream);
  835. snd_pcm_lib_free_pages(substream);
  836. return 0;
  837. }
  838. /*
  839. * pcm hardware definition, identical for all DMA types
  840. */
  841. static snd_pcm_hardware_t snd_atiixp_pcm_hw =
  842. {
  843. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  844. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  845. SNDRV_PCM_INFO_PAUSE |
  846. SNDRV_PCM_INFO_RESUME |
  847. SNDRV_PCM_INFO_MMAP_VALID),
  848. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  849. .rates = SNDRV_PCM_RATE_48000,
  850. .rate_min = 48000,
  851. .rate_max = 48000,
  852. .channels_min = 2,
  853. .channels_max = 2,
  854. .buffer_bytes_max = 256 * 1024,
  855. .period_bytes_min = 32,
  856. .period_bytes_max = 128 * 1024,
  857. .periods_min = 2,
  858. .periods_max = ATI_MAX_DESCRIPTORS,
  859. };
  860. static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
  861. {
  862. atiixp_t *chip = snd_pcm_substream_chip(substream);
  863. snd_pcm_runtime_t *runtime = substream->runtime;
  864. int err;
  865. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  866. if (dma->opened)
  867. return -EBUSY;
  868. dma->substream = substream;
  869. runtime->hw = snd_atiixp_pcm_hw;
  870. dma->ac97_pcm_type = pcm_type;
  871. if (pcm_type >= 0) {
  872. runtime->hw.rates = chip->pcms[pcm_type]->rates;
  873. snd_pcm_limit_hw_rates(runtime);
  874. } else {
  875. /* direct SPDIF */
  876. runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  877. }
  878. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  879. return err;
  880. runtime->private_data = dma;
  881. /* enable DMA bits */
  882. spin_lock_irq(&chip->reg_lock);
  883. dma->ops->enable_dma(chip, 1);
  884. spin_unlock_irq(&chip->reg_lock);
  885. dma->opened = 1;
  886. return 0;
  887. }
  888. static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
  889. {
  890. atiixp_t *chip = snd_pcm_substream_chip(substream);
  891. /* disable DMA bits */
  892. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  893. spin_lock_irq(&chip->reg_lock);
  894. dma->ops->enable_dma(chip, 0);
  895. spin_unlock_irq(&chip->reg_lock);
  896. dma->substream = NULL;
  897. dma->opened = 0;
  898. return 0;
  899. }
  900. /*
  901. */
  902. static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
  903. {
  904. atiixp_t *chip = snd_pcm_substream_chip(substream);
  905. int err;
  906. down(&chip->open_mutex);
  907. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  908. up(&chip->open_mutex);
  909. if (err < 0)
  910. return err;
  911. substream->runtime->hw.channels_max = chip->max_channels;
  912. if (chip->max_channels > 2)
  913. /* channels must be even */
  914. snd_pcm_hw_constraint_step(substream->runtime, 0,
  915. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  916. return 0;
  917. }
  918. static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
  919. {
  920. atiixp_t *chip = snd_pcm_substream_chip(substream);
  921. int err;
  922. down(&chip->open_mutex);
  923. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  924. up(&chip->open_mutex);
  925. return err;
  926. }
  927. static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
  928. {
  929. atiixp_t *chip = snd_pcm_substream_chip(substream);
  930. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  931. }
  932. static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
  933. {
  934. atiixp_t *chip = snd_pcm_substream_chip(substream);
  935. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  936. }
  937. static int snd_atiixp_spdif_open(snd_pcm_substream_t *substream)
  938. {
  939. atiixp_t *chip = snd_pcm_substream_chip(substream);
  940. int err;
  941. down(&chip->open_mutex);
  942. if (chip->spdif_over_aclink) /* share DMA_PLAYBACK */
  943. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 2);
  944. else
  945. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
  946. up(&chip->open_mutex);
  947. return err;
  948. }
  949. static int snd_atiixp_spdif_close(snd_pcm_substream_t *substream)
  950. {
  951. atiixp_t *chip = snd_pcm_substream_chip(substream);
  952. int err;
  953. down(&chip->open_mutex);
  954. if (chip->spdif_over_aclink)
  955. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  956. else
  957. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
  958. up(&chip->open_mutex);
  959. return err;
  960. }
  961. /* AC97 playback */
  962. static snd_pcm_ops_t snd_atiixp_playback_ops = {
  963. .open = snd_atiixp_playback_open,
  964. .close = snd_atiixp_playback_close,
  965. .ioctl = snd_pcm_lib_ioctl,
  966. .hw_params = snd_atiixp_pcm_hw_params,
  967. .hw_free = snd_atiixp_pcm_hw_free,
  968. .prepare = snd_atiixp_playback_prepare,
  969. .trigger = snd_atiixp_pcm_trigger,
  970. .pointer = snd_atiixp_pcm_pointer,
  971. };
  972. /* AC97 capture */
  973. static snd_pcm_ops_t snd_atiixp_capture_ops = {
  974. .open = snd_atiixp_capture_open,
  975. .close = snd_atiixp_capture_close,
  976. .ioctl = snd_pcm_lib_ioctl,
  977. .hw_params = snd_atiixp_pcm_hw_params,
  978. .hw_free = snd_atiixp_pcm_hw_free,
  979. .prepare = snd_atiixp_capture_prepare,
  980. .trigger = snd_atiixp_pcm_trigger,
  981. .pointer = snd_atiixp_pcm_pointer,
  982. };
  983. /* SPDIF playback */
  984. static snd_pcm_ops_t snd_atiixp_spdif_ops = {
  985. .open = snd_atiixp_spdif_open,
  986. .close = snd_atiixp_spdif_close,
  987. .ioctl = snd_pcm_lib_ioctl,
  988. .hw_params = snd_atiixp_pcm_hw_params,
  989. .hw_free = snd_atiixp_pcm_hw_free,
  990. .prepare = snd_atiixp_spdif_prepare,
  991. .trigger = snd_atiixp_pcm_trigger,
  992. .pointer = snd_atiixp_pcm_pointer,
  993. };
  994. static struct ac97_pcm atiixp_pcm_defs[] __devinitdata = {
  995. /* front PCM */
  996. {
  997. .exclusive = 1,
  998. .r = { {
  999. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  1000. (1 << AC97_SLOT_PCM_RIGHT) |
  1001. (1 << AC97_SLOT_PCM_CENTER) |
  1002. (1 << AC97_SLOT_PCM_SLEFT) |
  1003. (1 << AC97_SLOT_PCM_SRIGHT) |
  1004. (1 << AC97_SLOT_LFE)
  1005. }
  1006. }
  1007. },
  1008. /* PCM IN #1 */
  1009. {
  1010. .stream = 1,
  1011. .exclusive = 1,
  1012. .r = { {
  1013. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  1014. (1 << AC97_SLOT_PCM_RIGHT)
  1015. }
  1016. }
  1017. },
  1018. /* S/PDIF OUT (optional) */
  1019. {
  1020. .exclusive = 1,
  1021. .spdif = 1,
  1022. .r = { {
  1023. .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
  1024. (1 << AC97_SLOT_SPDIF_RIGHT2)
  1025. }
  1026. }
  1027. },
  1028. };
  1029. static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
  1030. .type = ATI_DMA_PLAYBACK,
  1031. .llp_offset = ATI_REG_OUT_DMA_LINKPTR,
  1032. .dt_cur = ATI_REG_OUT_DMA_DT_CUR,
  1033. .enable_dma = atiixp_out_enable_dma,
  1034. .enable_transfer = atiixp_out_enable_transfer,
  1035. .flush_dma = atiixp_out_flush_dma,
  1036. };
  1037. static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
  1038. .type = ATI_DMA_CAPTURE,
  1039. .llp_offset = ATI_REG_IN_DMA_LINKPTR,
  1040. .dt_cur = ATI_REG_IN_DMA_DT_CUR,
  1041. .enable_dma = atiixp_in_enable_dma,
  1042. .enable_transfer = atiixp_in_enable_transfer,
  1043. .flush_dma = atiixp_in_flush_dma,
  1044. };
  1045. static atiixp_dma_ops_t snd_atiixp_spdif_dma_ops = {
  1046. .type = ATI_DMA_SPDIF,
  1047. .llp_offset = ATI_REG_SPDF_DMA_LINKPTR,
  1048. .dt_cur = ATI_REG_SPDF_DMA_DT_CUR,
  1049. .enable_dma = atiixp_spdif_enable_dma,
  1050. .enable_transfer = atiixp_spdif_enable_transfer,
  1051. .flush_dma = atiixp_spdif_flush_dma,
  1052. };
  1053. static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
  1054. {
  1055. snd_pcm_t *pcm;
  1056. ac97_bus_t *pbus = chip->ac97_bus;
  1057. int err, i, num_pcms;
  1058. /* initialize constants */
  1059. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  1060. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  1061. if (! chip->spdif_over_aclink)
  1062. chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
  1063. /* assign AC97 pcm */
  1064. if (chip->spdif_over_aclink)
  1065. num_pcms = 3;
  1066. else
  1067. num_pcms = 2;
  1068. err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
  1069. if (err < 0)
  1070. return err;
  1071. for (i = 0; i < num_pcms; i++)
  1072. chip->pcms[i] = &pbus->pcms[i];
  1073. chip->max_channels = 2;
  1074. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  1075. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_LFE))
  1076. chip->max_channels = 6;
  1077. else
  1078. chip->max_channels = 4;
  1079. }
  1080. /* PCM #0: analog I/O */
  1081. err = snd_pcm_new(chip->card, "ATI IXP AC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  1082. if (err < 0)
  1083. return err;
  1084. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  1085. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  1086. pcm->private_data = chip;
  1087. strcpy(pcm->name, "ATI IXP AC97");
  1088. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  1089. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1090. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  1091. /* no SPDIF support on codec? */
  1092. if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
  1093. return 0;
  1094. /* FIXME: non-48k sample rate doesn't work on my test machine with AD1888 */
  1095. if (chip->pcms[ATI_PCM_SPDIF])
  1096. chip->pcms[ATI_PCM_SPDIF]->rates = SNDRV_PCM_RATE_48000;
  1097. /* PCM #1: spdif playback */
  1098. err = snd_pcm_new(chip->card, "ATI IXP IEC958", ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
  1099. if (err < 0)
  1100. return err;
  1101. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_spdif_ops);
  1102. pcm->private_data = chip;
  1103. if (chip->spdif_over_aclink)
  1104. strcpy(pcm->name, "ATI IXP IEC958 (AC97)");
  1105. else
  1106. strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
  1107. chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
  1108. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1109. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  1110. /* pre-select AC97 SPDIF slots 10/11 */
  1111. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1112. if (chip->ac97[i])
  1113. snd_ac97_update_bits(chip->ac97[i], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
  1114. }
  1115. return 0;
  1116. }
  1117. /*
  1118. * interrupt handler
  1119. */
  1120. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1121. {
  1122. atiixp_t *chip = dev_id;
  1123. unsigned int status;
  1124. status = atiixp_read(chip, ISR);
  1125. if (! status)
  1126. return IRQ_NONE;
  1127. /* process audio DMA */
  1128. if (status & ATI_REG_ISR_OUT_XRUN)
  1129. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1130. else if (status & ATI_REG_ISR_OUT_STATUS)
  1131. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1132. if (status & ATI_REG_ISR_IN_XRUN)
  1133. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1134. else if (status & ATI_REG_ISR_IN_STATUS)
  1135. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1136. if (! chip->spdif_over_aclink) {
  1137. if (status & ATI_REG_ISR_SPDF_XRUN)
  1138. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1139. else if (status & ATI_REG_ISR_SPDF_STATUS)
  1140. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1141. }
  1142. /* for codec detection */
  1143. if (status & CODEC_CHECK_BITS) {
  1144. unsigned int detected;
  1145. detected = status & CODEC_CHECK_BITS;
  1146. spin_lock(&chip->reg_lock);
  1147. chip->codec_not_ready_bits |= detected;
  1148. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  1149. spin_unlock(&chip->reg_lock);
  1150. }
  1151. /* ack */
  1152. atiixp_write(chip, ISR, status);
  1153. return IRQ_HANDLED;
  1154. }
  1155. /*
  1156. * ac97 mixer section
  1157. */
  1158. static struct ac97_quirk ac97_quirks[] __devinitdata = {
  1159. {
  1160. .subvendor = 0x103c,
  1161. .subdevice = 0x006b,
  1162. .name = "HP Pavilion ZV5030US",
  1163. .type = AC97_TUNE_MUTE_LED
  1164. },
  1165. { } /* terminator */
  1166. };
  1167. static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock, const char *quirk_override)
  1168. {
  1169. ac97_bus_t *pbus;
  1170. ac97_template_t ac97;
  1171. int i, err;
  1172. int codec_count;
  1173. static ac97_bus_ops_t ops = {
  1174. .write = snd_atiixp_ac97_write,
  1175. .read = snd_atiixp_ac97_read,
  1176. };
  1177. static unsigned int codec_skip[NUM_ATI_CODECS] = {
  1178. ATI_REG_ISR_CODEC0_NOT_READY,
  1179. ATI_REG_ISR_CODEC1_NOT_READY,
  1180. ATI_REG_ISR_CODEC2_NOT_READY,
  1181. };
  1182. if (snd_atiixp_codec_detect(chip) < 0)
  1183. return -ENXIO;
  1184. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  1185. return err;
  1186. pbus->clock = clock;
  1187. chip->ac97_bus = pbus;
  1188. codec_count = 0;
  1189. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1190. if (chip->codec_not_ready_bits & codec_skip[i])
  1191. continue;
  1192. memset(&ac97, 0, sizeof(ac97));
  1193. ac97.private_data = chip;
  1194. ac97.pci = chip->pci;
  1195. ac97.num = i;
  1196. ac97.scaps = AC97_SCAP_SKIP_MODEM;
  1197. if (! chip->spdif_over_aclink)
  1198. ac97.scaps |= AC97_SCAP_NO_SPDIF;
  1199. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  1200. chip->ac97[i] = NULL; /* to be sure */
  1201. snd_printdd("atiixp: codec %d not available for audio\n", i);
  1202. continue;
  1203. }
  1204. codec_count++;
  1205. }
  1206. if (! codec_count) {
  1207. snd_printk(KERN_ERR "atiixp: no codec available\n");
  1208. return -ENODEV;
  1209. }
  1210. snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
  1211. return 0;
  1212. }
  1213. #ifdef CONFIG_PM
  1214. /*
  1215. * power management
  1216. */
  1217. static int snd_atiixp_suspend(snd_card_t *card, pm_message_t state)
  1218. {
  1219. atiixp_t *chip = card->pm_private_data;
  1220. int i;
  1221. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1222. if (chip->pcmdevs[i]) {
  1223. atiixp_dma_t *dma = &chip->dmas[i];
  1224. if (dma->substream && dma->running)
  1225. dma->saved_curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  1226. snd_pcm_suspend_all(chip->pcmdevs[i]);
  1227. }
  1228. for (i = 0; i < NUM_ATI_CODECS; i++)
  1229. if (chip->ac97[i])
  1230. snd_ac97_suspend(chip->ac97[i]);
  1231. snd_atiixp_aclink_down(chip);
  1232. snd_atiixp_chip_stop(chip);
  1233. pci_set_power_state(chip->pci, PCI_D3hot);
  1234. pci_disable_device(chip->pci);
  1235. return 0;
  1236. }
  1237. static int snd_atiixp_resume(snd_card_t *card)
  1238. {
  1239. atiixp_t *chip = card->pm_private_data;
  1240. int i;
  1241. pci_enable_device(chip->pci);
  1242. pci_set_power_state(chip->pci, PCI_D0);
  1243. pci_set_master(chip->pci);
  1244. snd_atiixp_aclink_reset(chip);
  1245. snd_atiixp_chip_start(chip);
  1246. for (i = 0; i < NUM_ATI_CODECS; i++)
  1247. if (chip->ac97[i])
  1248. snd_ac97_resume(chip->ac97[i]);
  1249. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1250. if (chip->pcmdevs[i]) {
  1251. atiixp_dma_t *dma = &chip->dmas[i];
  1252. if (dma->substream && dma->suspended) {
  1253. dma->ops->enable_dma(chip, 1);
  1254. dma->substream->ops->prepare(dma->substream);
  1255. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  1256. chip->remap_addr + dma->ops->llp_offset);
  1257. writel(dma->saved_curptr, chip->remap_addr + dma->ops->dt_cur);
  1258. }
  1259. }
  1260. return 0;
  1261. }
  1262. #endif /* CONFIG_PM */
  1263. /*
  1264. * proc interface for register dump
  1265. */
  1266. static void snd_atiixp_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  1267. {
  1268. atiixp_t *chip = entry->private_data;
  1269. int i;
  1270. for (i = 0; i < 256; i += 4)
  1271. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  1272. }
  1273. static void __devinit snd_atiixp_proc_init(atiixp_t *chip)
  1274. {
  1275. snd_info_entry_t *entry;
  1276. if (! snd_card_proc_new(chip->card, "atiixp", &entry))
  1277. snd_info_set_text_ops(entry, chip, 1024, snd_atiixp_proc_read);
  1278. }
  1279. /*
  1280. * destructor
  1281. */
  1282. static int snd_atiixp_free(atiixp_t *chip)
  1283. {
  1284. if (chip->irq < 0)
  1285. goto __hw_end;
  1286. snd_atiixp_chip_stop(chip);
  1287. synchronize_irq(chip->irq);
  1288. __hw_end:
  1289. if (chip->irq >= 0)
  1290. free_irq(chip->irq, (void *)chip);
  1291. if (chip->remap_addr)
  1292. iounmap(chip->remap_addr);
  1293. pci_release_regions(chip->pci);
  1294. pci_disable_device(chip->pci);
  1295. kfree(chip);
  1296. return 0;
  1297. }
  1298. static int snd_atiixp_dev_free(snd_device_t *device)
  1299. {
  1300. atiixp_t *chip = device->device_data;
  1301. return snd_atiixp_free(chip);
  1302. }
  1303. /*
  1304. * constructor for chip instance
  1305. */
  1306. static int __devinit snd_atiixp_create(snd_card_t *card,
  1307. struct pci_dev *pci,
  1308. atiixp_t **r_chip)
  1309. {
  1310. static snd_device_ops_t ops = {
  1311. .dev_free = snd_atiixp_dev_free,
  1312. };
  1313. atiixp_t *chip;
  1314. int err;
  1315. if ((err = pci_enable_device(pci)) < 0)
  1316. return err;
  1317. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1318. if (chip == NULL) {
  1319. pci_disable_device(pci);
  1320. return -ENOMEM;
  1321. }
  1322. spin_lock_init(&chip->reg_lock);
  1323. init_MUTEX(&chip->open_mutex);
  1324. chip->card = card;
  1325. chip->pci = pci;
  1326. chip->irq = -1;
  1327. if ((err = pci_request_regions(pci, "ATI IXP AC97")) < 0) {
  1328. pci_disable_device(pci);
  1329. kfree(chip);
  1330. return err;
  1331. }
  1332. chip->addr = pci_resource_start(pci, 0);
  1333. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
  1334. if (chip->remap_addr == NULL) {
  1335. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1336. snd_atiixp_free(chip);
  1337. return -EIO;
  1338. }
  1339. if (request_irq(pci->irq, snd_atiixp_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
  1340. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1341. snd_atiixp_free(chip);
  1342. return -EBUSY;
  1343. }
  1344. chip->irq = pci->irq;
  1345. pci_set_master(pci);
  1346. synchronize_irq(chip->irq);
  1347. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1348. snd_atiixp_free(chip);
  1349. return err;
  1350. }
  1351. snd_card_set_dev(card, &pci->dev);
  1352. *r_chip = chip;
  1353. return 0;
  1354. }
  1355. static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  1356. const struct pci_device_id *pci_id)
  1357. {
  1358. snd_card_t *card;
  1359. atiixp_t *chip;
  1360. unsigned char revision;
  1361. int err;
  1362. card = snd_card_new(index, id, THIS_MODULE, 0);
  1363. if (card == NULL)
  1364. return -ENOMEM;
  1365. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  1366. strcpy(card->driver, spdif_aclink ? "ATIIXP" : "ATIIXP-SPDMA");
  1367. strcpy(card->shortname, "ATI IXP");
  1368. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1369. goto __error;
  1370. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1371. goto __error;
  1372. chip->spdif_over_aclink = spdif_aclink;
  1373. if ((err = snd_atiixp_mixer_new(chip, ac97_clock, ac97_quirk)) < 0)
  1374. goto __error;
  1375. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1376. goto __error;
  1377. snd_atiixp_proc_init(chip);
  1378. snd_atiixp_chip_start(chip);
  1379. snprintf(card->longname, sizeof(card->longname),
  1380. "%s rev %x with %s at %#lx, irq %i", card->shortname, revision,
  1381. chip->ac97[0] ? snd_ac97_get_short_name(chip->ac97[0]) : "?",
  1382. chip->addr, chip->irq);
  1383. snd_card_set_pm_callback(card, snd_atiixp_suspend, snd_atiixp_resume, chip);
  1384. if ((err = snd_card_register(card)) < 0)
  1385. goto __error;
  1386. pci_set_drvdata(pci, card);
  1387. return 0;
  1388. __error:
  1389. snd_card_free(card);
  1390. return err;
  1391. }
  1392. static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  1393. {
  1394. snd_card_free(pci_get_drvdata(pci));
  1395. pci_set_drvdata(pci, NULL);
  1396. }
  1397. static struct pci_driver driver = {
  1398. .name = "ATI IXP AC97 controller",
  1399. .id_table = snd_atiixp_ids,
  1400. .probe = snd_atiixp_probe,
  1401. .remove = __devexit_p(snd_atiixp_remove),
  1402. SND_PCI_PM_CALLBACKS
  1403. };
  1404. static int __init alsa_card_atiixp_init(void)
  1405. {
  1406. return pci_register_driver(&driver);
  1407. }
  1408. static void __exit alsa_card_atiixp_exit(void)
  1409. {
  1410. pci_unregister_driver(&driver);
  1411. }
  1412. module_init(alsa_card_atiixp_init)
  1413. module_exit(alsa_card_atiixp_exit)