atiixp.c 46 KB

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