atiixp_modem.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * ALSA driver for ATI IXP 150/200/250 AC97 modem controllers
  3. *
  4. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include <linux/moduleparam.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/info.h>
  33. #include <sound/ac97_codec.h>
  34. #include <sound/initval.h>
  35. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  36. MODULE_DESCRIPTION("ATI IXP MC97 controller");
  37. MODULE_LICENSE("GPL");
  38. MODULE_SUPPORTED_DEVICE("{{ATI,IXP150/200/250}}");
  39. static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* Exclude the first card */
  40. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  41. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  42. static int ac97_clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 48000};
  43. module_param_array(index, int, NULL, 0444);
  44. MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
  45. module_param_array(id, charp, NULL, 0444);
  46. MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
  47. module_param_array(enable, bool, NULL, 0444);
  48. MODULE_PARM_DESC(enable, "Enable audio part of ATI IXP controller.");
  49. module_param_array(ac97_clock, int, NULL, 0444);
  50. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  51. /*
  52. */
  53. #define ATI_REG_ISR 0x00 /* interrupt source */
  54. #define ATI_REG_ISR_MODEM_IN_XRUN (1U<<0)
  55. #define ATI_REG_ISR_MODEM_IN_STATUS (1U<<1)
  56. #define ATI_REG_ISR_MODEM_OUT1_XRUN (1U<<2)
  57. #define ATI_REG_ISR_MODEM_OUT1_STATUS (1U<<3)
  58. #define ATI_REG_ISR_MODEM_OUT2_XRUN (1U<<4)
  59. #define ATI_REG_ISR_MODEM_OUT2_STATUS (1U<<5)
  60. #define ATI_REG_ISR_MODEM_OUT3_XRUN (1U<<6)
  61. #define ATI_REG_ISR_MODEM_OUT3_STATUS (1U<<7)
  62. #define ATI_REG_ISR_PHYS_INTR (1U<<8)
  63. #define ATI_REG_ISR_PHYS_MISMATCH (1U<<9)
  64. #define ATI_REG_ISR_CODEC0_NOT_READY (1U<<10)
  65. #define ATI_REG_ISR_CODEC1_NOT_READY (1U<<11)
  66. #define ATI_REG_ISR_CODEC2_NOT_READY (1U<<12)
  67. #define ATI_REG_ISR_NEW_FRAME (1U<<13)
  68. #define ATI_REG_ISR_MODEM_GPIO_DATA (1U<<14)
  69. #define ATI_REG_IER 0x04 /* interrupt enable */
  70. #define ATI_REG_IER_MODEM_IN_XRUN_EN (1U<<0)
  71. #define ATI_REG_IER_MODEM_STATUS_EN (1U<<1)
  72. #define ATI_REG_IER_MODEM_OUT1_XRUN_EN (1U<<2)
  73. #define ATI_REG_IER_MODEM_OUT2_XRUN_EN (1U<<4)
  74. #define ATI_REG_IER_MODEM_OUT3_XRUN_EN (1U<<6)
  75. #define ATI_REG_IER_PHYS_INTR_EN (1U<<8)
  76. #define ATI_REG_IER_PHYS_MISMATCH_EN (1U<<9)
  77. #define ATI_REG_IER_CODEC0_INTR_EN (1U<<10)
  78. #define ATI_REG_IER_CODEC1_INTR_EN (1U<<11)
  79. #define ATI_REG_IER_CODEC2_INTR_EN (1U<<12)
  80. #define ATI_REG_IER_NEW_FRAME_EN (1U<<13) /* (RO */
  81. #define ATI_REG_IER_MODEM_GPIO_DATA_EN (1U<<14) /* (WO) modem is running */
  82. #define ATI_REG_IER_MODEM_SET_BUS_BUSY (1U<<15)
  83. #define ATI_REG_CMD 0x08 /* command */
  84. #define ATI_REG_CMD_POWERDOWN (1U<<0)
  85. #define ATI_REG_CMD_MODEM_RECEIVE_EN (1U<<1) /* modem only */
  86. #define ATI_REG_CMD_MODEM_SEND1_EN (1U<<2) /* modem only */
  87. #define ATI_REG_CMD_MODEM_SEND2_EN (1U<<3) /* modem only */
  88. #define ATI_REG_CMD_MODEM_SEND3_EN (1U<<4) /* modem only */
  89. #define ATI_REG_CMD_MODEM_STATUS_MEM (1U<<5) /* modem only */
  90. #define ATI_REG_CMD_MODEM_IN_DMA_EN (1U<<8) /* modem only */
  91. #define ATI_REG_CMD_MODEM_OUT_DMA1_EN (1U<<9) /* modem only */
  92. #define ATI_REG_CMD_MODEM_OUT_DMA2_EN (1U<<10) /* modem only */
  93. #define ATI_REG_CMD_MODEM_OUT_DMA3_EN (1U<<11) /* modem only */
  94. #define ATI_REG_CMD_AUDIO_PRESENT (1U<<20)
  95. #define ATI_REG_CMD_MODEM_GPIO_THRU_DMA (1U<<22) /* modem only */
  96. #define ATI_REG_CMD_LOOPBACK_EN (1U<<23)
  97. #define ATI_REG_CMD_PACKED_DIS (1U<<24)
  98. #define ATI_REG_CMD_BURST_EN (1U<<25)
  99. #define ATI_REG_CMD_PANIC_EN (1U<<26)
  100. #define ATI_REG_CMD_MODEM_PRESENT (1U<<27)
  101. #define ATI_REG_CMD_ACLINK_ACTIVE (1U<<28)
  102. #define ATI_REG_CMD_AC_SOFT_RESET (1U<<29)
  103. #define ATI_REG_CMD_AC_SYNC (1U<<30)
  104. #define ATI_REG_CMD_AC_RESET (1U<<31)
  105. #define ATI_REG_PHYS_OUT_ADDR 0x0c
  106. #define ATI_REG_PHYS_OUT_CODEC_MASK (3U<<0)
  107. #define ATI_REG_PHYS_OUT_RW (1U<<2)
  108. #define ATI_REG_PHYS_OUT_ADDR_EN (1U<<8)
  109. #define ATI_REG_PHYS_OUT_ADDR_SHIFT 9
  110. #define ATI_REG_PHYS_OUT_DATA_SHIFT 16
  111. #define ATI_REG_PHYS_IN_ADDR 0x10
  112. #define ATI_REG_PHYS_IN_READ_FLAG (1U<<8)
  113. #define ATI_REG_PHYS_IN_ADDR_SHIFT 9
  114. #define ATI_REG_PHYS_IN_DATA_SHIFT 16
  115. #define ATI_REG_SLOTREQ 0x14
  116. #define ATI_REG_COUNTER 0x18
  117. #define ATI_REG_COUNTER_SLOT (3U<<0) /* slot # */
  118. #define ATI_REG_COUNTER_BITCLOCK (31U<<8)
  119. #define ATI_REG_IN_FIFO_THRESHOLD 0x1c
  120. #define ATI_REG_MODEM_IN_DMA_LINKPTR 0x20
  121. #define ATI_REG_MODEM_IN_DMA_DT_START 0x24 /* RO */
  122. #define ATI_REG_MODEM_IN_DMA_DT_NEXT 0x28 /* RO */
  123. #define ATI_REG_MODEM_IN_DMA_DT_CUR 0x2c /* RO */
  124. #define ATI_REG_MODEM_IN_DMA_DT_SIZE 0x30
  125. #define ATI_REG_MODEM_OUT_FIFO 0x34 /* output threshold */
  126. #define ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK (0xf<<16)
  127. #define ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT 16
  128. #define ATI_REG_MODEM_OUT_DMA1_LINKPTR 0x38
  129. #define ATI_REG_MODEM_OUT_DMA2_LINKPTR 0x3c
  130. #define ATI_REG_MODEM_OUT_DMA3_LINKPTR 0x40
  131. #define ATI_REG_MODEM_OUT_DMA1_DT_START 0x44
  132. #define ATI_REG_MODEM_OUT_DMA1_DT_NEXT 0x48
  133. #define ATI_REG_MODEM_OUT_DMA1_DT_CUR 0x4c
  134. #define ATI_REG_MODEM_OUT_DMA2_DT_START 0x50
  135. #define ATI_REG_MODEM_OUT_DMA2_DT_NEXT 0x54
  136. #define ATI_REG_MODEM_OUT_DMA2_DT_CUR 0x58
  137. #define ATI_REG_MODEM_OUT_DMA3_DT_START 0x5c
  138. #define ATI_REG_MODEM_OUT_DMA3_DT_NEXT 0x60
  139. #define ATI_REG_MODEM_OUT_DMA3_DT_CUR 0x64
  140. #define ATI_REG_MODEM_OUT_DMA12_DT_SIZE 0x68
  141. #define ATI_REG_MODEM_OUT_DMA3_DT_SIZE 0x6c
  142. #define ATI_REG_MODEM_OUT_FIFO_USED 0x70
  143. #define ATI_REG_MODEM_OUT_GPIO 0x74
  144. #define ATI_REG_MODEM_OUT_GPIO_EN 1
  145. #define ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT 5
  146. #define ATI_REG_MODEM_IN_GPIO 0x78
  147. #define ATI_REG_MODEM_MIRROR 0x7c
  148. #define ATI_REG_AUDIO_MIRROR 0x80
  149. #define ATI_REG_MODEM_FIFO_FLUSH 0x88
  150. #define ATI_REG_MODEM_FIFO_OUT1_FLUSH (1U<<0)
  151. #define ATI_REG_MODEM_FIFO_OUT2_FLUSH (1U<<1)
  152. #define ATI_REG_MODEM_FIFO_OUT3_FLUSH (1U<<2)
  153. #define ATI_REG_MODEM_FIFO_IN_FLUSH (1U<<3)
  154. /* LINKPTR */
  155. #define ATI_REG_LINKPTR_EN (1U<<0)
  156. #define ATI_MAX_DESCRIPTORS 256 /* max number of descriptor packets */
  157. /*
  158. */
  159. typedef struct snd_atiixp atiixp_t;
  160. typedef struct snd_atiixp_dma atiixp_dma_t;
  161. typedef struct snd_atiixp_dma_ops atiixp_dma_ops_t;
  162. /*
  163. * DMA packate descriptor
  164. */
  165. typedef struct atiixp_dma_desc {
  166. u32 addr; /* DMA buffer address */
  167. u16 status; /* status bits */
  168. u16 size; /* size of the packet in dwords */
  169. u32 next; /* address of the next packet descriptor */
  170. } atiixp_dma_desc_t;
  171. /*
  172. * stream enum
  173. */
  174. enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, NUM_ATI_DMAS }; /* DMAs */
  175. enum { ATI_PCM_OUT, ATI_PCM_IN, NUM_ATI_PCMS }; /* AC97 pcm slots */
  176. enum { ATI_PCMDEV_ANALOG, NUM_ATI_PCMDEVS }; /* pcm devices */
  177. #define NUM_ATI_CODECS 3
  178. /*
  179. * constants and callbacks for each DMA type
  180. */
  181. struct snd_atiixp_dma_ops {
  182. int type; /* ATI_DMA_XXX */
  183. unsigned int llp_offset; /* LINKPTR offset */
  184. unsigned int dt_cur; /* DT_CUR offset */
  185. void (*enable_dma)(atiixp_t *chip, int on); /* called from open callback */
  186. void (*enable_transfer)(atiixp_t *chip, int on); /* called from trigger (START/STOP) */
  187. void (*flush_dma)(atiixp_t *chip); /* called from trigger (STOP only) */
  188. };
  189. /*
  190. * DMA stream
  191. */
  192. struct snd_atiixp_dma {
  193. const atiixp_dma_ops_t *ops;
  194. struct snd_dma_buffer desc_buf;
  195. snd_pcm_substream_t *substream; /* assigned PCM substream */
  196. unsigned int buf_addr, buf_bytes; /* DMA buffer address, bytes */
  197. unsigned int period_bytes, periods;
  198. int opened;
  199. int running;
  200. int pcm_open_flag;
  201. int ac97_pcm_type; /* index # of ac97_pcm to access, -1 = not used */
  202. };
  203. /*
  204. * ATI IXP chip
  205. */
  206. struct snd_atiixp {
  207. snd_card_t *card;
  208. struct pci_dev *pci;
  209. struct resource *res; /* memory i/o */
  210. unsigned long addr;
  211. void __iomem *remap_addr;
  212. int irq;
  213. ac97_bus_t *ac97_bus;
  214. ac97_t *ac97[NUM_ATI_CODECS];
  215. spinlock_t reg_lock;
  216. atiixp_dma_t dmas[NUM_ATI_DMAS];
  217. struct ac97_pcm *pcms[NUM_ATI_PCMS];
  218. snd_pcm_t *pcmdevs[NUM_ATI_PCMDEVS];
  219. int max_channels; /* max. channels for PCM out */
  220. unsigned int codec_not_ready_bits; /* for codec detection */
  221. int spdif_over_aclink; /* passed from the module option */
  222. struct semaphore open_mutex; /* playback open mutex */
  223. };
  224. /*
  225. */
  226. static struct pci_device_id snd_atiixp_ids[] = {
  227. { 0x1002, 0x434d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB200 */
  228. { 0, }
  229. };
  230. MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
  231. /*
  232. * lowlevel functions
  233. */
  234. /*
  235. * update the bits of the given register.
  236. * return 1 if the bits changed.
  237. */
  238. static int snd_atiixp_update_bits(atiixp_t *chip, unsigned int reg,
  239. unsigned int mask, unsigned int value)
  240. {
  241. void __iomem *addr = chip->remap_addr + reg;
  242. unsigned int data, old_data;
  243. old_data = data = readl(addr);
  244. data &= ~mask;
  245. data |= value;
  246. if (old_data == data)
  247. return 0;
  248. writel(data, addr);
  249. return 1;
  250. }
  251. /*
  252. * macros for easy use
  253. */
  254. #define atiixp_write(chip,reg,value) \
  255. writel(value, chip->remap_addr + ATI_REG_##reg)
  256. #define atiixp_read(chip,reg) \
  257. readl(chip->remap_addr + ATI_REG_##reg)
  258. #define atiixp_update(chip,reg,mask,val) \
  259. snd_atiixp_update_bits(chip, ATI_REG_##reg, mask, val)
  260. /* delay for one tick */
  261. #define do_delay() do { \
  262. set_current_state(TASK_UNINTERRUPTIBLE); \
  263. schedule_timeout(1); \
  264. } while (0)
  265. /*
  266. * handling DMA packets
  267. *
  268. * we allocate a linear buffer for the DMA, and split it to each packet.
  269. * in a future version, a scatter-gather buffer should be implemented.
  270. */
  271. #define ATI_DESC_LIST_SIZE \
  272. PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(atiixp_dma_desc_t))
  273. /*
  274. * build packets ring for the given buffer size.
  275. *
  276. * IXP handles the buffer descriptors, which are connected as a linked
  277. * list. although we can change the list dynamically, in this version,
  278. * a static RING of buffer descriptors is used.
  279. *
  280. * the ring is built in this function, and is set up to the hardware.
  281. */
  282. static int atiixp_build_dma_packets(atiixp_t *chip, atiixp_dma_t *dma,
  283. snd_pcm_substream_t *substream,
  284. unsigned int periods,
  285. unsigned int period_bytes)
  286. {
  287. unsigned int i;
  288. u32 addr, desc_addr;
  289. unsigned long flags;
  290. if (periods > ATI_MAX_DESCRIPTORS)
  291. return -ENOMEM;
  292. if (dma->desc_buf.area == NULL) {
  293. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  294. ATI_DESC_LIST_SIZE, &dma->desc_buf) < 0)
  295. return -ENOMEM;
  296. dma->period_bytes = dma->periods = 0; /* clear */
  297. }
  298. if (dma->periods == periods && dma->period_bytes == period_bytes)
  299. return 0;
  300. /* reset DMA before changing the descriptor table */
  301. spin_lock_irqsave(&chip->reg_lock, flags);
  302. writel(0, chip->remap_addr + dma->ops->llp_offset);
  303. dma->ops->enable_dma(chip, 0);
  304. dma->ops->enable_dma(chip, 1);
  305. spin_unlock_irqrestore(&chip->reg_lock, flags);
  306. /* fill the entries */
  307. addr = (u32)substream->runtime->dma_addr;
  308. desc_addr = (u32)dma->desc_buf.addr;
  309. for (i = 0; i < periods; i++) {
  310. atiixp_dma_desc_t *desc = &((atiixp_dma_desc_t *)dma->desc_buf.area)[i];
  311. desc->addr = cpu_to_le32(addr);
  312. desc->status = 0;
  313. desc->size = period_bytes >> 2; /* in dwords */
  314. desc_addr += sizeof(atiixp_dma_desc_t);
  315. if (i == periods - 1)
  316. desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
  317. else
  318. desc->next = cpu_to_le32(desc_addr);
  319. addr += period_bytes;
  320. }
  321. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  322. chip->remap_addr + dma->ops->llp_offset);
  323. dma->period_bytes = period_bytes;
  324. dma->periods = periods;
  325. return 0;
  326. }
  327. /*
  328. * remove the ring buffer and release it if assigned
  329. */
  330. static void atiixp_clear_dma_packets(atiixp_t *chip, atiixp_dma_t *dma, snd_pcm_substream_t *substream)
  331. {
  332. if (dma->desc_buf.area) {
  333. writel(0, chip->remap_addr + dma->ops->llp_offset);
  334. snd_dma_free_pages(&dma->desc_buf);
  335. dma->desc_buf.area = NULL;
  336. }
  337. }
  338. /*
  339. * AC97 interface
  340. */
  341. static int snd_atiixp_acquire_codec(atiixp_t *chip)
  342. {
  343. int timeout = 1000;
  344. while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
  345. if (! timeout--) {
  346. snd_printk(KERN_WARNING "atiixp: codec acquire timeout\n");
  347. return -EBUSY;
  348. }
  349. udelay(1);
  350. }
  351. return 0;
  352. }
  353. static unsigned short snd_atiixp_codec_read(atiixp_t *chip, unsigned short codec, unsigned short reg)
  354. {
  355. unsigned int data;
  356. int timeout;
  357. if (snd_atiixp_acquire_codec(chip) < 0)
  358. return 0xffff;
  359. data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  360. ATI_REG_PHYS_OUT_ADDR_EN |
  361. ATI_REG_PHYS_OUT_RW |
  362. codec;
  363. atiixp_write(chip, PHYS_OUT_ADDR, data);
  364. if (snd_atiixp_acquire_codec(chip) < 0)
  365. return 0xffff;
  366. timeout = 1000;
  367. do {
  368. data = atiixp_read(chip, PHYS_IN_ADDR);
  369. if (data & ATI_REG_PHYS_IN_READ_FLAG)
  370. return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
  371. udelay(1);
  372. } while (--timeout);
  373. /* time out may happen during reset */
  374. if (reg < 0x7c)
  375. snd_printk(KERN_WARNING "atiixp: codec read timeout (reg %x)\n", reg);
  376. return 0xffff;
  377. }
  378. static void snd_atiixp_codec_write(atiixp_t *chip, unsigned short codec, unsigned short reg, unsigned short val)
  379. {
  380. unsigned int data;
  381. if (snd_atiixp_acquire_codec(chip) < 0)
  382. return;
  383. data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
  384. ((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  385. ATI_REG_PHYS_OUT_ADDR_EN | codec;
  386. atiixp_write(chip, PHYS_OUT_ADDR, data);
  387. }
  388. static unsigned short snd_atiixp_ac97_read(ac97_t *ac97, unsigned short reg)
  389. {
  390. atiixp_t *chip = ac97->private_data;
  391. return snd_atiixp_codec_read(chip, ac97->num, reg);
  392. }
  393. static void snd_atiixp_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
  394. {
  395. atiixp_t *chip = ac97->private_data;
  396. if (reg == AC97_GPIO_STATUS) {
  397. atiixp_write(chip, MODEM_OUT_GPIO,
  398. (val << ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT) | ATI_REG_MODEM_OUT_GPIO_EN);
  399. return;
  400. }
  401. snd_atiixp_codec_write(chip, ac97->num, reg, val);
  402. }
  403. /*
  404. * reset AC link
  405. */
  406. static int snd_atiixp_aclink_reset(atiixp_t *chip)
  407. {
  408. int timeout;
  409. /* reset powerdoewn */
  410. if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
  411. udelay(10);
  412. /* perform a software reset */
  413. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
  414. atiixp_read(chip, CMD);
  415. udelay(10);
  416. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
  417. timeout = 10;
  418. while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
  419. /* do a hard reset */
  420. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  421. ATI_REG_CMD_AC_SYNC);
  422. atiixp_read(chip, CMD);
  423. do_delay();
  424. atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
  425. if (--timeout) {
  426. snd_printk(KERN_ERR "atiixp: codec reset timeout\n");
  427. break;
  428. }
  429. }
  430. /* deassert RESET and assert SYNC to make sure */
  431. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  432. ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
  433. return 0;
  434. }
  435. #ifdef CONFIG_PM
  436. static int snd_atiixp_aclink_down(atiixp_t *chip)
  437. {
  438. // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
  439. // return -EBUSY;
  440. atiixp_update(chip, CMD,
  441. ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
  442. ATI_REG_CMD_POWERDOWN);
  443. return 0;
  444. }
  445. #endif
  446. /*
  447. * auto-detection of codecs
  448. *
  449. * the IXP chip can generate interrupts for the non-existing codecs.
  450. * NEW_FRAME interrupt is used to make sure that the interrupt is generated
  451. * even if all three codecs are connected.
  452. */
  453. #define ALL_CODEC_NOT_READY \
  454. (ATI_REG_ISR_CODEC0_NOT_READY |\
  455. ATI_REG_ISR_CODEC1_NOT_READY |\
  456. ATI_REG_ISR_CODEC2_NOT_READY)
  457. #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
  458. static int snd_atiixp_codec_detect(atiixp_t *chip)
  459. {
  460. int timeout;
  461. chip->codec_not_ready_bits = 0;
  462. atiixp_write(chip, IER, CODEC_CHECK_BITS);
  463. /* wait for the interrupts */
  464. timeout = HZ / 10;
  465. while (timeout-- > 0) {
  466. do_delay();
  467. if (chip->codec_not_ready_bits)
  468. break;
  469. }
  470. atiixp_write(chip, IER, 0); /* disable irqs */
  471. if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
  472. snd_printk(KERN_ERR "atiixp: no codec detected!\n");
  473. return -ENXIO;
  474. }
  475. return 0;
  476. }
  477. /*
  478. * enable DMA and irqs
  479. */
  480. static int snd_atiixp_chip_start(atiixp_t *chip)
  481. {
  482. unsigned int reg;
  483. /* set up spdif, enable burst mode */
  484. reg = atiixp_read(chip, CMD);
  485. reg |= ATI_REG_CMD_BURST_EN;
  486. if(!(reg & ATI_REG_CMD_MODEM_PRESENT))
  487. reg |= ATI_REG_CMD_MODEM_PRESENT;
  488. atiixp_write(chip, CMD, reg);
  489. /* clear all interrupt source */
  490. atiixp_write(chip, ISR, 0xffffffff);
  491. /* enable irqs */
  492. atiixp_write(chip, IER,
  493. ATI_REG_IER_MODEM_STATUS_EN |
  494. ATI_REG_IER_MODEM_IN_XRUN_EN |
  495. ATI_REG_IER_MODEM_OUT1_XRUN_EN);
  496. return 0;
  497. }
  498. /*
  499. * disable DMA and IRQs
  500. */
  501. static int snd_atiixp_chip_stop(atiixp_t *chip)
  502. {
  503. /* clear interrupt source */
  504. atiixp_write(chip, ISR, atiixp_read(chip, ISR));
  505. /* disable irqs */
  506. atiixp_write(chip, IER, 0);
  507. return 0;
  508. }
  509. /*
  510. * PCM section
  511. */
  512. /*
  513. * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
  514. * position. when SG-buffer is implemented, the offset must be calculated
  515. * correctly...
  516. */
  517. static snd_pcm_uframes_t snd_atiixp_pcm_pointer(snd_pcm_substream_t *substream)
  518. {
  519. atiixp_t *chip = snd_pcm_substream_chip(substream);
  520. snd_pcm_runtime_t *runtime = substream->runtime;
  521. atiixp_dma_t *dma = (atiixp_dma_t *)runtime->private_data;
  522. unsigned int curptr;
  523. int timeout = 1000;
  524. while (timeout--) {
  525. curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  526. if (curptr < dma->buf_addr)
  527. continue;
  528. curptr -= dma->buf_addr;
  529. if (curptr >= dma->buf_bytes)
  530. continue;
  531. return bytes_to_frames(runtime, curptr);
  532. }
  533. snd_printd("atiixp-modem: invalid DMA pointer read 0x%x (buf=%x)\n",
  534. readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
  535. return 0;
  536. }
  537. /*
  538. * XRUN detected, and stop the PCM substream
  539. */
  540. static void snd_atiixp_xrun_dma(atiixp_t *chip, atiixp_dma_t *dma)
  541. {
  542. if (! dma->substream || ! dma->running)
  543. return;
  544. snd_printdd("atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
  545. snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
  546. }
  547. /*
  548. * the period ack. update the substream.
  549. */
  550. static void snd_atiixp_update_dma(atiixp_t *chip, atiixp_dma_t *dma)
  551. {
  552. if (! dma->substream || ! dma->running)
  553. return;
  554. snd_pcm_period_elapsed(dma->substream);
  555. }
  556. /* set BUS_BUSY interrupt bit if any DMA is running */
  557. /* call with spinlock held */
  558. static void snd_atiixp_check_bus_busy(atiixp_t *chip)
  559. {
  560. unsigned int bus_busy;
  561. if (atiixp_read(chip, CMD) & (ATI_REG_CMD_MODEM_SEND1_EN |
  562. ATI_REG_CMD_MODEM_RECEIVE_EN))
  563. bus_busy = ATI_REG_IER_MODEM_SET_BUS_BUSY;
  564. else
  565. bus_busy = 0;
  566. atiixp_update(chip, IER, ATI_REG_IER_MODEM_SET_BUS_BUSY, bus_busy);
  567. }
  568. /* common trigger callback
  569. * calling the lowlevel callbacks in it
  570. */
  571. static int snd_atiixp_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  572. {
  573. atiixp_t *chip = snd_pcm_substream_chip(substream);
  574. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  575. int err = 0;
  576. snd_assert(dma->ops->enable_transfer && dma->ops->flush_dma, return -EINVAL);
  577. spin_lock(&chip->reg_lock);
  578. switch(cmd) {
  579. case SNDRV_PCM_TRIGGER_START:
  580. dma->ops->enable_transfer(chip, 1);
  581. dma->running = 1;
  582. break;
  583. case SNDRV_PCM_TRIGGER_STOP:
  584. dma->ops->enable_transfer(chip, 0);
  585. dma->running = 0;
  586. break;
  587. default:
  588. err = -EINVAL;
  589. break;
  590. }
  591. if (! err) {
  592. snd_atiixp_check_bus_busy(chip);
  593. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  594. dma->ops->flush_dma(chip);
  595. snd_atiixp_check_bus_busy(chip);
  596. }
  597. }
  598. spin_unlock(&chip->reg_lock);
  599. return err;
  600. }
  601. /*
  602. * lowlevel callbacks for each DMA type
  603. *
  604. * every callback is supposed to be called in chip->reg_lock spinlock
  605. */
  606. /* flush FIFO of analog OUT DMA */
  607. static void atiixp_out_flush_dma(atiixp_t *chip)
  608. {
  609. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_OUT1_FLUSH);
  610. }
  611. /* enable/disable analog OUT DMA */
  612. static void atiixp_out_enable_dma(atiixp_t *chip, int on)
  613. {
  614. unsigned int data;
  615. data = atiixp_read(chip, CMD);
  616. if (on) {
  617. if (data & ATI_REG_CMD_MODEM_OUT_DMA1_EN)
  618. return;
  619. atiixp_out_flush_dma(chip);
  620. data |= ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  621. } else
  622. data &= ~ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  623. atiixp_write(chip, CMD, data);
  624. }
  625. /* start/stop transfer over OUT DMA */
  626. static void atiixp_out_enable_transfer(atiixp_t *chip, int on)
  627. {
  628. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_SEND1_EN,
  629. on ? ATI_REG_CMD_MODEM_SEND1_EN : 0);
  630. }
  631. /* enable/disable analog IN DMA */
  632. static void atiixp_in_enable_dma(atiixp_t *chip, int on)
  633. {
  634. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_IN_DMA_EN,
  635. on ? ATI_REG_CMD_MODEM_IN_DMA_EN : 0);
  636. }
  637. /* start/stop analog IN DMA */
  638. static void atiixp_in_enable_transfer(atiixp_t *chip, int on)
  639. {
  640. if (on) {
  641. unsigned int data = atiixp_read(chip, CMD);
  642. if (! (data & ATI_REG_CMD_MODEM_RECEIVE_EN)) {
  643. data |= ATI_REG_CMD_MODEM_RECEIVE_EN;
  644. atiixp_write(chip, CMD, data);
  645. }
  646. } else
  647. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_RECEIVE_EN, 0);
  648. }
  649. /* flush FIFO of analog IN DMA */
  650. static void atiixp_in_flush_dma(atiixp_t *chip)
  651. {
  652. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_IN_FLUSH);
  653. }
  654. /* set up slots and formats for analog OUT */
  655. static int snd_atiixp_playback_prepare(snd_pcm_substream_t *substream)
  656. {
  657. atiixp_t *chip = snd_pcm_substream_chip(substream);
  658. unsigned int data;
  659. spin_lock_irq(&chip->reg_lock);
  660. /* set output threshold */
  661. data = atiixp_read(chip, MODEM_OUT_FIFO);
  662. data &= ~ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK;
  663. data |= 0x04 << ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT;
  664. atiixp_write(chip, MODEM_OUT_FIFO, data);
  665. spin_unlock_irq(&chip->reg_lock);
  666. return 0;
  667. }
  668. /* set up slots and formats for analog IN */
  669. static int snd_atiixp_capture_prepare(snd_pcm_substream_t *substream)
  670. {
  671. return 0;
  672. }
  673. /*
  674. * hw_params - allocate the buffer and set up buffer descriptors
  675. */
  676. static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
  677. snd_pcm_hw_params_t *hw_params)
  678. {
  679. atiixp_t *chip = snd_pcm_substream_chip(substream);
  680. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  681. int err;
  682. int i;
  683. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  684. if (err < 0)
  685. return err;
  686. dma->buf_addr = substream->runtime->dma_addr;
  687. dma->buf_bytes = params_buffer_bytes(hw_params);
  688. err = atiixp_build_dma_packets(chip, dma, substream,
  689. params_periods(hw_params),
  690. params_period_bytes(hw_params));
  691. if (err < 0)
  692. return err;
  693. /* set up modem rate */
  694. for (i = 0; i < NUM_ATI_CODECS; i++) {
  695. if (! chip->ac97[i])
  696. continue;
  697. snd_ac97_write(chip->ac97[i], AC97_LINE1_RATE, params_rate(hw_params));
  698. snd_ac97_write(chip->ac97[i], AC97_LINE1_LEVEL, 0);
  699. }
  700. return err;
  701. }
  702. static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
  703. {
  704. atiixp_t *chip = snd_pcm_substream_chip(substream);
  705. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  706. atiixp_clear_dma_packets(chip, dma, substream);
  707. snd_pcm_lib_free_pages(substream);
  708. return 0;
  709. }
  710. /*
  711. * pcm hardware definition, identical for all DMA types
  712. */
  713. static snd_pcm_hardware_t snd_atiixp_pcm_hw =
  714. {
  715. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  716. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  717. SNDRV_PCM_INFO_MMAP_VALID),
  718. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  719. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  720. .rate_min = 8000,
  721. .rate_max = 16000,
  722. .channels_min = 2,
  723. .channels_max = 2,
  724. .buffer_bytes_max = 256 * 1024,
  725. .period_bytes_min = 32,
  726. .period_bytes_max = 128 * 1024,
  727. .periods_min = 2,
  728. .periods_max = ATI_MAX_DESCRIPTORS,
  729. };
  730. static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
  731. {
  732. atiixp_t *chip = snd_pcm_substream_chip(substream);
  733. snd_pcm_runtime_t *runtime = substream->runtime;
  734. int err;
  735. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  736. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  737. .count = ARRAY_SIZE(rates),
  738. .list = rates,
  739. .mask = 0,
  740. };
  741. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  742. if (dma->opened)
  743. return -EBUSY;
  744. dma->substream = substream;
  745. runtime->hw = snd_atiixp_pcm_hw;
  746. dma->ac97_pcm_type = pcm_type;
  747. if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
  748. return err;
  749. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  750. return err;
  751. runtime->private_data = dma;
  752. /* enable DMA bits */
  753. spin_lock_irq(&chip->reg_lock);
  754. dma->ops->enable_dma(chip, 1);
  755. spin_unlock_irq(&chip->reg_lock);
  756. dma->opened = 1;
  757. return 0;
  758. }
  759. static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
  760. {
  761. atiixp_t *chip = snd_pcm_substream_chip(substream);
  762. /* disable DMA bits */
  763. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  764. spin_lock_irq(&chip->reg_lock);
  765. dma->ops->enable_dma(chip, 0);
  766. spin_unlock_irq(&chip->reg_lock);
  767. dma->substream = NULL;
  768. dma->opened = 0;
  769. return 0;
  770. }
  771. /*
  772. */
  773. static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
  774. {
  775. atiixp_t *chip = snd_pcm_substream_chip(substream);
  776. int err;
  777. down(&chip->open_mutex);
  778. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  779. up(&chip->open_mutex);
  780. if (err < 0)
  781. return err;
  782. return 0;
  783. }
  784. static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
  785. {
  786. atiixp_t *chip = snd_pcm_substream_chip(substream);
  787. int err;
  788. down(&chip->open_mutex);
  789. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  790. up(&chip->open_mutex);
  791. return err;
  792. }
  793. static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
  794. {
  795. atiixp_t *chip = snd_pcm_substream_chip(substream);
  796. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  797. }
  798. static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
  799. {
  800. atiixp_t *chip = snd_pcm_substream_chip(substream);
  801. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  802. }
  803. /* AC97 playback */
  804. static snd_pcm_ops_t snd_atiixp_playback_ops = {
  805. .open = snd_atiixp_playback_open,
  806. .close = snd_atiixp_playback_close,
  807. .ioctl = snd_pcm_lib_ioctl,
  808. .hw_params = snd_atiixp_pcm_hw_params,
  809. .hw_free = snd_atiixp_pcm_hw_free,
  810. .prepare = snd_atiixp_playback_prepare,
  811. .trigger = snd_atiixp_pcm_trigger,
  812. .pointer = snd_atiixp_pcm_pointer,
  813. };
  814. /* AC97 capture */
  815. static snd_pcm_ops_t snd_atiixp_capture_ops = {
  816. .open = snd_atiixp_capture_open,
  817. .close = snd_atiixp_capture_close,
  818. .ioctl = snd_pcm_lib_ioctl,
  819. .hw_params = snd_atiixp_pcm_hw_params,
  820. .hw_free = snd_atiixp_pcm_hw_free,
  821. .prepare = snd_atiixp_capture_prepare,
  822. .trigger = snd_atiixp_pcm_trigger,
  823. .pointer = snd_atiixp_pcm_pointer,
  824. };
  825. static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
  826. .type = ATI_DMA_PLAYBACK,
  827. .llp_offset = ATI_REG_MODEM_OUT_DMA1_LINKPTR,
  828. .dt_cur = ATI_REG_MODEM_OUT_DMA1_DT_CUR,
  829. .enable_dma = atiixp_out_enable_dma,
  830. .enable_transfer = atiixp_out_enable_transfer,
  831. .flush_dma = atiixp_out_flush_dma,
  832. };
  833. static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
  834. .type = ATI_DMA_CAPTURE,
  835. .llp_offset = ATI_REG_MODEM_IN_DMA_LINKPTR,
  836. .dt_cur = ATI_REG_MODEM_IN_DMA_DT_CUR,
  837. .enable_dma = atiixp_in_enable_dma,
  838. .enable_transfer = atiixp_in_enable_transfer,
  839. .flush_dma = atiixp_in_flush_dma,
  840. };
  841. static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
  842. {
  843. snd_pcm_t *pcm;
  844. int err;
  845. /* initialize constants */
  846. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  847. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  848. /* PCM #0: analog I/O */
  849. err = snd_pcm_new(chip->card, "ATI IXP MC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  850. if (err < 0)
  851. return err;
  852. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  853. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  854. pcm->private_data = chip;
  855. strcpy(pcm->name, "ATI IXP MC97");
  856. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  857. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  858. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  859. return 0;
  860. }
  861. /*
  862. * interrupt handler
  863. */
  864. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  865. {
  866. atiixp_t *chip = dev_id;
  867. unsigned int status;
  868. status = atiixp_read(chip, ISR);
  869. if (! status)
  870. return IRQ_NONE;
  871. /* process audio DMA */
  872. if (status & ATI_REG_ISR_MODEM_OUT1_XRUN)
  873. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  874. else if (status & ATI_REG_ISR_MODEM_OUT1_STATUS)
  875. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  876. if (status & ATI_REG_ISR_MODEM_IN_XRUN)
  877. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  878. else if (status & ATI_REG_ISR_MODEM_IN_STATUS)
  879. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  880. /* for codec detection */
  881. if (status & CODEC_CHECK_BITS) {
  882. unsigned int detected;
  883. detected = status & CODEC_CHECK_BITS;
  884. spin_lock(&chip->reg_lock);
  885. chip->codec_not_ready_bits |= detected;
  886. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  887. spin_unlock(&chip->reg_lock);
  888. }
  889. /* ack */
  890. atiixp_write(chip, ISR, status);
  891. return IRQ_HANDLED;
  892. }
  893. /*
  894. * ac97 mixer section
  895. */
  896. static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock)
  897. {
  898. ac97_bus_t *pbus;
  899. ac97_template_t ac97;
  900. int i, err;
  901. int codec_count;
  902. static ac97_bus_ops_t ops = {
  903. .write = snd_atiixp_ac97_write,
  904. .read = snd_atiixp_ac97_read,
  905. };
  906. static unsigned int codec_skip[NUM_ATI_CODECS] = {
  907. ATI_REG_ISR_CODEC0_NOT_READY,
  908. ATI_REG_ISR_CODEC1_NOT_READY,
  909. ATI_REG_ISR_CODEC2_NOT_READY,
  910. };
  911. if (snd_atiixp_codec_detect(chip) < 0)
  912. return -ENXIO;
  913. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  914. return err;
  915. pbus->clock = clock;
  916. pbus->shared_type = AC97_SHARED_TYPE_ATIIXP; /* shared with audio driver */
  917. chip->ac97_bus = pbus;
  918. codec_count = 0;
  919. for (i = 0; i < NUM_ATI_CODECS; i++) {
  920. if (chip->codec_not_ready_bits & codec_skip[i])
  921. continue;
  922. memset(&ac97, 0, sizeof(ac97));
  923. ac97.private_data = chip;
  924. ac97.pci = chip->pci;
  925. ac97.num = i;
  926. ac97.scaps = AC97_SCAP_SKIP_AUDIO;
  927. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  928. chip->ac97[i] = NULL; /* to be sure */
  929. snd_printdd("atiixp: codec %d not available for modem\n", i);
  930. continue;
  931. }
  932. codec_count++;
  933. }
  934. if (! codec_count) {
  935. snd_printk(KERN_ERR "atiixp: no codec available\n");
  936. return -ENODEV;
  937. }
  938. /* snd_ac97_tune_hardware(chip->ac97, ac97_quirks); */
  939. return 0;
  940. }
  941. #ifdef CONFIG_PM
  942. /*
  943. * power management
  944. */
  945. static int snd_atiixp_suspend(snd_card_t *card, pm_message_t state)
  946. {
  947. atiixp_t *chip = card->pm_private_data;
  948. int i;
  949. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  950. if (chip->pcmdevs[i])
  951. snd_pcm_suspend_all(chip->pcmdevs[i]);
  952. for (i = 0; i < NUM_ATI_CODECS; i++)
  953. if (chip->ac97[i])
  954. snd_ac97_suspend(chip->ac97[i]);
  955. snd_atiixp_aclink_down(chip);
  956. snd_atiixp_chip_stop(chip);
  957. pci_set_power_state(chip->pci, PCI_D3hot);
  958. pci_disable_device(chip->pci);
  959. return 0;
  960. }
  961. static int snd_atiixp_resume(snd_card_t *card)
  962. {
  963. atiixp_t *chip = card->pm_private_data;
  964. int i;
  965. pci_enable_device(chip->pci);
  966. pci_set_power_state(chip->pci, PCI_D0);
  967. pci_set_master(chip->pci);
  968. snd_atiixp_aclink_reset(chip);
  969. snd_atiixp_chip_start(chip);
  970. for (i = 0; i < NUM_ATI_CODECS; i++)
  971. if (chip->ac97[i])
  972. snd_ac97_resume(chip->ac97[i]);
  973. return 0;
  974. }
  975. #endif /* CONFIG_PM */
  976. /*
  977. * proc interface for register dump
  978. */
  979. static void snd_atiixp_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  980. {
  981. atiixp_t *chip = entry->private_data;
  982. int i;
  983. for (i = 0; i < 256; i += 4)
  984. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  985. }
  986. static void __devinit snd_atiixp_proc_init(atiixp_t *chip)
  987. {
  988. snd_info_entry_t *entry;
  989. if (! snd_card_proc_new(chip->card, "atiixp", &entry))
  990. snd_info_set_text_ops(entry, chip, 1024, snd_atiixp_proc_read);
  991. }
  992. /*
  993. * destructor
  994. */
  995. static int snd_atiixp_free(atiixp_t *chip)
  996. {
  997. if (chip->irq < 0)
  998. goto __hw_end;
  999. snd_atiixp_chip_stop(chip);
  1000. synchronize_irq(chip->irq);
  1001. __hw_end:
  1002. if (chip->irq >= 0)
  1003. free_irq(chip->irq, (void *)chip);
  1004. if (chip->remap_addr)
  1005. iounmap(chip->remap_addr);
  1006. pci_release_regions(chip->pci);
  1007. pci_disable_device(chip->pci);
  1008. kfree(chip);
  1009. return 0;
  1010. }
  1011. static int snd_atiixp_dev_free(snd_device_t *device)
  1012. {
  1013. atiixp_t *chip = device->device_data;
  1014. return snd_atiixp_free(chip);
  1015. }
  1016. /*
  1017. * constructor for chip instance
  1018. */
  1019. static int __devinit snd_atiixp_create(snd_card_t *card,
  1020. struct pci_dev *pci,
  1021. atiixp_t **r_chip)
  1022. {
  1023. static snd_device_ops_t ops = {
  1024. .dev_free = snd_atiixp_dev_free,
  1025. };
  1026. atiixp_t *chip;
  1027. int err;
  1028. if ((err = pci_enable_device(pci)) < 0)
  1029. return err;
  1030. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1031. if (chip == NULL) {
  1032. pci_disable_device(pci);
  1033. return -ENOMEM;
  1034. }
  1035. spin_lock_init(&chip->reg_lock);
  1036. init_MUTEX(&chip->open_mutex);
  1037. chip->card = card;
  1038. chip->pci = pci;
  1039. chip->irq = -1;
  1040. if ((err = pci_request_regions(pci, "ATI IXP MC97")) < 0) {
  1041. kfree(chip);
  1042. pci_disable_device(pci);
  1043. return err;
  1044. }
  1045. chip->addr = pci_resource_start(pci, 0);
  1046. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
  1047. if (chip->remap_addr == NULL) {
  1048. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1049. snd_atiixp_free(chip);
  1050. return -EIO;
  1051. }
  1052. if (request_irq(pci->irq, snd_atiixp_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
  1053. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1054. snd_atiixp_free(chip);
  1055. return -EBUSY;
  1056. }
  1057. chip->irq = pci->irq;
  1058. pci_set_master(pci);
  1059. synchronize_irq(chip->irq);
  1060. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1061. snd_atiixp_free(chip);
  1062. return err;
  1063. }
  1064. snd_card_set_dev(card, &pci->dev);
  1065. *r_chip = chip;
  1066. return 0;
  1067. }
  1068. static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  1069. const struct pci_device_id *pci_id)
  1070. {
  1071. static int dev;
  1072. snd_card_t *card;
  1073. atiixp_t *chip;
  1074. unsigned char revision;
  1075. int err;
  1076. if (dev >= SNDRV_CARDS)
  1077. return -ENODEV;
  1078. if (!enable[dev]) {
  1079. dev++;
  1080. return -ENOENT;
  1081. }
  1082. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1083. if (card == NULL)
  1084. return -ENOMEM;
  1085. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  1086. strcpy(card->driver, "ATIIXP-MODEM");
  1087. strcpy(card->shortname, "ATI IXP Modem");
  1088. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1089. goto __error;
  1090. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1091. goto __error;
  1092. if ((err = snd_atiixp_mixer_new(chip, ac97_clock[dev])) < 0)
  1093. goto __error;
  1094. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1095. goto __error;
  1096. snd_atiixp_proc_init(chip);
  1097. snd_atiixp_chip_start(chip);
  1098. sprintf(card->longname, "%s rev %x at 0x%lx, irq %i",
  1099. card->shortname, revision, chip->addr, chip->irq);
  1100. snd_card_set_pm_callback(card, snd_atiixp_suspend, snd_atiixp_resume, chip);
  1101. if ((err = snd_card_register(card)) < 0)
  1102. goto __error;
  1103. pci_set_drvdata(pci, card);
  1104. dev++;
  1105. return 0;
  1106. __error:
  1107. snd_card_free(card);
  1108. return err;
  1109. }
  1110. static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  1111. {
  1112. snd_card_free(pci_get_drvdata(pci));
  1113. pci_set_drvdata(pci, NULL);
  1114. }
  1115. static struct pci_driver driver = {
  1116. .name = "ATI IXP MC97 controller",
  1117. .id_table = snd_atiixp_ids,
  1118. .probe = snd_atiixp_probe,
  1119. .remove = __devexit_p(snd_atiixp_remove),
  1120. SND_PCI_PM_CALLBACKS
  1121. };
  1122. static int __init alsa_card_atiixp_init(void)
  1123. {
  1124. return pci_register_driver(&driver);
  1125. }
  1126. static void __exit alsa_card_atiixp_exit(void)
  1127. {
  1128. pci_unregister_driver(&driver);
  1129. }
  1130. module_init(alsa_card_atiixp_init)
  1131. module_exit(alsa_card_atiixp_exit)