atiixp.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  40. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  41. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  42. static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 48000};
  43. static char *ac97_quirk[SNDRV_CARDS];
  44. static int spdif_aclink[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  45. module_param_array(index, int, NULL, 0444);
  46. MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
  47. module_param_array(id, charp, NULL, 0444);
  48. MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
  49. module_param_array(enable, bool, NULL, 0444);
  50. MODULE_PARM_DESC(enable, "Enable audio part of ATI IXP controller.");
  51. module_param_array(ac97_clock, int, NULL, 0444);
  52. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  53. module_param_array(ac97_quirk, charp, NULL, 0444);
  54. MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  55. module_param_array(spdif_aclink, bool, NULL, 0444);
  56. MODULE_PARM_DESC(spdif_aclink, "S/PDIF over AC-link.");
  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 pcm_open_flag;
  215. int ac97_pcm_type; /* index # of ac97_pcm to access, -1 = not used */
  216. unsigned int saved_curptr;
  217. };
  218. /*
  219. * ATI IXP chip
  220. */
  221. struct snd_atiixp {
  222. snd_card_t *card;
  223. struct pci_dev *pci;
  224. unsigned long addr;
  225. void __iomem *remap_addr;
  226. int irq;
  227. ac97_bus_t *ac97_bus;
  228. ac97_t *ac97[NUM_ATI_CODECS];
  229. spinlock_t reg_lock;
  230. atiixp_dma_t dmas[NUM_ATI_DMAS];
  231. struct ac97_pcm *pcms[NUM_ATI_PCMS];
  232. snd_pcm_t *pcmdevs[NUM_ATI_PCMDEVS];
  233. int max_channels; /* max. channels for PCM out */
  234. unsigned int codec_not_ready_bits; /* for codec detection */
  235. int spdif_over_aclink; /* passed from the module option */
  236. struct semaphore open_mutex; /* playback open mutex */
  237. };
  238. /*
  239. */
  240. static struct pci_device_id snd_atiixp_ids[] = {
  241. { 0x1002, 0x4341, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB200 */
  242. { 0x1002, 0x4361, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB300 */
  243. { 0x1002, 0x4370, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB400 */
  244. { 0, }
  245. };
  246. MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
  247. /*
  248. * lowlevel functions
  249. */
  250. /*
  251. * update the bits of the given register.
  252. * return 1 if the bits changed.
  253. */
  254. static int snd_atiixp_update_bits(atiixp_t *chip, unsigned int reg,
  255. unsigned int mask, unsigned int value)
  256. {
  257. void __iomem *addr = chip->remap_addr + reg;
  258. unsigned int data, old_data;
  259. old_data = data = readl(addr);
  260. data &= ~mask;
  261. data |= value;
  262. if (old_data == data)
  263. return 0;
  264. writel(data, addr);
  265. return 1;
  266. }
  267. /*
  268. * macros for easy use
  269. */
  270. #define atiixp_write(chip,reg,value) \
  271. writel(value, chip->remap_addr + ATI_REG_##reg)
  272. #define atiixp_read(chip,reg) \
  273. readl(chip->remap_addr + ATI_REG_##reg)
  274. #define atiixp_update(chip,reg,mask,val) \
  275. snd_atiixp_update_bits(chip, ATI_REG_##reg, mask, val)
  276. /* delay for one tick */
  277. #define do_delay() do { \
  278. set_current_state(TASK_UNINTERRUPTIBLE); \
  279. schedule_timeout(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. dma->ops->enable_transfer(chip, 1);
  597. dma->running = 1;
  598. break;
  599. case SNDRV_PCM_TRIGGER_STOP:
  600. dma->ops->enable_transfer(chip, 0);
  601. dma->running = 0;
  602. break;
  603. default:
  604. err = -EINVAL;
  605. break;
  606. }
  607. if (! err) {
  608. snd_atiixp_check_bus_busy(chip);
  609. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  610. dma->ops->flush_dma(chip);
  611. snd_atiixp_check_bus_busy(chip);
  612. }
  613. }
  614. spin_unlock(&chip->reg_lock);
  615. return err;
  616. }
  617. /*
  618. * lowlevel callbacks for each DMA type
  619. *
  620. * every callback is supposed to be called in chip->reg_lock spinlock
  621. */
  622. /* flush FIFO of analog OUT DMA */
  623. static void atiixp_out_flush_dma(atiixp_t *chip)
  624. {
  625. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_OUT_FLUSH);
  626. }
  627. /* enable/disable analog OUT DMA */
  628. static void atiixp_out_enable_dma(atiixp_t *chip, int on)
  629. {
  630. unsigned int data;
  631. data = atiixp_read(chip, CMD);
  632. if (on) {
  633. if (data & ATI_REG_CMD_OUT_DMA_EN)
  634. return;
  635. atiixp_out_flush_dma(chip);
  636. data |= ATI_REG_CMD_OUT_DMA_EN;
  637. } else
  638. data &= ~ATI_REG_CMD_OUT_DMA_EN;
  639. atiixp_write(chip, CMD, data);
  640. }
  641. /* start/stop transfer over OUT DMA */
  642. static void atiixp_out_enable_transfer(atiixp_t *chip, int on)
  643. {
  644. atiixp_update(chip, CMD, ATI_REG_CMD_SEND_EN,
  645. on ? ATI_REG_CMD_SEND_EN : 0);
  646. }
  647. /* enable/disable analog IN DMA */
  648. static void atiixp_in_enable_dma(atiixp_t *chip, int on)
  649. {
  650. atiixp_update(chip, CMD, ATI_REG_CMD_IN_DMA_EN,
  651. on ? ATI_REG_CMD_IN_DMA_EN : 0);
  652. }
  653. /* start/stop analog IN DMA */
  654. static void atiixp_in_enable_transfer(atiixp_t *chip, int on)
  655. {
  656. if (on) {
  657. unsigned int data = atiixp_read(chip, CMD);
  658. if (! (data & ATI_REG_CMD_RECEIVE_EN)) {
  659. data |= ATI_REG_CMD_RECEIVE_EN;
  660. #if 0 /* FIXME: this causes the endless loop */
  661. /* wait until slot 3/4 are finished */
  662. while ((atiixp_read(chip, COUNTER) &
  663. ATI_REG_COUNTER_SLOT) != 5)
  664. ;
  665. #endif
  666. atiixp_write(chip, CMD, data);
  667. }
  668. } else
  669. atiixp_update(chip, CMD, ATI_REG_CMD_RECEIVE_EN, 0);
  670. }
  671. /* flush FIFO of analog IN DMA */
  672. static void atiixp_in_flush_dma(atiixp_t *chip)
  673. {
  674. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_IN_FLUSH);
  675. }
  676. /* enable/disable SPDIF OUT DMA */
  677. static void atiixp_spdif_enable_dma(atiixp_t *chip, int on)
  678. {
  679. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_DMA_EN,
  680. on ? ATI_REG_CMD_SPDF_DMA_EN : 0);
  681. }
  682. /* start/stop SPDIF OUT DMA */
  683. static void atiixp_spdif_enable_transfer(atiixp_t *chip, int on)
  684. {
  685. unsigned int data;
  686. data = atiixp_read(chip, CMD);
  687. if (on)
  688. data |= ATI_REG_CMD_SPDF_OUT_EN;
  689. else
  690. data &= ~ATI_REG_CMD_SPDF_OUT_EN;
  691. atiixp_write(chip, CMD, data);
  692. }
  693. /* flush FIFO of SPDIF OUT DMA */
  694. static void atiixp_spdif_flush_dma(atiixp_t *chip)
  695. {
  696. int timeout;
  697. /* DMA off, transfer on */
  698. atiixp_spdif_enable_dma(chip, 0);
  699. atiixp_spdif_enable_transfer(chip, 1);
  700. timeout = 100;
  701. do {
  702. if (! (atiixp_read(chip, SPDF_DMA_DT_SIZE) & ATI_REG_DMA_FIFO_USED))
  703. break;
  704. udelay(1);
  705. } while (timeout-- > 0);
  706. atiixp_spdif_enable_transfer(chip, 0);
  707. }
  708. /* set up slots and formats for SPDIF OUT */
  709. static int snd_atiixp_spdif_prepare(snd_pcm_substream_t *substream)
  710. {
  711. atiixp_t *chip = snd_pcm_substream_chip(substream);
  712. spin_lock_irq(&chip->reg_lock);
  713. if (chip->spdif_over_aclink) {
  714. unsigned int data;
  715. /* enable slots 10/11 */
  716. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK,
  717. ATI_REG_CMD_SPDF_CONFIG_01);
  718. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  719. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  720. ATI_REG_OUT_DMA_SLOT_BIT(11);
  721. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  722. atiixp_write(chip, OUT_DMA_SLOT, data);
  723. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  724. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  725. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  726. } else {
  727. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
  728. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
  729. }
  730. spin_unlock_irq(&chip->reg_lock);
  731. return 0;
  732. }
  733. /* set up slots and formats for analog OUT */
  734. static int snd_atiixp_playback_prepare(snd_pcm_substream_t *substream)
  735. {
  736. atiixp_t *chip = snd_pcm_substream_chip(substream);
  737. unsigned int data;
  738. spin_lock_irq(&chip->reg_lock);
  739. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  740. switch (substream->runtime->channels) {
  741. case 8:
  742. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  743. ATI_REG_OUT_DMA_SLOT_BIT(11);
  744. /* fallthru */
  745. case 6:
  746. data |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
  747. ATI_REG_OUT_DMA_SLOT_BIT(8);
  748. /* fallthru */
  749. case 4:
  750. data |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
  751. ATI_REG_OUT_DMA_SLOT_BIT(9);
  752. /* fallthru */
  753. default:
  754. data |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
  755. ATI_REG_OUT_DMA_SLOT_BIT(4);
  756. break;
  757. }
  758. /* set output threshold */
  759. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  760. atiixp_write(chip, OUT_DMA_SLOT, data);
  761. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  762. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  763. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  764. /*
  765. * enable 6 channel re-ordering bit if needed
  766. */
  767. atiixp_update(chip, 6CH_REORDER, ATI_REG_6CH_REORDER_EN,
  768. substream->runtime->channels >= 6 ? ATI_REG_6CH_REORDER_EN: 0);
  769. spin_unlock_irq(&chip->reg_lock);
  770. return 0;
  771. }
  772. /* set up slots and formats for analog IN */
  773. static int snd_atiixp_capture_prepare(snd_pcm_substream_t *substream)
  774. {
  775. atiixp_t *chip = snd_pcm_substream_chip(substream);
  776. spin_lock_irq(&chip->reg_lock);
  777. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_IN,
  778. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  779. ATI_REG_CMD_INTERLEAVE_IN : 0);
  780. spin_unlock_irq(&chip->reg_lock);
  781. return 0;
  782. }
  783. /*
  784. * hw_params - allocate the buffer and set up buffer descriptors
  785. */
  786. static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
  787. snd_pcm_hw_params_t *hw_params)
  788. {
  789. atiixp_t *chip = snd_pcm_substream_chip(substream);
  790. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  791. int err;
  792. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  793. if (err < 0)
  794. return err;
  795. dma->buf_addr = substream->runtime->dma_addr;
  796. dma->buf_bytes = params_buffer_bytes(hw_params);
  797. err = atiixp_build_dma_packets(chip, dma, substream,
  798. params_periods(hw_params),
  799. params_period_bytes(hw_params));
  800. if (err < 0)
  801. return err;
  802. if (dma->ac97_pcm_type >= 0) {
  803. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  804. /* PCM is bound to AC97 codec(s)
  805. * set up the AC97 codecs
  806. */
  807. if (dma->pcm_open_flag) {
  808. snd_ac97_pcm_close(pcm);
  809. dma->pcm_open_flag = 0;
  810. }
  811. err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
  812. params_channels(hw_params),
  813. pcm->r[0].slots);
  814. if (err >= 0)
  815. dma->pcm_open_flag = 1;
  816. }
  817. return err;
  818. }
  819. static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
  820. {
  821. atiixp_t *chip = snd_pcm_substream_chip(substream);
  822. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  823. if (dma->pcm_open_flag) {
  824. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  825. snd_ac97_pcm_close(pcm);
  826. dma->pcm_open_flag = 0;
  827. }
  828. atiixp_clear_dma_packets(chip, dma, substream);
  829. snd_pcm_lib_free_pages(substream);
  830. return 0;
  831. }
  832. /*
  833. * pcm hardware definition, identical for all DMA types
  834. */
  835. static snd_pcm_hardware_t snd_atiixp_pcm_hw =
  836. {
  837. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  838. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  839. SNDRV_PCM_INFO_RESUME |
  840. SNDRV_PCM_INFO_MMAP_VALID),
  841. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  842. .rates = SNDRV_PCM_RATE_48000,
  843. .rate_min = 48000,
  844. .rate_max = 48000,
  845. .channels_min = 2,
  846. .channels_max = 2,
  847. .buffer_bytes_max = 256 * 1024,
  848. .period_bytes_min = 32,
  849. .period_bytes_max = 128 * 1024,
  850. .periods_min = 2,
  851. .periods_max = ATI_MAX_DESCRIPTORS,
  852. };
  853. static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
  854. {
  855. atiixp_t *chip = snd_pcm_substream_chip(substream);
  856. snd_pcm_runtime_t *runtime = substream->runtime;
  857. int err;
  858. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  859. if (dma->opened)
  860. return -EBUSY;
  861. dma->substream = substream;
  862. runtime->hw = snd_atiixp_pcm_hw;
  863. dma->ac97_pcm_type = pcm_type;
  864. if (pcm_type >= 0) {
  865. runtime->hw.rates = chip->pcms[pcm_type]->rates;
  866. snd_pcm_limit_hw_rates(runtime);
  867. } else {
  868. /* direct SPDIF */
  869. runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  870. }
  871. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  872. return err;
  873. runtime->private_data = dma;
  874. /* enable DMA bits */
  875. spin_lock_irq(&chip->reg_lock);
  876. dma->ops->enable_dma(chip, 1);
  877. spin_unlock_irq(&chip->reg_lock);
  878. dma->opened = 1;
  879. return 0;
  880. }
  881. static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
  882. {
  883. atiixp_t *chip = snd_pcm_substream_chip(substream);
  884. /* disable DMA bits */
  885. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  886. spin_lock_irq(&chip->reg_lock);
  887. dma->ops->enable_dma(chip, 0);
  888. spin_unlock_irq(&chip->reg_lock);
  889. dma->substream = NULL;
  890. dma->opened = 0;
  891. return 0;
  892. }
  893. /*
  894. */
  895. static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
  896. {
  897. atiixp_t *chip = snd_pcm_substream_chip(substream);
  898. int err;
  899. down(&chip->open_mutex);
  900. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  901. up(&chip->open_mutex);
  902. if (err < 0)
  903. return err;
  904. substream->runtime->hw.channels_max = chip->max_channels;
  905. if (chip->max_channels > 2)
  906. /* channels must be even */
  907. snd_pcm_hw_constraint_step(substream->runtime, 0,
  908. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  909. return 0;
  910. }
  911. static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
  912. {
  913. atiixp_t *chip = snd_pcm_substream_chip(substream);
  914. int err;
  915. down(&chip->open_mutex);
  916. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  917. up(&chip->open_mutex);
  918. return err;
  919. }
  920. static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
  921. {
  922. atiixp_t *chip = snd_pcm_substream_chip(substream);
  923. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  924. }
  925. static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
  926. {
  927. atiixp_t *chip = snd_pcm_substream_chip(substream);
  928. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  929. }
  930. static int snd_atiixp_spdif_open(snd_pcm_substream_t *substream)
  931. {
  932. atiixp_t *chip = snd_pcm_substream_chip(substream);
  933. int err;
  934. down(&chip->open_mutex);
  935. if (chip->spdif_over_aclink) /* share DMA_PLAYBACK */
  936. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 2);
  937. else
  938. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
  939. up(&chip->open_mutex);
  940. return err;
  941. }
  942. static int snd_atiixp_spdif_close(snd_pcm_substream_t *substream)
  943. {
  944. atiixp_t *chip = snd_pcm_substream_chip(substream);
  945. int err;
  946. down(&chip->open_mutex);
  947. if (chip->spdif_over_aclink)
  948. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  949. else
  950. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
  951. up(&chip->open_mutex);
  952. return err;
  953. }
  954. /* AC97 playback */
  955. static snd_pcm_ops_t snd_atiixp_playback_ops = {
  956. .open = snd_atiixp_playback_open,
  957. .close = snd_atiixp_playback_close,
  958. .ioctl = snd_pcm_lib_ioctl,
  959. .hw_params = snd_atiixp_pcm_hw_params,
  960. .hw_free = snd_atiixp_pcm_hw_free,
  961. .prepare = snd_atiixp_playback_prepare,
  962. .trigger = snd_atiixp_pcm_trigger,
  963. .pointer = snd_atiixp_pcm_pointer,
  964. };
  965. /* AC97 capture */
  966. static snd_pcm_ops_t snd_atiixp_capture_ops = {
  967. .open = snd_atiixp_capture_open,
  968. .close = snd_atiixp_capture_close,
  969. .ioctl = snd_pcm_lib_ioctl,
  970. .hw_params = snd_atiixp_pcm_hw_params,
  971. .hw_free = snd_atiixp_pcm_hw_free,
  972. .prepare = snd_atiixp_capture_prepare,
  973. .trigger = snd_atiixp_pcm_trigger,
  974. .pointer = snd_atiixp_pcm_pointer,
  975. };
  976. /* SPDIF playback */
  977. static snd_pcm_ops_t snd_atiixp_spdif_ops = {
  978. .open = snd_atiixp_spdif_open,
  979. .close = snd_atiixp_spdif_close,
  980. .ioctl = snd_pcm_lib_ioctl,
  981. .hw_params = snd_atiixp_pcm_hw_params,
  982. .hw_free = snd_atiixp_pcm_hw_free,
  983. .prepare = snd_atiixp_spdif_prepare,
  984. .trigger = snd_atiixp_pcm_trigger,
  985. .pointer = snd_atiixp_pcm_pointer,
  986. };
  987. static struct ac97_pcm atiixp_pcm_defs[] __devinitdata = {
  988. /* front PCM */
  989. {
  990. .exclusive = 1,
  991. .r = { {
  992. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  993. (1 << AC97_SLOT_PCM_RIGHT) |
  994. (1 << AC97_SLOT_PCM_CENTER) |
  995. (1 << AC97_SLOT_PCM_SLEFT) |
  996. (1 << AC97_SLOT_PCM_SRIGHT) |
  997. (1 << AC97_SLOT_LFE)
  998. }
  999. }
  1000. },
  1001. /* PCM IN #1 */
  1002. {
  1003. .stream = 1,
  1004. .exclusive = 1,
  1005. .r = { {
  1006. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  1007. (1 << AC97_SLOT_PCM_RIGHT)
  1008. }
  1009. }
  1010. },
  1011. /* S/PDIF OUT (optional) */
  1012. {
  1013. .exclusive = 1,
  1014. .spdif = 1,
  1015. .r = { {
  1016. .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
  1017. (1 << AC97_SLOT_SPDIF_RIGHT2)
  1018. }
  1019. }
  1020. },
  1021. };
  1022. static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
  1023. .type = ATI_DMA_PLAYBACK,
  1024. .llp_offset = ATI_REG_OUT_DMA_LINKPTR,
  1025. .dt_cur = ATI_REG_OUT_DMA_DT_CUR,
  1026. .enable_dma = atiixp_out_enable_dma,
  1027. .enable_transfer = atiixp_out_enable_transfer,
  1028. .flush_dma = atiixp_out_flush_dma,
  1029. };
  1030. static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
  1031. .type = ATI_DMA_CAPTURE,
  1032. .llp_offset = ATI_REG_IN_DMA_LINKPTR,
  1033. .dt_cur = ATI_REG_IN_DMA_DT_CUR,
  1034. .enable_dma = atiixp_in_enable_dma,
  1035. .enable_transfer = atiixp_in_enable_transfer,
  1036. .flush_dma = atiixp_in_flush_dma,
  1037. };
  1038. static atiixp_dma_ops_t snd_atiixp_spdif_dma_ops = {
  1039. .type = ATI_DMA_SPDIF,
  1040. .llp_offset = ATI_REG_SPDF_DMA_LINKPTR,
  1041. .dt_cur = ATI_REG_SPDF_DMA_DT_CUR,
  1042. .enable_dma = atiixp_spdif_enable_dma,
  1043. .enable_transfer = atiixp_spdif_enable_transfer,
  1044. .flush_dma = atiixp_spdif_flush_dma,
  1045. };
  1046. static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
  1047. {
  1048. snd_pcm_t *pcm;
  1049. ac97_bus_t *pbus = chip->ac97_bus;
  1050. int err, i, num_pcms;
  1051. /* initialize constants */
  1052. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  1053. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  1054. if (! chip->spdif_over_aclink)
  1055. chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
  1056. /* assign AC97 pcm */
  1057. if (chip->spdif_over_aclink)
  1058. num_pcms = 3;
  1059. else
  1060. num_pcms = 2;
  1061. err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
  1062. if (err < 0)
  1063. return err;
  1064. for (i = 0; i < num_pcms; i++)
  1065. chip->pcms[i] = &pbus->pcms[i];
  1066. chip->max_channels = 2;
  1067. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  1068. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_LFE))
  1069. chip->max_channels = 6;
  1070. else
  1071. chip->max_channels = 4;
  1072. }
  1073. /* PCM #0: analog I/O */
  1074. err = snd_pcm_new(chip->card, "ATI IXP AC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  1075. if (err < 0)
  1076. return err;
  1077. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  1078. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  1079. pcm->private_data = chip;
  1080. strcpy(pcm->name, "ATI IXP AC97");
  1081. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  1082. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1083. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  1084. /* no SPDIF support on codec? */
  1085. if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
  1086. return 0;
  1087. /* FIXME: non-48k sample rate doesn't work on my test machine with AD1888 */
  1088. if (chip->pcms[ATI_PCM_SPDIF])
  1089. chip->pcms[ATI_PCM_SPDIF]->rates = SNDRV_PCM_RATE_48000;
  1090. /* PCM #1: spdif playback */
  1091. err = snd_pcm_new(chip->card, "ATI IXP IEC958", ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
  1092. if (err < 0)
  1093. return err;
  1094. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_spdif_ops);
  1095. pcm->private_data = chip;
  1096. if (chip->spdif_over_aclink)
  1097. strcpy(pcm->name, "ATI IXP IEC958 (AC97)");
  1098. else
  1099. strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
  1100. chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
  1101. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1102. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  1103. /* pre-select AC97 SPDIF slots 10/11 */
  1104. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1105. if (chip->ac97[i])
  1106. snd_ac97_update_bits(chip->ac97[i], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
  1107. }
  1108. return 0;
  1109. }
  1110. /*
  1111. * interrupt handler
  1112. */
  1113. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1114. {
  1115. atiixp_t *chip = dev_id;
  1116. unsigned int status;
  1117. status = atiixp_read(chip, ISR);
  1118. if (! status)
  1119. return IRQ_NONE;
  1120. /* process audio DMA */
  1121. if (status & ATI_REG_ISR_OUT_XRUN)
  1122. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1123. else if (status & ATI_REG_ISR_OUT_STATUS)
  1124. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1125. if (status & ATI_REG_ISR_IN_XRUN)
  1126. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1127. else if (status & ATI_REG_ISR_IN_STATUS)
  1128. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1129. if (! chip->spdif_over_aclink) {
  1130. if (status & ATI_REG_ISR_SPDF_XRUN)
  1131. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1132. else if (status & ATI_REG_ISR_SPDF_STATUS)
  1133. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1134. }
  1135. /* for codec detection */
  1136. if (status & CODEC_CHECK_BITS) {
  1137. unsigned int detected;
  1138. detected = status & CODEC_CHECK_BITS;
  1139. spin_lock(&chip->reg_lock);
  1140. chip->codec_not_ready_bits |= detected;
  1141. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  1142. spin_unlock(&chip->reg_lock);
  1143. }
  1144. /* ack */
  1145. atiixp_write(chip, ISR, status);
  1146. return IRQ_HANDLED;
  1147. }
  1148. /*
  1149. * ac97 mixer section
  1150. */
  1151. static struct ac97_quirk ac97_quirks[] __devinitdata = {
  1152. {
  1153. .subvendor = 0x103c,
  1154. .subdevice = 0x006b,
  1155. .name = "HP Pavilion ZV5030US",
  1156. .type = AC97_TUNE_MUTE_LED
  1157. },
  1158. { } /* terminator */
  1159. };
  1160. static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock, const char *quirk_override)
  1161. {
  1162. ac97_bus_t *pbus;
  1163. ac97_template_t ac97;
  1164. int i, err;
  1165. int codec_count;
  1166. static ac97_bus_ops_t ops = {
  1167. .write = snd_atiixp_ac97_write,
  1168. .read = snd_atiixp_ac97_read,
  1169. };
  1170. static unsigned int codec_skip[NUM_ATI_CODECS] = {
  1171. ATI_REG_ISR_CODEC0_NOT_READY,
  1172. ATI_REG_ISR_CODEC1_NOT_READY,
  1173. ATI_REG_ISR_CODEC2_NOT_READY,
  1174. };
  1175. if (snd_atiixp_codec_detect(chip) < 0)
  1176. return -ENXIO;
  1177. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  1178. return err;
  1179. pbus->clock = clock;
  1180. pbus->shared_type = AC97_SHARED_TYPE_ATIIXP; /* shared with modem driver */
  1181. chip->ac97_bus = pbus;
  1182. codec_count = 0;
  1183. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1184. if (chip->codec_not_ready_bits & codec_skip[i])
  1185. continue;
  1186. memset(&ac97, 0, sizeof(ac97));
  1187. ac97.private_data = chip;
  1188. ac97.pci = chip->pci;
  1189. ac97.num = i;
  1190. ac97.scaps = AC97_SCAP_SKIP_MODEM;
  1191. if (! chip->spdif_over_aclink)
  1192. ac97.scaps |= AC97_SCAP_NO_SPDIF;
  1193. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  1194. chip->ac97[i] = NULL; /* to be sure */
  1195. snd_printdd("atiixp: codec %d not available for audio\n", i);
  1196. continue;
  1197. }
  1198. codec_count++;
  1199. }
  1200. if (! codec_count) {
  1201. snd_printk(KERN_ERR "atiixp: no codec available\n");
  1202. return -ENODEV;
  1203. }
  1204. snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
  1205. return 0;
  1206. }
  1207. #ifdef CONFIG_PM
  1208. /*
  1209. * power management
  1210. */
  1211. static int snd_atiixp_suspend(snd_card_t *card, pm_message_t state)
  1212. {
  1213. atiixp_t *chip = card->pm_private_data;
  1214. int i;
  1215. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1216. if (chip->pcmdevs[i]) {
  1217. atiixp_dma_t *dma = &chip->dmas[i];
  1218. if (dma->substream && dma->running)
  1219. dma->saved_curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  1220. snd_pcm_suspend_all(chip->pcmdevs[i]);
  1221. }
  1222. for (i = 0; i < NUM_ATI_CODECS; i++)
  1223. if (chip->ac97[i])
  1224. snd_ac97_suspend(chip->ac97[i]);
  1225. snd_atiixp_aclink_down(chip);
  1226. snd_atiixp_chip_stop(chip);
  1227. pci_set_power_state(chip->pci, 3);
  1228. pci_disable_device(chip->pci);
  1229. return 0;
  1230. }
  1231. static int snd_atiixp_resume(snd_card_t *card)
  1232. {
  1233. atiixp_t *chip = card->pm_private_data;
  1234. int i;
  1235. pci_enable_device(chip->pci);
  1236. pci_set_power_state(chip->pci, 0);
  1237. pci_set_master(chip->pci);
  1238. snd_atiixp_aclink_reset(chip);
  1239. snd_atiixp_chip_start(chip);
  1240. for (i = 0; i < NUM_ATI_CODECS; i++)
  1241. if (chip->ac97[i])
  1242. snd_ac97_resume(chip->ac97[i]);
  1243. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1244. if (chip->pcmdevs[i]) {
  1245. atiixp_dma_t *dma = &chip->dmas[i];
  1246. if (dma->substream && dma->running) {
  1247. dma->ops->enable_dma(chip, 1);
  1248. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  1249. chip->remap_addr + dma->ops->llp_offset);
  1250. writel(dma->saved_curptr, chip->remap_addr + dma->ops->dt_cur);
  1251. }
  1252. }
  1253. return 0;
  1254. }
  1255. #endif /* CONFIG_PM */
  1256. /*
  1257. * proc interface for register dump
  1258. */
  1259. static void snd_atiixp_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  1260. {
  1261. atiixp_t *chip = entry->private_data;
  1262. int i;
  1263. for (i = 0; i < 256; i += 4)
  1264. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  1265. }
  1266. static void __devinit snd_atiixp_proc_init(atiixp_t *chip)
  1267. {
  1268. snd_info_entry_t *entry;
  1269. if (! snd_card_proc_new(chip->card, "atiixp", &entry))
  1270. snd_info_set_text_ops(entry, chip, 1024, snd_atiixp_proc_read);
  1271. }
  1272. /*
  1273. * destructor
  1274. */
  1275. static int snd_atiixp_free(atiixp_t *chip)
  1276. {
  1277. if (chip->irq < 0)
  1278. goto __hw_end;
  1279. snd_atiixp_chip_stop(chip);
  1280. synchronize_irq(chip->irq);
  1281. __hw_end:
  1282. if (chip->irq >= 0)
  1283. free_irq(chip->irq, (void *)chip);
  1284. if (chip->remap_addr)
  1285. iounmap(chip->remap_addr);
  1286. pci_release_regions(chip->pci);
  1287. pci_disable_device(chip->pci);
  1288. kfree(chip);
  1289. return 0;
  1290. }
  1291. static int snd_atiixp_dev_free(snd_device_t *device)
  1292. {
  1293. atiixp_t *chip = device->device_data;
  1294. return snd_atiixp_free(chip);
  1295. }
  1296. /*
  1297. * constructor for chip instance
  1298. */
  1299. static int __devinit snd_atiixp_create(snd_card_t *card,
  1300. struct pci_dev *pci,
  1301. atiixp_t **r_chip)
  1302. {
  1303. static snd_device_ops_t ops = {
  1304. .dev_free = snd_atiixp_dev_free,
  1305. };
  1306. atiixp_t *chip;
  1307. int err;
  1308. if ((err = pci_enable_device(pci)) < 0)
  1309. return err;
  1310. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1311. if (chip == NULL) {
  1312. pci_disable_device(pci);
  1313. return -ENOMEM;
  1314. }
  1315. spin_lock_init(&chip->reg_lock);
  1316. init_MUTEX(&chip->open_mutex);
  1317. chip->card = card;
  1318. chip->pci = pci;
  1319. chip->irq = -1;
  1320. if ((err = pci_request_regions(pci, "ATI IXP AC97")) < 0) {
  1321. pci_disable_device(pci);
  1322. kfree(chip);
  1323. return err;
  1324. }
  1325. chip->addr = pci_resource_start(pci, 0);
  1326. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
  1327. if (chip->remap_addr == NULL) {
  1328. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1329. snd_atiixp_free(chip);
  1330. return -EIO;
  1331. }
  1332. if (request_irq(pci->irq, snd_atiixp_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
  1333. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1334. snd_atiixp_free(chip);
  1335. return -EBUSY;
  1336. }
  1337. chip->irq = pci->irq;
  1338. pci_set_master(pci);
  1339. synchronize_irq(chip->irq);
  1340. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1341. snd_atiixp_free(chip);
  1342. return err;
  1343. }
  1344. snd_card_set_dev(card, &pci->dev);
  1345. *r_chip = chip;
  1346. return 0;
  1347. }
  1348. static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  1349. const struct pci_device_id *pci_id)
  1350. {
  1351. static int dev;
  1352. snd_card_t *card;
  1353. atiixp_t *chip;
  1354. unsigned char revision;
  1355. int err;
  1356. if (dev >= SNDRV_CARDS)
  1357. return -ENODEV;
  1358. if (!enable[dev]) {
  1359. dev++;
  1360. return -ENOENT;
  1361. }
  1362. card = snd_card_new(index[dev], id[dev], 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[dev] ? "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[dev];
  1373. if ((err = snd_atiixp_mixer_new(chip, ac97_clock[dev], ac97_quirk[dev])) < 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. dev++;
  1388. return 0;
  1389. __error:
  1390. snd_card_free(card);
  1391. return err;
  1392. }
  1393. static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  1394. {
  1395. snd_card_free(pci_get_drvdata(pci));
  1396. pci_set_drvdata(pci, NULL);
  1397. }
  1398. static struct pci_driver driver = {
  1399. .name = "ATI IXP AC97 controller",
  1400. .id_table = snd_atiixp_ids,
  1401. .probe = snd_atiixp_probe,
  1402. .remove = __devexit_p(snd_atiixp_remove),
  1403. SND_PCI_PM_CALLBACKS
  1404. };
  1405. static int __init alsa_card_atiixp_init(void)
  1406. {
  1407. return pci_register_driver(&driver);
  1408. }
  1409. static void __exit alsa_card_atiixp_exit(void)
  1410. {
  1411. pci_unregister_driver(&driver);
  1412. }
  1413. module_init(alsa_card_atiixp_init)
  1414. module_exit(alsa_card_atiixp_exit)