atiixp.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  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_stream_lock(dma->substream);
  592. snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
  593. snd_pcm_stream_unlock(dma->substream);
  594. }
  595. /*
  596. * the period ack. update the substream.
  597. */
  598. static void snd_atiixp_update_dma(struct atiixp *chip, struct atiixp_dma *dma)
  599. {
  600. if (! dma->substream || ! dma->running)
  601. return;
  602. snd_pcm_period_elapsed(dma->substream);
  603. }
  604. /* set BUS_BUSY interrupt bit if any DMA is running */
  605. /* call with spinlock held */
  606. static void snd_atiixp_check_bus_busy(struct atiixp *chip)
  607. {
  608. unsigned int bus_busy;
  609. if (atiixp_read(chip, CMD) & (ATI_REG_CMD_SEND_EN |
  610. ATI_REG_CMD_RECEIVE_EN |
  611. ATI_REG_CMD_SPDF_OUT_EN))
  612. bus_busy = ATI_REG_IER_SET_BUS_BUSY;
  613. else
  614. bus_busy = 0;
  615. atiixp_update(chip, IER, ATI_REG_IER_SET_BUS_BUSY, bus_busy);
  616. }
  617. /* common trigger callback
  618. * calling the lowlevel callbacks in it
  619. */
  620. static int snd_atiixp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  621. {
  622. struct atiixp *chip = snd_pcm_substream_chip(substream);
  623. struct atiixp_dma *dma = substream->runtime->private_data;
  624. int err = 0;
  625. if (snd_BUG_ON(!dma->ops->enable_transfer ||
  626. !dma->ops->flush_dma))
  627. return -EINVAL;
  628. spin_lock(&chip->reg_lock);
  629. switch (cmd) {
  630. case SNDRV_PCM_TRIGGER_START:
  631. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  632. case SNDRV_PCM_TRIGGER_RESUME:
  633. dma->ops->enable_transfer(chip, 1);
  634. dma->running = 1;
  635. dma->suspended = 0;
  636. break;
  637. case SNDRV_PCM_TRIGGER_STOP:
  638. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  639. case SNDRV_PCM_TRIGGER_SUSPEND:
  640. dma->ops->enable_transfer(chip, 0);
  641. dma->running = 0;
  642. dma->suspended = cmd == SNDRV_PCM_TRIGGER_SUSPEND;
  643. break;
  644. default:
  645. err = -EINVAL;
  646. break;
  647. }
  648. if (! err) {
  649. snd_atiixp_check_bus_busy(chip);
  650. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  651. dma->ops->flush_dma(chip);
  652. snd_atiixp_check_bus_busy(chip);
  653. }
  654. }
  655. spin_unlock(&chip->reg_lock);
  656. return err;
  657. }
  658. /*
  659. * lowlevel callbacks for each DMA type
  660. *
  661. * every callback is supposed to be called in chip->reg_lock spinlock
  662. */
  663. /* flush FIFO of analog OUT DMA */
  664. static void atiixp_out_flush_dma(struct atiixp *chip)
  665. {
  666. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_OUT_FLUSH);
  667. }
  668. /* enable/disable analog OUT DMA */
  669. static void atiixp_out_enable_dma(struct atiixp *chip, int on)
  670. {
  671. unsigned int data;
  672. data = atiixp_read(chip, CMD);
  673. if (on) {
  674. if (data & ATI_REG_CMD_OUT_DMA_EN)
  675. return;
  676. atiixp_out_flush_dma(chip);
  677. data |= ATI_REG_CMD_OUT_DMA_EN;
  678. } else
  679. data &= ~ATI_REG_CMD_OUT_DMA_EN;
  680. atiixp_write(chip, CMD, data);
  681. }
  682. /* start/stop transfer over OUT DMA */
  683. static void atiixp_out_enable_transfer(struct atiixp *chip, int on)
  684. {
  685. atiixp_update(chip, CMD, ATI_REG_CMD_SEND_EN,
  686. on ? ATI_REG_CMD_SEND_EN : 0);
  687. }
  688. /* enable/disable analog IN DMA */
  689. static void atiixp_in_enable_dma(struct atiixp *chip, int on)
  690. {
  691. atiixp_update(chip, CMD, ATI_REG_CMD_IN_DMA_EN,
  692. on ? ATI_REG_CMD_IN_DMA_EN : 0);
  693. }
  694. /* start/stop analog IN DMA */
  695. static void atiixp_in_enable_transfer(struct atiixp *chip, int on)
  696. {
  697. if (on) {
  698. unsigned int data = atiixp_read(chip, CMD);
  699. if (! (data & ATI_REG_CMD_RECEIVE_EN)) {
  700. data |= ATI_REG_CMD_RECEIVE_EN;
  701. #if 0 /* FIXME: this causes the endless loop */
  702. /* wait until slot 3/4 are finished */
  703. while ((atiixp_read(chip, COUNTER) &
  704. ATI_REG_COUNTER_SLOT) != 5)
  705. ;
  706. #endif
  707. atiixp_write(chip, CMD, data);
  708. }
  709. } else
  710. atiixp_update(chip, CMD, ATI_REG_CMD_RECEIVE_EN, 0);
  711. }
  712. /* flush FIFO of analog IN DMA */
  713. static void atiixp_in_flush_dma(struct atiixp *chip)
  714. {
  715. atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_IN_FLUSH);
  716. }
  717. /* enable/disable SPDIF OUT DMA */
  718. static void atiixp_spdif_enable_dma(struct atiixp *chip, int on)
  719. {
  720. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_DMA_EN,
  721. on ? ATI_REG_CMD_SPDF_DMA_EN : 0);
  722. }
  723. /* start/stop SPDIF OUT DMA */
  724. static void atiixp_spdif_enable_transfer(struct atiixp *chip, int on)
  725. {
  726. unsigned int data;
  727. data = atiixp_read(chip, CMD);
  728. if (on)
  729. data |= ATI_REG_CMD_SPDF_OUT_EN;
  730. else
  731. data &= ~ATI_REG_CMD_SPDF_OUT_EN;
  732. atiixp_write(chip, CMD, data);
  733. }
  734. /* flush FIFO of SPDIF OUT DMA */
  735. static void atiixp_spdif_flush_dma(struct atiixp *chip)
  736. {
  737. int timeout;
  738. /* DMA off, transfer on */
  739. atiixp_spdif_enable_dma(chip, 0);
  740. atiixp_spdif_enable_transfer(chip, 1);
  741. timeout = 100;
  742. do {
  743. if (! (atiixp_read(chip, SPDF_DMA_DT_SIZE) & ATI_REG_DMA_FIFO_USED))
  744. break;
  745. udelay(1);
  746. } while (timeout-- > 0);
  747. atiixp_spdif_enable_transfer(chip, 0);
  748. }
  749. /* set up slots and formats for SPDIF OUT */
  750. static int snd_atiixp_spdif_prepare(struct snd_pcm_substream *substream)
  751. {
  752. struct atiixp *chip = snd_pcm_substream_chip(substream);
  753. spin_lock_irq(&chip->reg_lock);
  754. if (chip->spdif_over_aclink) {
  755. unsigned int data;
  756. /* enable slots 10/11 */
  757. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK,
  758. ATI_REG_CMD_SPDF_CONFIG_01);
  759. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  760. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  761. ATI_REG_OUT_DMA_SLOT_BIT(11);
  762. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  763. atiixp_write(chip, OUT_DMA_SLOT, data);
  764. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  765. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  766. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  767. } else {
  768. atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
  769. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
  770. }
  771. spin_unlock_irq(&chip->reg_lock);
  772. return 0;
  773. }
  774. /* set up slots and formats for analog OUT */
  775. static int snd_atiixp_playback_prepare(struct snd_pcm_substream *substream)
  776. {
  777. struct atiixp *chip = snd_pcm_substream_chip(substream);
  778. unsigned int data;
  779. spin_lock_irq(&chip->reg_lock);
  780. data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
  781. switch (substream->runtime->channels) {
  782. case 8:
  783. data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
  784. ATI_REG_OUT_DMA_SLOT_BIT(11);
  785. /* fallthru */
  786. case 6:
  787. data |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
  788. ATI_REG_OUT_DMA_SLOT_BIT(8);
  789. /* fallthru */
  790. case 4:
  791. data |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
  792. ATI_REG_OUT_DMA_SLOT_BIT(9);
  793. /* fallthru */
  794. default:
  795. data |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
  796. ATI_REG_OUT_DMA_SLOT_BIT(4);
  797. break;
  798. }
  799. /* set output threshold */
  800. data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
  801. atiixp_write(chip, OUT_DMA_SLOT, data);
  802. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
  803. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  804. ATI_REG_CMD_INTERLEAVE_OUT : 0);
  805. /*
  806. * enable 6 channel re-ordering bit if needed
  807. */
  808. atiixp_update(chip, 6CH_REORDER, ATI_REG_6CH_REORDER_EN,
  809. substream->runtime->channels >= 6 ? ATI_REG_6CH_REORDER_EN: 0);
  810. spin_unlock_irq(&chip->reg_lock);
  811. return 0;
  812. }
  813. /* set up slots and formats for analog IN */
  814. static int snd_atiixp_capture_prepare(struct snd_pcm_substream *substream)
  815. {
  816. struct atiixp *chip = snd_pcm_substream_chip(substream);
  817. spin_lock_irq(&chip->reg_lock);
  818. atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_IN,
  819. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
  820. ATI_REG_CMD_INTERLEAVE_IN : 0);
  821. spin_unlock_irq(&chip->reg_lock);
  822. return 0;
  823. }
  824. /*
  825. * hw_params - allocate the buffer and set up buffer descriptors
  826. */
  827. static int snd_atiixp_pcm_hw_params(struct snd_pcm_substream *substream,
  828. struct snd_pcm_hw_params *hw_params)
  829. {
  830. struct atiixp *chip = snd_pcm_substream_chip(substream);
  831. struct atiixp_dma *dma = substream->runtime->private_data;
  832. int err;
  833. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  834. if (err < 0)
  835. return err;
  836. dma->buf_addr = substream->runtime->dma_addr;
  837. dma->buf_bytes = params_buffer_bytes(hw_params);
  838. err = atiixp_build_dma_packets(chip, dma, substream,
  839. params_periods(hw_params),
  840. params_period_bytes(hw_params));
  841. if (err < 0)
  842. return err;
  843. if (dma->ac97_pcm_type >= 0) {
  844. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  845. /* PCM is bound to AC97 codec(s)
  846. * set up the AC97 codecs
  847. */
  848. if (dma->pcm_open_flag) {
  849. snd_ac97_pcm_close(pcm);
  850. dma->pcm_open_flag = 0;
  851. }
  852. err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
  853. params_channels(hw_params),
  854. pcm->r[0].slots);
  855. if (err >= 0)
  856. dma->pcm_open_flag = 1;
  857. }
  858. return err;
  859. }
  860. static int snd_atiixp_pcm_hw_free(struct snd_pcm_substream *substream)
  861. {
  862. struct atiixp *chip = snd_pcm_substream_chip(substream);
  863. struct atiixp_dma *dma = substream->runtime->private_data;
  864. if (dma->pcm_open_flag) {
  865. struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
  866. snd_ac97_pcm_close(pcm);
  867. dma->pcm_open_flag = 0;
  868. }
  869. atiixp_clear_dma_packets(chip, dma, substream);
  870. snd_pcm_lib_free_pages(substream);
  871. return 0;
  872. }
  873. /*
  874. * pcm hardware definition, identical for all DMA types
  875. */
  876. static struct snd_pcm_hardware snd_atiixp_pcm_hw =
  877. {
  878. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  879. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  880. SNDRV_PCM_INFO_PAUSE |
  881. SNDRV_PCM_INFO_RESUME |
  882. SNDRV_PCM_INFO_MMAP_VALID),
  883. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  884. .rates = SNDRV_PCM_RATE_48000,
  885. .rate_min = 48000,
  886. .rate_max = 48000,
  887. .channels_min = 2,
  888. .channels_max = 2,
  889. .buffer_bytes_max = 256 * 1024,
  890. .period_bytes_min = 32,
  891. .period_bytes_max = 128 * 1024,
  892. .periods_min = 2,
  893. .periods_max = ATI_MAX_DESCRIPTORS,
  894. };
  895. static int snd_atiixp_pcm_open(struct snd_pcm_substream *substream,
  896. struct atiixp_dma *dma, int pcm_type)
  897. {
  898. struct atiixp *chip = snd_pcm_substream_chip(substream);
  899. struct snd_pcm_runtime *runtime = substream->runtime;
  900. int err;
  901. if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
  902. return -EINVAL;
  903. if (dma->opened)
  904. return -EBUSY;
  905. dma->substream = substream;
  906. runtime->hw = snd_atiixp_pcm_hw;
  907. dma->ac97_pcm_type = pcm_type;
  908. if (pcm_type >= 0) {
  909. runtime->hw.rates = chip->pcms[pcm_type]->rates;
  910. snd_pcm_limit_hw_rates(runtime);
  911. } else {
  912. /* direct SPDIF */
  913. runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  914. }
  915. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  916. return err;
  917. runtime->private_data = dma;
  918. /* enable DMA bits */
  919. spin_lock_irq(&chip->reg_lock);
  920. dma->ops->enable_dma(chip, 1);
  921. spin_unlock_irq(&chip->reg_lock);
  922. dma->opened = 1;
  923. return 0;
  924. }
  925. static int snd_atiixp_pcm_close(struct snd_pcm_substream *substream,
  926. struct atiixp_dma *dma)
  927. {
  928. struct atiixp *chip = snd_pcm_substream_chip(substream);
  929. /* disable DMA bits */
  930. if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
  931. return -EINVAL;
  932. spin_lock_irq(&chip->reg_lock);
  933. dma->ops->enable_dma(chip, 0);
  934. spin_unlock_irq(&chip->reg_lock);
  935. dma->substream = NULL;
  936. dma->opened = 0;
  937. return 0;
  938. }
  939. /*
  940. */
  941. static int snd_atiixp_playback_open(struct snd_pcm_substream *substream)
  942. {
  943. struct atiixp *chip = snd_pcm_substream_chip(substream);
  944. int err;
  945. mutex_lock(&chip->open_mutex);
  946. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  947. mutex_unlock(&chip->open_mutex);
  948. if (err < 0)
  949. return err;
  950. substream->runtime->hw.channels_max = chip->max_channels;
  951. if (chip->max_channels > 2)
  952. /* channels must be even */
  953. snd_pcm_hw_constraint_step(substream->runtime, 0,
  954. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  955. return 0;
  956. }
  957. static int snd_atiixp_playback_close(struct snd_pcm_substream *substream)
  958. {
  959. struct atiixp *chip = snd_pcm_substream_chip(substream);
  960. int err;
  961. mutex_lock(&chip->open_mutex);
  962. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  963. mutex_unlock(&chip->open_mutex);
  964. return err;
  965. }
  966. static int snd_atiixp_capture_open(struct snd_pcm_substream *substream)
  967. {
  968. struct atiixp *chip = snd_pcm_substream_chip(substream);
  969. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  970. }
  971. static int snd_atiixp_capture_close(struct snd_pcm_substream *substream)
  972. {
  973. struct atiixp *chip = snd_pcm_substream_chip(substream);
  974. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  975. }
  976. static int snd_atiixp_spdif_open(struct snd_pcm_substream *substream)
  977. {
  978. struct atiixp *chip = snd_pcm_substream_chip(substream);
  979. int err;
  980. mutex_lock(&chip->open_mutex);
  981. if (chip->spdif_over_aclink) /* share DMA_PLAYBACK */
  982. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 2);
  983. else
  984. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
  985. mutex_unlock(&chip->open_mutex);
  986. return err;
  987. }
  988. static int snd_atiixp_spdif_close(struct snd_pcm_substream *substream)
  989. {
  990. struct atiixp *chip = snd_pcm_substream_chip(substream);
  991. int err;
  992. mutex_lock(&chip->open_mutex);
  993. if (chip->spdif_over_aclink)
  994. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  995. else
  996. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
  997. mutex_unlock(&chip->open_mutex);
  998. return err;
  999. }
  1000. /* AC97 playback */
  1001. static struct snd_pcm_ops snd_atiixp_playback_ops = {
  1002. .open = snd_atiixp_playback_open,
  1003. .close = snd_atiixp_playback_close,
  1004. .ioctl = snd_pcm_lib_ioctl,
  1005. .hw_params = snd_atiixp_pcm_hw_params,
  1006. .hw_free = snd_atiixp_pcm_hw_free,
  1007. .prepare = snd_atiixp_playback_prepare,
  1008. .trigger = snd_atiixp_pcm_trigger,
  1009. .pointer = snd_atiixp_pcm_pointer,
  1010. };
  1011. /* AC97 capture */
  1012. static struct snd_pcm_ops snd_atiixp_capture_ops = {
  1013. .open = snd_atiixp_capture_open,
  1014. .close = snd_atiixp_capture_close,
  1015. .ioctl = snd_pcm_lib_ioctl,
  1016. .hw_params = snd_atiixp_pcm_hw_params,
  1017. .hw_free = snd_atiixp_pcm_hw_free,
  1018. .prepare = snd_atiixp_capture_prepare,
  1019. .trigger = snd_atiixp_pcm_trigger,
  1020. .pointer = snd_atiixp_pcm_pointer,
  1021. };
  1022. /* SPDIF playback */
  1023. static struct snd_pcm_ops snd_atiixp_spdif_ops = {
  1024. .open = snd_atiixp_spdif_open,
  1025. .close = snd_atiixp_spdif_close,
  1026. .ioctl = snd_pcm_lib_ioctl,
  1027. .hw_params = snd_atiixp_pcm_hw_params,
  1028. .hw_free = snd_atiixp_pcm_hw_free,
  1029. .prepare = snd_atiixp_spdif_prepare,
  1030. .trigger = snd_atiixp_pcm_trigger,
  1031. .pointer = snd_atiixp_pcm_pointer,
  1032. };
  1033. static struct ac97_pcm atiixp_pcm_defs[] = {
  1034. /* front PCM */
  1035. {
  1036. .exclusive = 1,
  1037. .r = { {
  1038. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  1039. (1 << AC97_SLOT_PCM_RIGHT) |
  1040. (1 << AC97_SLOT_PCM_CENTER) |
  1041. (1 << AC97_SLOT_PCM_SLEFT) |
  1042. (1 << AC97_SLOT_PCM_SRIGHT) |
  1043. (1 << AC97_SLOT_LFE)
  1044. }
  1045. }
  1046. },
  1047. /* PCM IN #1 */
  1048. {
  1049. .stream = 1,
  1050. .exclusive = 1,
  1051. .r = { {
  1052. .slots = (1 << AC97_SLOT_PCM_LEFT) |
  1053. (1 << AC97_SLOT_PCM_RIGHT)
  1054. }
  1055. }
  1056. },
  1057. /* S/PDIF OUT (optional) */
  1058. {
  1059. .exclusive = 1,
  1060. .spdif = 1,
  1061. .r = { {
  1062. .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
  1063. (1 << AC97_SLOT_SPDIF_RIGHT2)
  1064. }
  1065. }
  1066. },
  1067. };
  1068. static struct atiixp_dma_ops snd_atiixp_playback_dma_ops = {
  1069. .type = ATI_DMA_PLAYBACK,
  1070. .llp_offset = ATI_REG_OUT_DMA_LINKPTR,
  1071. .dt_cur = ATI_REG_OUT_DMA_DT_CUR,
  1072. .enable_dma = atiixp_out_enable_dma,
  1073. .enable_transfer = atiixp_out_enable_transfer,
  1074. .flush_dma = atiixp_out_flush_dma,
  1075. };
  1076. static struct atiixp_dma_ops snd_atiixp_capture_dma_ops = {
  1077. .type = ATI_DMA_CAPTURE,
  1078. .llp_offset = ATI_REG_IN_DMA_LINKPTR,
  1079. .dt_cur = ATI_REG_IN_DMA_DT_CUR,
  1080. .enable_dma = atiixp_in_enable_dma,
  1081. .enable_transfer = atiixp_in_enable_transfer,
  1082. .flush_dma = atiixp_in_flush_dma,
  1083. };
  1084. static struct atiixp_dma_ops snd_atiixp_spdif_dma_ops = {
  1085. .type = ATI_DMA_SPDIF,
  1086. .llp_offset = ATI_REG_SPDF_DMA_LINKPTR,
  1087. .dt_cur = ATI_REG_SPDF_DMA_DT_CUR,
  1088. .enable_dma = atiixp_spdif_enable_dma,
  1089. .enable_transfer = atiixp_spdif_enable_transfer,
  1090. .flush_dma = atiixp_spdif_flush_dma,
  1091. };
  1092. static int snd_atiixp_pcm_new(struct atiixp *chip)
  1093. {
  1094. struct snd_pcm *pcm;
  1095. struct snd_pcm_chmap *chmap;
  1096. struct snd_ac97_bus *pbus = chip->ac97_bus;
  1097. int err, i, num_pcms;
  1098. /* initialize constants */
  1099. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  1100. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  1101. if (! chip->spdif_over_aclink)
  1102. chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
  1103. /* assign AC97 pcm */
  1104. if (chip->spdif_over_aclink)
  1105. num_pcms = 3;
  1106. else
  1107. num_pcms = 2;
  1108. err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
  1109. if (err < 0)
  1110. return err;
  1111. for (i = 0; i < num_pcms; i++)
  1112. chip->pcms[i] = &pbus->pcms[i];
  1113. chip->max_channels = 2;
  1114. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
  1115. if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_LFE))
  1116. chip->max_channels = 6;
  1117. else
  1118. chip->max_channels = 4;
  1119. }
  1120. /* PCM #0: analog I/O */
  1121. err = snd_pcm_new(chip->card, "ATI IXP AC97",
  1122. ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  1123. if (err < 0)
  1124. return err;
  1125. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  1126. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  1127. pcm->private_data = chip;
  1128. strcpy(pcm->name, "ATI IXP AC97");
  1129. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  1130. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1131. snd_dma_pci_data(chip->pci),
  1132. 64*1024, 128*1024);
  1133. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1134. snd_pcm_alt_chmaps, chip->max_channels, 0,
  1135. &chmap);
  1136. if (err < 0)
  1137. return err;
  1138. chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
  1139. chip->ac97[0]->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
  1140. /* no SPDIF support on codec? */
  1141. if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
  1142. return 0;
  1143. /* FIXME: non-48k sample rate doesn't work on my test machine with AD1888 */
  1144. if (chip->pcms[ATI_PCM_SPDIF])
  1145. chip->pcms[ATI_PCM_SPDIF]->rates = SNDRV_PCM_RATE_48000;
  1146. /* PCM #1: spdif playback */
  1147. err = snd_pcm_new(chip->card, "ATI IXP IEC958",
  1148. ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
  1149. if (err < 0)
  1150. return err;
  1151. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_spdif_ops);
  1152. pcm->private_data = chip;
  1153. if (chip->spdif_over_aclink)
  1154. strcpy(pcm->name, "ATI IXP IEC958 (AC97)");
  1155. else
  1156. strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
  1157. chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
  1158. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1159. snd_dma_pci_data(chip->pci),
  1160. 64*1024, 128*1024);
  1161. /* pre-select AC97 SPDIF slots 10/11 */
  1162. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1163. if (chip->ac97[i])
  1164. snd_ac97_update_bits(chip->ac97[i],
  1165. AC97_EXTENDED_STATUS,
  1166. 0x03 << 4, 0x03 << 4);
  1167. }
  1168. return 0;
  1169. }
  1170. /*
  1171. * interrupt handler
  1172. */
  1173. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id)
  1174. {
  1175. struct atiixp *chip = dev_id;
  1176. unsigned int status;
  1177. status = atiixp_read(chip, ISR);
  1178. if (! status)
  1179. return IRQ_NONE;
  1180. /* process audio DMA */
  1181. if (status & ATI_REG_ISR_OUT_XRUN)
  1182. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1183. else if (status & ATI_REG_ISR_OUT_STATUS)
  1184. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  1185. if (status & ATI_REG_ISR_IN_XRUN)
  1186. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1187. else if (status & ATI_REG_ISR_IN_STATUS)
  1188. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  1189. if (! chip->spdif_over_aclink) {
  1190. if (status & ATI_REG_ISR_SPDF_XRUN)
  1191. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1192. else if (status & ATI_REG_ISR_SPDF_STATUS)
  1193. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
  1194. }
  1195. /* for codec detection */
  1196. if (status & CODEC_CHECK_BITS) {
  1197. unsigned int detected;
  1198. detected = status & CODEC_CHECK_BITS;
  1199. spin_lock(&chip->reg_lock);
  1200. chip->codec_not_ready_bits |= detected;
  1201. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  1202. spin_unlock(&chip->reg_lock);
  1203. }
  1204. /* ack */
  1205. atiixp_write(chip, ISR, status);
  1206. return IRQ_HANDLED;
  1207. }
  1208. /*
  1209. * ac97 mixer section
  1210. */
  1211. static struct ac97_quirk ac97_quirks[] = {
  1212. {
  1213. .subvendor = 0x103c,
  1214. .subdevice = 0x006b,
  1215. .name = "HP Pavilion ZV5030US",
  1216. .type = AC97_TUNE_MUTE_LED
  1217. },
  1218. {
  1219. .subvendor = 0x103c,
  1220. .subdevice = 0x308b,
  1221. .name = "HP nx6125",
  1222. .type = AC97_TUNE_MUTE_LED
  1223. },
  1224. {
  1225. .subvendor = 0x103c,
  1226. .subdevice = 0x3091,
  1227. .name = "unknown HP",
  1228. .type = AC97_TUNE_MUTE_LED
  1229. },
  1230. { } /* terminator */
  1231. };
  1232. static int snd_atiixp_mixer_new(struct atiixp *chip, int clock,
  1233. const char *quirk_override)
  1234. {
  1235. struct snd_ac97_bus *pbus;
  1236. struct snd_ac97_template ac97;
  1237. int i, err;
  1238. int codec_count;
  1239. static struct snd_ac97_bus_ops ops = {
  1240. .write = snd_atiixp_ac97_write,
  1241. .read = snd_atiixp_ac97_read,
  1242. };
  1243. static unsigned int codec_skip[NUM_ATI_CODECS] = {
  1244. ATI_REG_ISR_CODEC0_NOT_READY,
  1245. ATI_REG_ISR_CODEC1_NOT_READY,
  1246. ATI_REG_ISR_CODEC2_NOT_READY,
  1247. };
  1248. if (snd_atiixp_codec_detect(chip) < 0)
  1249. return -ENXIO;
  1250. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  1251. return err;
  1252. pbus->clock = clock;
  1253. chip->ac97_bus = pbus;
  1254. codec_count = 0;
  1255. for (i = 0; i < NUM_ATI_CODECS; i++) {
  1256. if (chip->codec_not_ready_bits & codec_skip[i])
  1257. continue;
  1258. memset(&ac97, 0, sizeof(ac97));
  1259. ac97.private_data = chip;
  1260. ac97.pci = chip->pci;
  1261. ac97.num = i;
  1262. ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
  1263. if (! chip->spdif_over_aclink)
  1264. ac97.scaps |= AC97_SCAP_NO_SPDIF;
  1265. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  1266. chip->ac97[i] = NULL; /* to be sure */
  1267. snd_printdd("atiixp: codec %d not available for audio\n", i);
  1268. continue;
  1269. }
  1270. codec_count++;
  1271. }
  1272. if (! codec_count) {
  1273. snd_printk(KERN_ERR "atiixp: no codec available\n");
  1274. return -ENODEV;
  1275. }
  1276. snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
  1277. return 0;
  1278. }
  1279. #ifdef CONFIG_PM_SLEEP
  1280. /*
  1281. * power management
  1282. */
  1283. static int snd_atiixp_suspend(struct device *dev)
  1284. {
  1285. struct pci_dev *pci = to_pci_dev(dev);
  1286. struct snd_card *card = dev_get_drvdata(dev);
  1287. struct atiixp *chip = card->private_data;
  1288. int i;
  1289. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1290. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1291. if (chip->pcmdevs[i]) {
  1292. struct atiixp_dma *dma = &chip->dmas[i];
  1293. if (dma->substream && dma->running)
  1294. dma->saved_curptr = readl(chip->remap_addr +
  1295. dma->ops->dt_cur);
  1296. snd_pcm_suspend_all(chip->pcmdevs[i]);
  1297. }
  1298. for (i = 0; i < NUM_ATI_CODECS; i++)
  1299. snd_ac97_suspend(chip->ac97[i]);
  1300. snd_atiixp_aclink_down(chip);
  1301. snd_atiixp_chip_stop(chip);
  1302. pci_disable_device(pci);
  1303. pci_save_state(pci);
  1304. pci_set_power_state(pci, PCI_D3hot);
  1305. return 0;
  1306. }
  1307. static int snd_atiixp_resume(struct device *dev)
  1308. {
  1309. struct pci_dev *pci = to_pci_dev(dev);
  1310. struct snd_card *card = dev_get_drvdata(dev);
  1311. struct atiixp *chip = card->private_data;
  1312. int i;
  1313. pci_set_power_state(pci, PCI_D0);
  1314. pci_restore_state(pci);
  1315. if (pci_enable_device(pci) < 0) {
  1316. printk(KERN_ERR "atiixp: pci_enable_device failed, "
  1317. "disabling device\n");
  1318. snd_card_disconnect(card);
  1319. return -EIO;
  1320. }
  1321. pci_set_master(pci);
  1322. snd_atiixp_aclink_reset(chip);
  1323. snd_atiixp_chip_start(chip);
  1324. for (i = 0; i < NUM_ATI_CODECS; i++)
  1325. snd_ac97_resume(chip->ac97[i]);
  1326. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  1327. if (chip->pcmdevs[i]) {
  1328. struct atiixp_dma *dma = &chip->dmas[i];
  1329. if (dma->substream && dma->suspended) {
  1330. dma->ops->enable_dma(chip, 1);
  1331. dma->substream->ops->prepare(dma->substream);
  1332. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  1333. chip->remap_addr + dma->ops->llp_offset);
  1334. writel(dma->saved_curptr, chip->remap_addr +
  1335. dma->ops->dt_cur);
  1336. }
  1337. }
  1338. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1339. return 0;
  1340. }
  1341. static SIMPLE_DEV_PM_OPS(snd_atiixp_pm, snd_atiixp_suspend, snd_atiixp_resume);
  1342. #define SND_ATIIXP_PM_OPS &snd_atiixp_pm
  1343. #else
  1344. #define SND_ATIIXP_PM_OPS NULL
  1345. #endif /* CONFIG_PM_SLEEP */
  1346. #ifdef CONFIG_PROC_FS
  1347. /*
  1348. * proc interface for register dump
  1349. */
  1350. static void snd_atiixp_proc_read(struct snd_info_entry *entry,
  1351. struct snd_info_buffer *buffer)
  1352. {
  1353. struct atiixp *chip = entry->private_data;
  1354. int i;
  1355. for (i = 0; i < 256; i += 4)
  1356. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  1357. }
  1358. static void snd_atiixp_proc_init(struct atiixp *chip)
  1359. {
  1360. struct snd_info_entry *entry;
  1361. if (! snd_card_proc_new(chip->card, "atiixp", &entry))
  1362. snd_info_set_text_ops(entry, chip, snd_atiixp_proc_read);
  1363. }
  1364. #else /* !CONFIG_PROC_FS */
  1365. #define snd_atiixp_proc_init(chip)
  1366. #endif
  1367. /*
  1368. * destructor
  1369. */
  1370. static int snd_atiixp_free(struct atiixp *chip)
  1371. {
  1372. if (chip->irq < 0)
  1373. goto __hw_end;
  1374. snd_atiixp_chip_stop(chip);
  1375. __hw_end:
  1376. if (chip->irq >= 0)
  1377. free_irq(chip->irq, chip);
  1378. if (chip->remap_addr)
  1379. iounmap(chip->remap_addr);
  1380. pci_release_regions(chip->pci);
  1381. pci_disable_device(chip->pci);
  1382. kfree(chip);
  1383. return 0;
  1384. }
  1385. static int snd_atiixp_dev_free(struct snd_device *device)
  1386. {
  1387. struct atiixp *chip = device->device_data;
  1388. return snd_atiixp_free(chip);
  1389. }
  1390. /*
  1391. * constructor for chip instance
  1392. */
  1393. static int snd_atiixp_create(struct snd_card *card,
  1394. struct pci_dev *pci,
  1395. struct atiixp **r_chip)
  1396. {
  1397. static struct snd_device_ops ops = {
  1398. .dev_free = snd_atiixp_dev_free,
  1399. };
  1400. struct atiixp *chip;
  1401. int err;
  1402. if ((err = pci_enable_device(pci)) < 0)
  1403. return err;
  1404. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1405. if (chip == NULL) {
  1406. pci_disable_device(pci);
  1407. return -ENOMEM;
  1408. }
  1409. spin_lock_init(&chip->reg_lock);
  1410. mutex_init(&chip->open_mutex);
  1411. chip->card = card;
  1412. chip->pci = pci;
  1413. chip->irq = -1;
  1414. if ((err = pci_request_regions(pci, "ATI IXP AC97")) < 0) {
  1415. pci_disable_device(pci);
  1416. kfree(chip);
  1417. return err;
  1418. }
  1419. chip->addr = pci_resource_start(pci, 0);
  1420. chip->remap_addr = pci_ioremap_bar(pci, 0);
  1421. if (chip->remap_addr == NULL) {
  1422. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1423. snd_atiixp_free(chip);
  1424. return -EIO;
  1425. }
  1426. if (request_irq(pci->irq, snd_atiixp_interrupt, IRQF_SHARED,
  1427. KBUILD_MODNAME, chip)) {
  1428. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1429. snd_atiixp_free(chip);
  1430. return -EBUSY;
  1431. }
  1432. chip->irq = pci->irq;
  1433. pci_set_master(pci);
  1434. synchronize_irq(chip->irq);
  1435. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1436. snd_atiixp_free(chip);
  1437. return err;
  1438. }
  1439. snd_card_set_dev(card, &pci->dev);
  1440. *r_chip = chip;
  1441. return 0;
  1442. }
  1443. static int snd_atiixp_probe(struct pci_dev *pci,
  1444. const struct pci_device_id *pci_id)
  1445. {
  1446. struct snd_card *card;
  1447. struct atiixp *chip;
  1448. int err;
  1449. err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  1450. if (err < 0)
  1451. return err;
  1452. strcpy(card->driver, spdif_aclink ? "ATIIXP" : "ATIIXP-SPDMA");
  1453. strcpy(card->shortname, "ATI IXP");
  1454. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1455. goto __error;
  1456. card->private_data = chip;
  1457. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1458. goto __error;
  1459. chip->spdif_over_aclink = spdif_aclink;
  1460. if ((err = snd_atiixp_mixer_new(chip, ac97_clock, ac97_quirk)) < 0)
  1461. goto __error;
  1462. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1463. goto __error;
  1464. snd_atiixp_proc_init(chip);
  1465. snd_atiixp_chip_start(chip);
  1466. snprintf(card->longname, sizeof(card->longname),
  1467. "%s rev %x with %s at %#lx, irq %i", card->shortname,
  1468. pci->revision,
  1469. chip->ac97[0] ? snd_ac97_get_short_name(chip->ac97[0]) : "?",
  1470. chip->addr, chip->irq);
  1471. if ((err = snd_card_register(card)) < 0)
  1472. goto __error;
  1473. pci_set_drvdata(pci, card);
  1474. return 0;
  1475. __error:
  1476. snd_card_free(card);
  1477. return err;
  1478. }
  1479. static void snd_atiixp_remove(struct pci_dev *pci)
  1480. {
  1481. snd_card_free(pci_get_drvdata(pci));
  1482. }
  1483. static struct pci_driver atiixp_driver = {
  1484. .name = KBUILD_MODNAME,
  1485. .id_table = snd_atiixp_ids,
  1486. .probe = snd_atiixp_probe,
  1487. .remove = snd_atiixp_remove,
  1488. .driver = {
  1489. .pm = SND_ATIIXP_PM_OPS,
  1490. },
  1491. };
  1492. module_pci_driver(atiixp_driver);