atiixp.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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 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. set_current_state(TASK_UNINTERRUPTIBLE); \
  280. schedule_timeout(1); \
  281. } while (0)
  282. /*
  283. * handling DMA packets
  284. *
  285. * we allocate a linear buffer for the DMA, and split it to each packet.
  286. * in a future version, a scatter-gather buffer should be implemented.
  287. */
  288. #define ATI_DESC_LIST_SIZE \
  289. PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(atiixp_dma_desc_t))
  290. /*
  291. * build packets ring for the given buffer size.
  292. *
  293. * IXP handles the buffer descriptors, which are connected as a linked
  294. * list. although we can change the list dynamically, in this version,
  295. * a static RING of buffer descriptors is used.
  296. *
  297. * the ring is built in this function, and is set up to the hardware.
  298. */
  299. static int atiixp_build_dma_packets(atiixp_t *chip, atiixp_dma_t *dma,
  300. snd_pcm_substream_t *substream,
  301. unsigned int periods,
  302. unsigned int period_bytes)
  303. {
  304. unsigned int i;
  305. u32 addr, desc_addr;
  306. unsigned long flags;
  307. if (periods > ATI_MAX_DESCRIPTORS)
  308. return -ENOMEM;
  309. if (dma->desc_buf.area == NULL) {
  310. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  311. ATI_DESC_LIST_SIZE, &dma->desc_buf) < 0)
  312. return -ENOMEM;
  313. dma->period_bytes = dma->periods = 0; /* clear */
  314. }
  315. if (dma->periods == periods && dma->period_bytes == period_bytes)
  316. return 0;
  317. /* reset DMA before changing the descriptor table */
  318. spin_lock_irqsave(&chip->reg_lock, flags);
  319. writel(0, chip->remap_addr + dma->ops->llp_offset);
  320. dma->ops->enable_dma(chip, 0);
  321. dma->ops->enable_dma(chip, 1);
  322. spin_unlock_irqrestore(&chip->reg_lock, flags);
  323. /* fill the entries */
  324. addr = (u32)substream->runtime->dma_addr;
  325. desc_addr = (u32)dma->desc_buf.addr;
  326. for (i = 0; i < periods; i++) {
  327. atiixp_dma_desc_t *desc = &((atiixp_dma_desc_t *)dma->desc_buf.area)[i];
  328. desc->addr = cpu_to_le32(addr);
  329. desc->status = 0;
  330. desc->size = period_bytes >> 2; /* in dwords */
  331. desc_addr += sizeof(atiixp_dma_desc_t);
  332. if (i == periods - 1)
  333. desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
  334. else
  335. desc->next = cpu_to_le32(desc_addr);
  336. addr += period_bytes;
  337. }
  338. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  339. chip->remap_addr + dma->ops->llp_offset);
  340. dma->period_bytes = period_bytes;
  341. dma->periods = periods;
  342. return 0;
  343. }
  344. /*
  345. * remove the ring buffer and release it if assigned
  346. */
  347. static void atiixp_clear_dma_packets(atiixp_t *chip, atiixp_dma_t *dma, snd_pcm_substream_t *substream)
  348. {
  349. if (dma->desc_buf.area) {
  350. writel(0, chip->remap_addr + dma->ops->llp_offset);
  351. snd_dma_free_pages(&dma->desc_buf);
  352. dma->desc_buf.area = NULL;
  353. }
  354. }
  355. /*
  356. * AC97 interface
  357. */
  358. static int snd_atiixp_acquire_codec(atiixp_t *chip)
  359. {
  360. int timeout = 1000;
  361. while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
  362. if (! timeout--) {
  363. snd_printk(KERN_WARNING "atiixp: codec acquire timeout\n");
  364. return -EBUSY;
  365. }
  366. udelay(1);
  367. }
  368. return 0;
  369. }
  370. static unsigned short snd_atiixp_codec_read(atiixp_t *chip, unsigned short codec, unsigned short reg)
  371. {
  372. unsigned int data;
  373. int timeout;
  374. if (snd_atiixp_acquire_codec(chip) < 0)
  375. return 0xffff;
  376. data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  377. ATI_REG_PHYS_OUT_ADDR_EN |
  378. ATI_REG_PHYS_OUT_RW |
  379. codec;
  380. atiixp_write(chip, PHYS_OUT_ADDR, data);
  381. if (snd_atiixp_acquire_codec(chip) < 0)
  382. return 0xffff;
  383. timeout = 1000;
  384. do {
  385. data = atiixp_read(chip, PHYS_IN_ADDR);
  386. if (data & ATI_REG_PHYS_IN_READ_FLAG)
  387. return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
  388. udelay(1);
  389. } while (--timeout);
  390. /* time out may happen during reset */
  391. if (reg < 0x7c)
  392. snd_printk(KERN_WARNING "atiixp: codec read timeout (reg %x)\n", reg);
  393. return 0xffff;
  394. }
  395. static void snd_atiixp_codec_write(atiixp_t *chip, unsigned short codec, unsigned short reg, unsigned short val)
  396. {
  397. unsigned int data;
  398. if (snd_atiixp_acquire_codec(chip) < 0)
  399. return;
  400. data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
  401. ((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  402. ATI_REG_PHYS_OUT_ADDR_EN | codec;
  403. atiixp_write(chip, PHYS_OUT_ADDR, data);
  404. }
  405. static unsigned short snd_atiixp_ac97_read(ac97_t *ac97, unsigned short reg)
  406. {
  407. atiixp_t *chip = ac97->private_data;
  408. return snd_atiixp_codec_read(chip, ac97->num, reg);
  409. }
  410. static void snd_atiixp_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
  411. {
  412. atiixp_t *chip = ac97->private_data;
  413. snd_atiixp_codec_write(chip, ac97->num, reg, val);
  414. }
  415. /*
  416. * reset AC link
  417. */
  418. static int snd_atiixp_aclink_reset(atiixp_t *chip)
  419. {
  420. int timeout;
  421. /* reset powerdoewn */
  422. if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
  423. udelay(10);
  424. /* perform a software reset */
  425. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
  426. atiixp_read(chip, CMD);
  427. udelay(10);
  428. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
  429. timeout = 10;
  430. while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
  431. /* do a hard reset */
  432. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  433. ATI_REG_CMD_AC_SYNC);
  434. atiixp_read(chip, CMD);
  435. do_delay();
  436. atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
  437. if (--timeout) {
  438. snd_printk(KERN_ERR "atiixp: codec reset timeout\n");
  439. break;
  440. }
  441. }
  442. /* deassert RESET and assert SYNC to make sure */
  443. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  444. ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
  445. return 0;
  446. }
  447. #ifdef CONFIG_PM
  448. static int snd_atiixp_aclink_down(atiixp_t *chip)
  449. {
  450. // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
  451. // return -EBUSY;
  452. atiixp_update(chip, CMD,
  453. ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
  454. ATI_REG_CMD_POWERDOWN);
  455. return 0;
  456. }
  457. #endif
  458. /*
  459. * auto-detection of codecs
  460. *
  461. * the IXP chip can generate interrupts for the non-existing codecs.
  462. * NEW_FRAME interrupt is used to make sure that the interrupt is generated
  463. * even if all three codecs are connected.
  464. */
  465. #define ALL_CODEC_NOT_READY \
  466. (ATI_REG_ISR_CODEC0_NOT_READY |\
  467. ATI_REG_ISR_CODEC1_NOT_READY |\
  468. ATI_REG_ISR_CODEC2_NOT_READY)
  469. #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
  470. static int snd_atiixp_codec_detect(atiixp_t *chip)
  471. {
  472. int timeout;
  473. chip->codec_not_ready_bits = 0;
  474. atiixp_write(chip, IER, CODEC_CHECK_BITS);
  475. /* wait for the interrupts */
  476. timeout = HZ / 10;
  477. while (timeout-- > 0) {
  478. do_delay();
  479. if (chip->codec_not_ready_bits)
  480. break;
  481. }
  482. atiixp_write(chip, IER, 0); /* disable irqs */
  483. if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
  484. snd_printk(KERN_ERR "atiixp: no codec detected!\n");
  485. return -ENXIO;
  486. }
  487. return 0;
  488. }
  489. /*
  490. * enable DMA and irqs
  491. */
  492. static int snd_atiixp_chip_start(atiixp_t *chip)
  493. {
  494. unsigned int reg;
  495. /* set up spdif, enable burst mode */
  496. reg = atiixp_read(chip, CMD);
  497. reg |= 0x02 << ATI_REG_CMD_SPDF_THRESHOLD_SHIFT;
  498. reg |= ATI_REG_CMD_BURST_EN;
  499. atiixp_write(chip, CMD, reg);
  500. reg = atiixp_read(chip, SPDF_CMD);
  501. reg &= ~(ATI_REG_SPDF_CMD_LFSR|ATI_REG_SPDF_CMD_SINGLE_CH);
  502. atiixp_write(chip, SPDF_CMD, reg);
  503. /* clear all interrupt source */
  504. atiixp_write(chip, ISR, 0xffffffff);
  505. /* enable irqs */
  506. atiixp_write(chip, IER,
  507. ATI_REG_IER_IO_STATUS_EN |
  508. ATI_REG_IER_IN_XRUN_EN |
  509. ATI_REG_IER_OUT_XRUN_EN |
  510. ATI_REG_IER_SPDF_XRUN_EN |
  511. ATI_REG_IER_SPDF_STATUS_EN);
  512. return 0;
  513. }
  514. /*
  515. * disable DMA and IRQs
  516. */
  517. static int snd_atiixp_chip_stop(atiixp_t *chip)
  518. {
  519. /* clear interrupt source */
  520. atiixp_write(chip, ISR, atiixp_read(chip, ISR));
  521. /* disable irqs */
  522. atiixp_write(chip, IER, 0);
  523. return 0;
  524. }
  525. /*
  526. * PCM section
  527. */
  528. /*
  529. * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
  530. * position. when SG-buffer is implemented, the offset must be calculated
  531. * correctly...
  532. */
  533. static snd_pcm_uframes_t snd_atiixp_pcm_pointer(snd_pcm_substream_t *substream)
  534. {
  535. atiixp_t *chip = snd_pcm_substream_chip(substream);
  536. snd_pcm_runtime_t *runtime = substream->runtime;
  537. atiixp_dma_t *dma = (atiixp_dma_t *)runtime->private_data;
  538. unsigned int curptr;
  539. int timeout = 1000;
  540. while (timeout--) {
  541. curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  542. if (curptr < dma->buf_addr)
  543. continue;
  544. curptr -= dma->buf_addr;
  545. if (curptr >= dma->buf_bytes)
  546. continue;
  547. return bytes_to_frames(runtime, curptr);
  548. }
  549. snd_printd("atiixp: invalid DMA pointer read 0x%x (buf=%x)\n",
  550. readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
  551. return 0;
  552. }
  553. /*
  554. * XRUN detected, and stop the PCM substream
  555. */
  556. static void snd_atiixp_xrun_dma(atiixp_t *chip, atiixp_dma_t *dma)
  557. {
  558. if (! dma->substream || ! dma->running)
  559. return;
  560. snd_printdd("atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
  561. snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
  562. }
  563. /*
  564. * the period ack. update the substream.
  565. */
  566. static void snd_atiixp_update_dma(atiixp_t *chip, atiixp_dma_t *dma)
  567. {
  568. if (! dma->substream || ! dma->running)
  569. return;
  570. snd_pcm_period_elapsed(dma->substream);
  571. }
  572. /* set BUS_BUSY interrupt bit if any DMA is running */
  573. /* call with spinlock held */
  574. static void snd_atiixp_check_bus_busy(atiixp_t *chip)
  575. {
  576. unsigned int bus_busy;
  577. if (atiixp_read(chip, CMD) & (ATI_REG_CMD_SEND_EN |
  578. ATI_REG_CMD_RECEIVE_EN |
  579. ATI_REG_CMD_SPDF_OUT_EN))
  580. bus_busy = ATI_REG_IER_SET_BUS_BUSY;
  581. else
  582. bus_busy = 0;
  583. atiixp_update(chip, IER, ATI_REG_IER_SET_BUS_BUSY, bus_busy);
  584. }
  585. /* common trigger callback
  586. * calling the lowlevel callbacks in it
  587. */
  588. static int snd_atiixp_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  589. {
  590. atiixp_t *chip = snd_pcm_substream_chip(substream);
  591. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  592. int err = 0;
  593. snd_assert(dma->ops->enable_transfer && dma->ops->flush_dma, return -EINVAL);
  594. spin_lock(&chip->reg_lock);
  595. switch (cmd) {
  596. case SNDRV_PCM_TRIGGER_START:
  597. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  598. case SNDRV_PCM_TRIGGER_RESUME:
  599. dma->ops->enable_transfer(chip, 1);
  600. dma->running = 1;
  601. dma->suspended = 0;
  602. break;
  603. case SNDRV_PCM_TRIGGER_STOP:
  604. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  605. case SNDRV_PCM_TRIGGER_SUSPEND:
  606. dma->ops->enable_transfer(chip, 0);
  607. dma->running = 0;
  608. dma->suspended = cmd == SNDRV_PCM_TRIGGER_SUSPEND;
  609. break;
  610. default:
  611. err = -EINVAL;
  612. break;
  613. }
  614. if (! err) {
  615. snd_atiixp_check_bus_busy(chip);
  616. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  617. dma->ops->flush_dma(chip);
  618. snd_atiixp_check_bus_busy(chip);
  619. }
  620. }
  621. spin_unlock(&chip->reg_lock);
  622. return err;
  623. }
  624. /*
  625. * lowlevel callbacks for each DMA type
  626. *
  627. * every callback is supposed to be called in chip->reg_lock spinlock
  628. */
  629. /* flush FIFO of analog OUT DMA */
  630. static void atiixp_out_flush_dma(atiixp_t *chip)
  631. {
  632. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_OUT_FLUSH);
  633. }
  634. /* enable/disable analog OUT DMA */
  635. static void atiixp_out_enable_dma(atiixp_t *chip, int on)
  636. {
  637. unsigned int data;
  638. data = atiixp_read(chip, CMD);
  639. if (on) {
  640. if (data & ATI_REG_CMD_OUT_DMA_EN)
  641. return;
  642. atiixp_out_flush_dma(chip);
  643. data |= ATI_REG_CMD_OUT_DMA_EN;
  644. } else
  645. data &= ~ATI_REG_CMD_OUT_DMA_EN;
  646. atiixp_write(chip, CMD, data);
  647. }
  648. /* start/stop transfer over OUT DMA */
  649. static void atiixp_out_enable_transfer(atiixp_t *chip, int on)
  650. {
  651. atiixp_update(chip, CMD, ATI_REG_CMD_SEND_EN,
  652. on ? ATI_REG_CMD_SEND_EN : 0);
  653. }
  654. /* enable/disable analog IN DMA */
  655. static void atiixp_in_enable_dma(atiixp_t *chip, int on)
  656. {
  657. atiixp_update(chip, CMD, ATI_REG_CMD_IN_DMA_EN,
  658. on ? ATI_REG_CMD_IN_DMA_EN : 0);
  659. }
  660. /* start/stop analog IN DMA */
  661. static void atiixp_in_enable_transfer(atiixp_t *chip, int on)
  662. {
  663. if (on) {
  664. unsigned int data = atiixp_read(chip, CMD);
  665. if (! (data & ATI_REG_CMD_RECEIVE_EN)) {
  666. data |= ATI_REG_CMD_RECEIVE_EN;
  667. #if 0 /* FIXME: this causes the endless loop */
  668. /* wait until slot 3/4 are finished */
  669. while ((atiixp_read(chip, COUNTER) &
  670. ATI_REG_COUNTER_SLOT) != 5)
  671. ;
  672. #endif
  673. atiixp_write(chip, CMD, data);
  674. }
  675. } else
  676. atiixp_update(chip, CMD, ATI_REG_CMD_RECEIVE_EN, 0);
  677. }
  678. /* flush FIFO of analog IN DMA */
  679. static void atiixp_in_flush_dma(atiixp_t *chip)
  680. {
  681. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_IN_FLUSH);
  682. }
  683. /* enable/disable SPDIF OUT DMA */
  684. static void atiixp_spdif_enable_dma(atiixp_t *chip, int on)
  685. {
  686. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_DMA_EN,
  687. on ? ATI_REG_CMD_SPDF_DMA_EN : 0);
  688. }
  689. /* start/stop SPDIF OUT DMA */
  690. static void atiixp_spdif_enable_transfer(atiixp_t *chip, int on)
  691. {
  692. unsigned int data;
  693. data = atiixp_read(chip, CMD);
  694. if (on)
  695. data |= ATI_REG_CMD_SPDF_OUT_EN;
  696. else
  697. data &= ~ATI_REG_CMD_SPDF_OUT_EN;
  698. atiixp_write(chip, CMD, data);
  699. }
  700. /* flush FIFO of SPDIF OUT DMA */
  701. static void atiixp_spdif_flush_dma(atiixp_t *chip)
  702. {
  703. int timeout;
  704. /* DMA off, transfer on */
  705. atiixp_spdif_enable_dma(chip, 0);
  706. atiixp_spdif_enable_transfer(chip, 1);
  707. timeout = 100;
  708. do {
  709. if (! (atiixp_read(chip, SPDF_DMA_DT_SIZE) & ATI_REG_DMA_FIFO_USED))
  710. break;
  711. udelay(1);
  712. } while (timeout-- > 0);
  713. atiixp_spdif_enable_transfer(chip, 0);
  714. }
  715. /* set up slots and formats for SPDIF OUT */
  716. static int snd_atiixp_spdif_prepare(snd_pcm_substream_t *substream)
  717. {
  718. atiixp_t *chip = snd_pcm_substream_chip(substream);
  719. spin_lock_irq(&chip->reg_lock);
  720. if (chip->spdif_over_aclink) {
  721. unsigned int data;
  722. /* enable slots 10/11 */
  723. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK,
  724. ATI_REG_CMD_SPDF_CONFIG_01);
  725. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  726. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  727. ATI_REG_OUT_DMA_SLOT_BIT(11);
  728. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  729. atiixp_write(chip, OUT_DMA_SLOT, data);
  730. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  731. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  732. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  733. } else {
  734. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
  735. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
  736. }
  737. spin_unlock_irq(&chip->reg_lock);
  738. return 0;
  739. }
  740. /* set up slots and formats for analog OUT */
  741. static int snd_atiixp_playback_prepare(snd_pcm_substream_t *substream)
  742. {
  743. atiixp_t *chip = snd_pcm_substream_chip(substream);
  744. unsigned int data;
  745. spin_lock_irq(&chip->reg_lock);
  746. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  747. switch (substream->runtime->channels) {
  748. case 8:
  749. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  750. ATI_REG_OUT_DMA_SLOT_BIT(11);
  751. /* fallthru */
  752. case 6:
  753. data |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
  754. ATI_REG_OUT_DMA_SLOT_BIT(8);
  755. /* fallthru */
  756. case 4:
  757. data |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
  758. ATI_REG_OUT_DMA_SLOT_BIT(9);
  759. /* fallthru */
  760. default:
  761. data |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
  762. ATI_REG_OUT_DMA_SLOT_BIT(4);
  763. break;
  764. }
  765. /* set output threshold */
  766. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  767. atiixp_write(chip, OUT_DMA_SLOT, data);
  768. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  769. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  770. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  771. /*
  772. * enable 6 channel re-ordering bit if needed
  773. */
  774. atiixp_update(chip, 6CH_REORDER, ATI_REG_6CH_REORDER_EN,
  775. substream->runtime->channels >= 6 ? ATI_REG_6CH_REORDER_EN: 0);
  776. spin_unlock_irq(&chip->reg_lock);
  777. return 0;
  778. }
  779. /* set up slots and formats for analog IN */
  780. static int snd_atiixp_capture_prepare(snd_pcm_substream_t *substream)
  781. {
  782. atiixp_t *chip = snd_pcm_substream_chip(substream);
  783. spin_lock_irq(&chip->reg_lock);
  784. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_IN,
  785. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  786. ATI_REG_CMD_INTERLEAVE_IN : 0);
  787. spin_unlock_irq(&chip->reg_lock);
  788. return 0;
  789. }
  790. /*
  791. * hw_params - allocate the buffer and set up buffer descriptors
  792. */
  793. static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
  794. snd_pcm_hw_params_t *hw_params)
  795. {
  796. atiixp_t *chip = snd_pcm_substream_chip(substream);
  797. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  798. int err;
  799. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  800. if (err < 0)
  801. return err;
  802. dma->buf_addr = substream->runtime->dma_addr;
  803. dma->buf_bytes = params_buffer_bytes(hw_params);
  804. err = atiixp_build_dma_packets(chip, dma, substream,
  805. params_periods(hw_params),
  806. params_period_bytes(hw_params));
  807. if (err < 0)
  808. return err;
  809. if (dma->ac97_pcm_type >= 0) {
  810. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  811. /* PCM is bound to AC97 codec(s)
  812. * set up the AC97 codecs
  813. */
  814. if (dma->pcm_open_flag) {
  815. snd_ac97_pcm_close(pcm);
  816. dma->pcm_open_flag = 0;
  817. }
  818. err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
  819. params_channels(hw_params),
  820. pcm->r[0].slots);
  821. if (err >= 0)
  822. dma->pcm_open_flag = 1;
  823. }
  824. return err;
  825. }
  826. static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
  827. {
  828. atiixp_t *chip = snd_pcm_substream_chip(substream);
  829. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  830. if (dma->pcm_open_flag) {
  831. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  832. snd_ac97_pcm_close(pcm);
  833. dma->pcm_open_flag = 0;
  834. }
  835. atiixp_clear_dma_packets(chip, dma, substream);
  836. snd_pcm_lib_free_pages(substream);
  837. return 0;
  838. }
  839. /*
  840. * pcm hardware definition, identical for all DMA types
  841. */
  842. static snd_pcm_hardware_t snd_atiixp_pcm_hw =
  843. {
  844. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  845. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  846. SNDRV_PCM_INFO_PAUSE |
  847. SNDRV_PCM_INFO_RESUME |
  848. SNDRV_PCM_INFO_MMAP_VALID),
  849. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  850. .rates = SNDRV_PCM_RATE_48000,
  851. .rate_min = 48000,
  852. .rate_max = 48000,
  853. .channels_min = 2,
  854. .channels_max = 2,
  855. .buffer_bytes_max = 256 * 1024,
  856. .period_bytes_min = 32,
  857. .period_bytes_max = 128 * 1024,
  858. .periods_min = 2,
  859. .periods_max = ATI_MAX_DESCRIPTORS,
  860. };
  861. static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
  862. {
  863. atiixp_t *chip = snd_pcm_substream_chip(substream);
  864. snd_pcm_runtime_t *runtime = substream->runtime;
  865. int err;
  866. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  867. if (dma->opened)
  868. return -EBUSY;
  869. dma->substream = substream;
  870. runtime->hw = snd_atiixp_pcm_hw;
  871. dma->ac97_pcm_type = pcm_type;
  872. if (pcm_type >= 0) {
  873. runtime->hw.rates = chip->pcms[pcm_type]->rates;
  874. snd_pcm_limit_hw_rates(runtime);
  875. } else {
  876. /* direct SPDIF */
  877. runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  878. }
  879. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  880. return err;
  881. runtime->private_data = dma;
  882. /* enable DMA bits */
  883. spin_lock_irq(&chip->reg_lock);
  884. dma->ops->enable_dma(chip, 1);
  885. spin_unlock_irq(&chip->reg_lock);
  886. dma->opened = 1;
  887. return 0;
  888. }
  889. static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
  890. {
  891. atiixp_t *chip = snd_pcm_substream_chip(substream);
  892. /* disable DMA bits */
  893. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  894. spin_lock_irq(&chip->reg_lock);
  895. dma->ops->enable_dma(chip, 0);
  896. spin_unlock_irq(&chip->reg_lock);
  897. dma->substream = NULL;
  898. dma->opened = 0;
  899. return 0;
  900. }
  901. /*
  902. */
  903. static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
  904. {
  905. atiixp_t *chip = snd_pcm_substream_chip(substream);
  906. int err;
  907. down(&chip->open_mutex);
  908. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  909. up(&chip->open_mutex);
  910. if (err < 0)
  911. return err;
  912. substream->runtime->hw.channels_max = chip->max_channels;
  913. if (chip->max_channels > 2)
  914. /* channels must be even */
  915. snd_pcm_hw_constraint_step(substream->runtime, 0,
  916. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  917. return 0;
  918. }
  919. static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
  920. {
  921. atiixp_t *chip = snd_pcm_substream_chip(substream);
  922. int err;
  923. down(&chip->open_mutex);
  924. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  925. up(&chip->open_mutex);
  926. return err;
  927. }
  928. static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
  929. {
  930. atiixp_t *chip = snd_pcm_substream_chip(substream);
  931. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  932. }
  933. static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
  934. {
  935. atiixp_t *chip = snd_pcm_substream_chip(substream);
  936. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  937. }
  938. static int snd_atiixp_spdif_open(snd_pcm_substream_t *substream)
  939. {
  940. atiixp_t *chip = snd_pcm_substream_chip(substream);
  941. int err;
  942. down(&chip->open_mutex);
  943. if (chip->spdif_over_aclink) /* share DMA_PLAYBACK */
  944. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 2);
  945. else
  946. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
  947. up(&chip->open_mutex);
  948. return err;
  949. }
  950. static int snd_atiixp_spdif_close(snd_pcm_substream_t *substream)
  951. {
  952. atiixp_t *chip = snd_pcm_substream_chip(substream);
  953. int err;
  954. down(&chip->open_mutex);
  955. if (chip->spdif_over_aclink)
  956. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  957. else
  958. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
  959. up(&chip->open_mutex);
  960. return err;
  961. }
  962. /* AC97 playback */
  963. static snd_pcm_ops_t snd_atiixp_playback_ops = {
  964. .open = snd_atiixp_playback_open,
  965. .close = snd_atiixp_playback_close,
  966. .ioctl = snd_pcm_lib_ioctl,
  967. .hw_params = snd_atiixp_pcm_hw_params,
  968. .hw_free = snd_atiixp_pcm_hw_free,
  969. .prepare = snd_atiixp_playback_prepare,
  970. .trigger = snd_atiixp_pcm_trigger,
  971. .pointer = snd_atiixp_pcm_pointer,
  972. };
  973. /* AC97 capture */
  974. static snd_pcm_ops_t snd_atiixp_capture_ops = {
  975. .open = snd_atiixp_capture_open,
  976. .close = snd_atiixp_capture_close,
  977. .ioctl = snd_pcm_lib_ioctl,
  978. .hw_params = snd_atiixp_pcm_hw_params,
  979. .hw_free = snd_atiixp_pcm_hw_free,
  980. .prepare = snd_atiixp_capture_prepare,
  981. .trigger = snd_atiixp_pcm_trigger,
  982. .pointer = snd_atiixp_pcm_pointer,
  983. };
  984. /* SPDIF playback */
  985. static snd_pcm_ops_t snd_atiixp_spdif_ops = {
  986. .open = snd_atiixp_spdif_open,
  987. .close = snd_atiixp_spdif_close,
  988. .ioctl = snd_pcm_lib_ioctl,
  989. .hw_params = snd_atiixp_pcm_hw_params,
  990. .hw_free = snd_atiixp_pcm_hw_free,
  991. .prepare = snd_atiixp_spdif_prepare,
  992. .trigger = snd_atiixp_pcm_trigger,
  993. .pointer = snd_atiixp_pcm_pointer,
  994. };
  995. static struct ac97_pcm atiixp_pcm_defs[] __devinitdata = {
  996. /* front PCM */
  997. {
  998. .exclusive = 1,
  999. .r = { {
  1000. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  1001. (1 << AC97_SLOT_PCM_RIGHT) |
  1002. (1 << AC97_SLOT_PCM_CENTER) |
  1003. (1 << AC97_SLOT_PCM_SLEFT) |
  1004. (1 << AC97_SLOT_PCM_SRIGHT) |
  1005. (1 << AC97_SLOT_LFE)
  1006. }
  1007. }
  1008. },
  1009. /* PCM IN #1 */
  1010. {
  1011. .stream = 1,
  1012. .exclusive = 1,
  1013. .r = { {
  1014. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  1015. (1 << AC97_SLOT_PCM_RIGHT)
  1016. }
  1017. }
  1018. },
  1019. /* S/PDIF OUT (optional) */
  1020. {
  1021. .exclusive = 1,
  1022. .spdif = 1,
  1023. .r = { {
  1024. .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
  1025. (1 << AC97_SLOT_SPDIF_RIGHT2)
  1026. }
  1027. }
  1028. },
  1029. };
  1030. static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
  1031. .type = ATI_DMA_PLAYBACK,
  1032. .llp_offset = ATI_REG_OUT_DMA_LINKPTR,
  1033. .dt_cur = ATI_REG_OUT_DMA_DT_CUR,
  1034. .enable_dma = atiixp_out_enable_dma,
  1035. .enable_transfer = atiixp_out_enable_transfer,
  1036. .flush_dma = atiixp_out_flush_dma,
  1037. };
  1038. static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
  1039. .type = ATI_DMA_CAPTURE,
  1040. .llp_offset = ATI_REG_IN_DMA_LINKPTR,
  1041. .dt_cur = ATI_REG_IN_DMA_DT_CUR,
  1042. .enable_dma = atiixp_in_enable_dma,
  1043. .enable_transfer = atiixp_in_enable_transfer,
  1044. .flush_dma = atiixp_in_flush_dma,
  1045. };
  1046. static atiixp_dma_ops_t snd_atiixp_spdif_dma_ops = {
  1047. .type = ATI_DMA_SPDIF,
  1048. .llp_offset = ATI_REG_SPDF_DMA_LINKPTR,
  1049. .dt_cur = ATI_REG_SPDF_DMA_DT_CUR,
  1050. .enable_dma = atiixp_spdif_enable_dma,
  1051. .enable_transfer = atiixp_spdif_enable_transfer,
  1052. .flush_dma = atiixp_spdif_flush_dma,
  1053. };
  1054. static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
  1055. {
  1056. snd_pcm_t *pcm;
  1057. ac97_bus_t *pbus = chip->ac97_bus;
  1058. int err, i, num_pcms;
  1059. /* initialize constants */
  1060. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  1061. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  1062. if (! chip->spdif_over_aclink)
  1063. chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
  1064. /* assign AC97 pcm */
  1065. if (chip->spdif_over_aclink)
  1066. num_pcms = 3;
  1067. else
  1068. num_pcms = 2;
  1069. err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
  1070. if (err < 0)
  1071. return err;
  1072. for (i = 0; i < num_pcms; i++)
  1073. chip->pcms[i] = &pbus->pcms[i];
  1074. chip->max_channels = 2;
  1075. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  1076. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_LFE))
  1077. chip->max_channels = 6;
  1078. else
  1079. chip->max_channels = 4;
  1080. }
  1081. /* PCM #0: analog I/O */
  1082. err = snd_pcm_new(chip->card, "ATI IXP AC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  1083. if (err < 0)
  1084. return err;
  1085. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  1086. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  1087. pcm->private_data = chip;
  1088. strcpy(pcm->name, "ATI IXP AC97");
  1089. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  1090. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1091. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  1092. /* no SPDIF support on codec? */
  1093. if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
  1094. return 0;
  1095. /* FIXME: non-48k sample rate doesn't work on my test machine with AD1888 */
  1096. if (chip->pcms[ATI_PCM_SPDIF])
  1097. chip->pcms[ATI_PCM_SPDIF]->rates = SNDRV_PCM_RATE_48000;
  1098. /* PCM #1: spdif playback */
  1099. err = snd_pcm_new(chip->card, "ATI IXP IEC958", ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
  1100. if (err < 0)
  1101. return err;
  1102. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_spdif_ops);
  1103. pcm->private_data = chip;
  1104. if (chip->spdif_over_aclink)
  1105. strcpy(pcm->name, "ATI IXP IEC958 (AC97)");
  1106. else
  1107. strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
  1108. chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
  1109. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1110. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  1111. /* pre-select AC97 SPDIF slots 10/11 */
  1112. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1113. if (chip->ac97[i])
  1114. snd_ac97_update_bits(chip->ac97[i], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
  1115. }
  1116. return 0;
  1117. }
  1118. /*
  1119. * interrupt handler
  1120. */
  1121. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1122. {
  1123. atiixp_t *chip = dev_id;
  1124. unsigned int status;
  1125. status = atiixp_read(chip, ISR);
  1126. if (! status)
  1127. return IRQ_NONE;
  1128. /* process audio DMA */
  1129. if (status & ATI_REG_ISR_OUT_XRUN)
  1130. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1131. else if (status & ATI_REG_ISR_OUT_STATUS)
  1132. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1133. if (status & ATI_REG_ISR_IN_XRUN)
  1134. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1135. else if (status & ATI_REG_ISR_IN_STATUS)
  1136. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1137. if (! chip->spdif_over_aclink) {
  1138. if (status & ATI_REG_ISR_SPDF_XRUN)
  1139. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1140. else if (status & ATI_REG_ISR_SPDF_STATUS)
  1141. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1142. }
  1143. /* for codec detection */
  1144. if (status & CODEC_CHECK_BITS) {
  1145. unsigned int detected;
  1146. detected = status & CODEC_CHECK_BITS;
  1147. spin_lock(&chip->reg_lock);
  1148. chip->codec_not_ready_bits |= detected;
  1149. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  1150. spin_unlock(&chip->reg_lock);
  1151. }
  1152. /* ack */
  1153. atiixp_write(chip, ISR, status);
  1154. return IRQ_HANDLED;
  1155. }
  1156. /*
  1157. * ac97 mixer section
  1158. */
  1159. static struct ac97_quirk ac97_quirks[] __devinitdata = {
  1160. {
  1161. .subvendor = 0x103c,
  1162. .subdevice = 0x006b,
  1163. .name = "HP Pavilion ZV5030US",
  1164. .type = AC97_TUNE_MUTE_LED
  1165. },
  1166. { } /* terminator */
  1167. };
  1168. static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock, const char *quirk_override)
  1169. {
  1170. ac97_bus_t *pbus;
  1171. ac97_template_t ac97;
  1172. int i, err;
  1173. int codec_count;
  1174. static ac97_bus_ops_t ops = {
  1175. .write = snd_atiixp_ac97_write,
  1176. .read = snd_atiixp_ac97_read,
  1177. };
  1178. static unsigned int codec_skip[NUM_ATI_CODECS] = {
  1179. ATI_REG_ISR_CODEC0_NOT_READY,
  1180. ATI_REG_ISR_CODEC1_NOT_READY,
  1181. ATI_REG_ISR_CODEC2_NOT_READY,
  1182. };
  1183. if (snd_atiixp_codec_detect(chip) < 0)
  1184. return -ENXIO;
  1185. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  1186. return err;
  1187. pbus->clock = clock;
  1188. pbus->shared_type = AC97_SHARED_TYPE_ATIIXP; /* shared with modem driver */
  1189. chip->ac97_bus = pbus;
  1190. codec_count = 0;
  1191. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1192. if (chip->codec_not_ready_bits & codec_skip[i])
  1193. continue;
  1194. memset(&ac97, 0, sizeof(ac97));
  1195. ac97.private_data = chip;
  1196. ac97.pci = chip->pci;
  1197. ac97.num = i;
  1198. ac97.scaps = AC97_SCAP_SKIP_MODEM;
  1199. if (! chip->spdif_over_aclink)
  1200. ac97.scaps |= AC97_SCAP_NO_SPDIF;
  1201. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  1202. chip->ac97[i] = NULL; /* to be sure */
  1203. snd_printdd("atiixp: codec %d not available for audio\n", i);
  1204. continue;
  1205. }
  1206. codec_count++;
  1207. }
  1208. if (! codec_count) {
  1209. snd_printk(KERN_ERR "atiixp: no codec available\n");
  1210. return -ENODEV;
  1211. }
  1212. snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
  1213. return 0;
  1214. }
  1215. #ifdef CONFIG_PM
  1216. /*
  1217. * power management
  1218. */
  1219. static int snd_atiixp_suspend(snd_card_t *card, pm_message_t state)
  1220. {
  1221. atiixp_t *chip = card->pm_private_data;
  1222. int i;
  1223. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1224. if (chip->pcmdevs[i]) {
  1225. atiixp_dma_t *dma = &chip->dmas[i];
  1226. if (dma->substream && dma->running)
  1227. dma->saved_curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  1228. snd_pcm_suspend_all(chip->pcmdevs[i]);
  1229. }
  1230. for (i = 0; i < NUM_ATI_CODECS; i++)
  1231. if (chip->ac97[i])
  1232. snd_ac97_suspend(chip->ac97[i]);
  1233. snd_atiixp_aclink_down(chip);
  1234. snd_atiixp_chip_stop(chip);
  1235. pci_set_power_state(chip->pci, PCI_D3hot);
  1236. pci_disable_device(chip->pci);
  1237. return 0;
  1238. }
  1239. static int snd_atiixp_resume(snd_card_t *card)
  1240. {
  1241. atiixp_t *chip = card->pm_private_data;
  1242. int i;
  1243. pci_enable_device(chip->pci);
  1244. pci_set_power_state(chip->pci, PCI_D0);
  1245. pci_set_master(chip->pci);
  1246. snd_atiixp_aclink_reset(chip);
  1247. snd_atiixp_chip_start(chip);
  1248. for (i = 0; i < NUM_ATI_CODECS; i++)
  1249. if (chip->ac97[i])
  1250. snd_ac97_resume(chip->ac97[i]);
  1251. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1252. if (chip->pcmdevs[i]) {
  1253. atiixp_dma_t *dma = &chip->dmas[i];
  1254. if (dma->substream && dma->suspended) {
  1255. dma->ops->enable_dma(chip, 1);
  1256. dma->substream->ops->prepare(dma->substream);
  1257. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  1258. chip->remap_addr + dma->ops->llp_offset);
  1259. writel(dma->saved_curptr, chip->remap_addr + dma->ops->dt_cur);
  1260. }
  1261. }
  1262. return 0;
  1263. }
  1264. #endif /* CONFIG_PM */
  1265. /*
  1266. * proc interface for register dump
  1267. */
  1268. static void snd_atiixp_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  1269. {
  1270. atiixp_t *chip = entry->private_data;
  1271. int i;
  1272. for (i = 0; i < 256; i += 4)
  1273. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  1274. }
  1275. static void __devinit snd_atiixp_proc_init(atiixp_t *chip)
  1276. {
  1277. snd_info_entry_t *entry;
  1278. if (! snd_card_proc_new(chip->card, "atiixp", &entry))
  1279. snd_info_set_text_ops(entry, chip, 1024, snd_atiixp_proc_read);
  1280. }
  1281. /*
  1282. * destructor
  1283. */
  1284. static int snd_atiixp_free(atiixp_t *chip)
  1285. {
  1286. if (chip->irq < 0)
  1287. goto __hw_end;
  1288. snd_atiixp_chip_stop(chip);
  1289. synchronize_irq(chip->irq);
  1290. __hw_end:
  1291. if (chip->irq >= 0)
  1292. free_irq(chip->irq, (void *)chip);
  1293. if (chip->remap_addr)
  1294. iounmap(chip->remap_addr);
  1295. pci_release_regions(chip->pci);
  1296. pci_disable_device(chip->pci);
  1297. kfree(chip);
  1298. return 0;
  1299. }
  1300. static int snd_atiixp_dev_free(snd_device_t *device)
  1301. {
  1302. atiixp_t *chip = device->device_data;
  1303. return snd_atiixp_free(chip);
  1304. }
  1305. /*
  1306. * constructor for chip instance
  1307. */
  1308. static int __devinit snd_atiixp_create(snd_card_t *card,
  1309. struct pci_dev *pci,
  1310. atiixp_t **r_chip)
  1311. {
  1312. static snd_device_ops_t ops = {
  1313. .dev_free = snd_atiixp_dev_free,
  1314. };
  1315. atiixp_t *chip;
  1316. int err;
  1317. if ((err = pci_enable_device(pci)) < 0)
  1318. return err;
  1319. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1320. if (chip == NULL) {
  1321. pci_disable_device(pci);
  1322. return -ENOMEM;
  1323. }
  1324. spin_lock_init(&chip->reg_lock);
  1325. init_MUTEX(&chip->open_mutex);
  1326. chip->card = card;
  1327. chip->pci = pci;
  1328. chip->irq = -1;
  1329. if ((err = pci_request_regions(pci, "ATI IXP AC97")) < 0) {
  1330. pci_disable_device(pci);
  1331. kfree(chip);
  1332. return err;
  1333. }
  1334. chip->addr = pci_resource_start(pci, 0);
  1335. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
  1336. if (chip->remap_addr == NULL) {
  1337. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1338. snd_atiixp_free(chip);
  1339. return -EIO;
  1340. }
  1341. if (request_irq(pci->irq, snd_atiixp_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
  1342. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1343. snd_atiixp_free(chip);
  1344. return -EBUSY;
  1345. }
  1346. chip->irq = pci->irq;
  1347. pci_set_master(pci);
  1348. synchronize_irq(chip->irq);
  1349. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1350. snd_atiixp_free(chip);
  1351. return err;
  1352. }
  1353. snd_card_set_dev(card, &pci->dev);
  1354. *r_chip = chip;
  1355. return 0;
  1356. }
  1357. static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  1358. const struct pci_device_id *pci_id)
  1359. {
  1360. static int dev;
  1361. snd_card_t *card;
  1362. atiixp_t *chip;
  1363. unsigned char revision;
  1364. int err;
  1365. if (dev >= SNDRV_CARDS)
  1366. return -ENODEV;
  1367. if (!enable[dev]) {
  1368. dev++;
  1369. return -ENOENT;
  1370. }
  1371. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1372. if (card == NULL)
  1373. return -ENOMEM;
  1374. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  1375. strcpy(card->driver, spdif_aclink[dev] ? "ATIIXP" : "ATIIXP-SPDMA");
  1376. strcpy(card->shortname, "ATI IXP");
  1377. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1378. goto __error;
  1379. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1380. goto __error;
  1381. chip->spdif_over_aclink = spdif_aclink[dev];
  1382. if ((err = snd_atiixp_mixer_new(chip, ac97_clock[dev], ac97_quirk[dev])) < 0)
  1383. goto __error;
  1384. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1385. goto __error;
  1386. snd_atiixp_proc_init(chip);
  1387. snd_atiixp_chip_start(chip);
  1388. snprintf(card->longname, sizeof(card->longname),
  1389. "%s rev %x with %s at %#lx, irq %i", card->shortname, revision,
  1390. chip->ac97[0] ? snd_ac97_get_short_name(chip->ac97[0]) : "?",
  1391. chip->addr, chip->irq);
  1392. snd_card_set_pm_callback(card, snd_atiixp_suspend, snd_atiixp_resume, chip);
  1393. if ((err = snd_card_register(card)) < 0)
  1394. goto __error;
  1395. pci_set_drvdata(pci, card);
  1396. dev++;
  1397. return 0;
  1398. __error:
  1399. snd_card_free(card);
  1400. return err;
  1401. }
  1402. static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  1403. {
  1404. snd_card_free(pci_get_drvdata(pci));
  1405. pci_set_drvdata(pci, NULL);
  1406. }
  1407. static struct pci_driver driver = {
  1408. .name = "ATI IXP AC97 controller",
  1409. .owner = THIS_MODULE,
  1410. .id_table = snd_atiixp_ids,
  1411. .probe = snd_atiixp_probe,
  1412. .remove = __devexit_p(snd_atiixp_remove),
  1413. SND_PCI_PM_CALLBACKS
  1414. };
  1415. static int __init alsa_card_atiixp_init(void)
  1416. {
  1417. return pci_register_driver(&driver);
  1418. }
  1419. static void __exit alsa_card_atiixp_exit(void)
  1420. {
  1421. pci_unregister_driver(&driver);
  1422. }
  1423. module_init(alsa_card_atiixp_init)
  1424. module_exit(alsa_card_atiixp_exit)