atiixp_modem.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. snd_atiixp_codec_write(chip, ac97->num, reg, val);
  397. }
  398. /*
  399. * reset AC link
  400. */
  401. static int snd_atiixp_aclink_reset(atiixp_t *chip)
  402. {
  403. int timeout;
  404. /* reset powerdoewn */
  405. if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
  406. udelay(10);
  407. /* perform a software reset */
  408. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
  409. atiixp_read(chip, CMD);
  410. udelay(10);
  411. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
  412. timeout = 10;
  413. while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
  414. /* do a hard reset */
  415. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  416. ATI_REG_CMD_AC_SYNC);
  417. atiixp_read(chip, CMD);
  418. do_delay();
  419. atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
  420. if (--timeout) {
  421. snd_printk(KERN_ERR "atiixp: codec reset timeout\n");
  422. break;
  423. }
  424. }
  425. /* deassert RESET and assert SYNC to make sure */
  426. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  427. ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
  428. return 0;
  429. }
  430. #ifdef CONFIG_PM
  431. static int snd_atiixp_aclink_down(atiixp_t *chip)
  432. {
  433. // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
  434. // return -EBUSY;
  435. atiixp_update(chip, CMD,
  436. ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
  437. ATI_REG_CMD_POWERDOWN);
  438. return 0;
  439. }
  440. #endif
  441. /*
  442. * auto-detection of codecs
  443. *
  444. * the IXP chip can generate interrupts for the non-existing codecs.
  445. * NEW_FRAME interrupt is used to make sure that the interrupt is generated
  446. * even if all three codecs are connected.
  447. */
  448. #define ALL_CODEC_NOT_READY \
  449. (ATI_REG_ISR_CODEC0_NOT_READY |\
  450. ATI_REG_ISR_CODEC1_NOT_READY |\
  451. ATI_REG_ISR_CODEC2_NOT_READY)
  452. #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
  453. static int snd_atiixp_codec_detect(atiixp_t *chip)
  454. {
  455. int timeout;
  456. chip->codec_not_ready_bits = 0;
  457. atiixp_write(chip, IER, CODEC_CHECK_BITS);
  458. /* wait for the interrupts */
  459. timeout = HZ / 10;
  460. while (timeout-- > 0) {
  461. do_delay();
  462. if (chip->codec_not_ready_bits)
  463. break;
  464. }
  465. atiixp_write(chip, IER, 0); /* disable irqs */
  466. if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
  467. snd_printk(KERN_ERR "atiixp: no codec detected!\n");
  468. return -ENXIO;
  469. }
  470. return 0;
  471. }
  472. /*
  473. * enable DMA and irqs
  474. */
  475. static int snd_atiixp_chip_start(atiixp_t *chip)
  476. {
  477. unsigned int reg;
  478. /* set up spdif, enable burst mode */
  479. reg = atiixp_read(chip, CMD);
  480. reg |= ATI_REG_CMD_BURST_EN;
  481. if(!(reg & ATI_REG_CMD_MODEM_PRESENT))
  482. reg |= ATI_REG_CMD_MODEM_PRESENT;
  483. atiixp_write(chip, CMD, reg);
  484. /* clear all interrupt source */
  485. atiixp_write(chip, ISR, 0xffffffff);
  486. /* enable irqs */
  487. atiixp_write(chip, IER,
  488. ATI_REG_IER_MODEM_STATUS_EN |
  489. ATI_REG_IER_MODEM_IN_XRUN_EN |
  490. ATI_REG_IER_MODEM_OUT1_XRUN_EN);
  491. return 0;
  492. }
  493. /*
  494. * disable DMA and IRQs
  495. */
  496. static int snd_atiixp_chip_stop(atiixp_t *chip)
  497. {
  498. /* clear interrupt source */
  499. atiixp_write(chip, ISR, atiixp_read(chip, ISR));
  500. /* disable irqs */
  501. atiixp_write(chip, IER, 0);
  502. return 0;
  503. }
  504. /*
  505. * PCM section
  506. */
  507. /*
  508. * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
  509. * position. when SG-buffer is implemented, the offset must be calculated
  510. * correctly...
  511. */
  512. static snd_pcm_uframes_t snd_atiixp_pcm_pointer(snd_pcm_substream_t *substream)
  513. {
  514. atiixp_t *chip = snd_pcm_substream_chip(substream);
  515. snd_pcm_runtime_t *runtime = substream->runtime;
  516. atiixp_dma_t *dma = (atiixp_dma_t *)runtime->private_data;
  517. unsigned int curptr;
  518. int timeout = 1000;
  519. while (timeout--) {
  520. curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  521. if (curptr < dma->buf_addr)
  522. continue;
  523. curptr -= dma->buf_addr;
  524. if (curptr >= dma->buf_bytes)
  525. continue;
  526. return bytes_to_frames(runtime, curptr);
  527. }
  528. snd_printd("atiixp-modem: invalid DMA pointer read 0x%x (buf=%x)\n",
  529. readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
  530. return 0;
  531. }
  532. /*
  533. * XRUN detected, and stop the PCM substream
  534. */
  535. static void snd_atiixp_xrun_dma(atiixp_t *chip, atiixp_dma_t *dma)
  536. {
  537. if (! dma->substream || ! dma->running)
  538. return;
  539. snd_printdd("atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
  540. snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
  541. }
  542. /*
  543. * the period ack. update the substream.
  544. */
  545. static void snd_atiixp_update_dma(atiixp_t *chip, atiixp_dma_t *dma)
  546. {
  547. if (! dma->substream || ! dma->running)
  548. return;
  549. snd_pcm_period_elapsed(dma->substream);
  550. }
  551. /* set BUS_BUSY interrupt bit if any DMA is running */
  552. /* call with spinlock held */
  553. static void snd_atiixp_check_bus_busy(atiixp_t *chip)
  554. {
  555. unsigned int bus_busy;
  556. if (atiixp_read(chip, CMD) & (ATI_REG_CMD_MODEM_SEND1_EN |
  557. ATI_REG_CMD_MODEM_RECEIVE_EN))
  558. bus_busy = ATI_REG_IER_MODEM_SET_BUS_BUSY;
  559. else
  560. bus_busy = 0;
  561. atiixp_update(chip, IER, ATI_REG_IER_MODEM_SET_BUS_BUSY, bus_busy);
  562. }
  563. /* common trigger callback
  564. * calling the lowlevel callbacks in it
  565. */
  566. static int snd_atiixp_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  567. {
  568. atiixp_t *chip = snd_pcm_substream_chip(substream);
  569. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  570. unsigned int reg = 0;
  571. int i;
  572. snd_assert(dma->ops->enable_transfer && dma->ops->flush_dma, return -EINVAL);
  573. if (cmd != SNDRV_PCM_TRIGGER_START && cmd != SNDRV_PCM_TRIGGER_STOP)
  574. return -EINVAL;
  575. spin_lock(&chip->reg_lock);
  576. /* hook off/on: via GPIO_OUT */
  577. for (i = 0; i < NUM_ATI_CODECS; i++) {
  578. if (chip->ac97[i]) {
  579. reg = snd_ac97_read(chip->ac97[i], AC97_GPIO_STATUS);
  580. break;
  581. }
  582. }
  583. if(cmd == SNDRV_PCM_TRIGGER_START)
  584. reg |= AC97_GPIO_LINE1_OH;
  585. else
  586. reg &= ~AC97_GPIO_LINE1_OH;
  587. reg = (reg << ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT) | ATI_REG_MODEM_OUT_GPIO_EN ;
  588. atiixp_write(chip, MODEM_OUT_GPIO, reg);
  589. if (cmd == SNDRV_PCM_TRIGGER_START) {
  590. dma->ops->enable_transfer(chip, 1);
  591. dma->running = 1;
  592. } else {
  593. dma->ops->enable_transfer(chip, 0);
  594. dma->running = 0;
  595. }
  596. snd_atiixp_check_bus_busy(chip);
  597. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  598. dma->ops->flush_dma(chip);
  599. snd_atiixp_check_bus_busy(chip);
  600. }
  601. spin_unlock(&chip->reg_lock);
  602. return 0;
  603. }
  604. /*
  605. * lowlevel callbacks for each DMA type
  606. *
  607. * every callback is supposed to be called in chip->reg_lock spinlock
  608. */
  609. /* flush FIFO of analog OUT DMA */
  610. static void atiixp_out_flush_dma(atiixp_t *chip)
  611. {
  612. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_OUT1_FLUSH);
  613. }
  614. /* enable/disable analog OUT DMA */
  615. static void atiixp_out_enable_dma(atiixp_t *chip, int on)
  616. {
  617. unsigned int data;
  618. data = atiixp_read(chip, CMD);
  619. if (on) {
  620. if (data & ATI_REG_CMD_MODEM_OUT_DMA1_EN)
  621. return;
  622. atiixp_out_flush_dma(chip);
  623. data |= ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  624. } else
  625. data &= ~ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  626. atiixp_write(chip, CMD, data);
  627. }
  628. /* start/stop transfer over OUT DMA */
  629. static void atiixp_out_enable_transfer(atiixp_t *chip, int on)
  630. {
  631. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_SEND1_EN,
  632. on ? ATI_REG_CMD_MODEM_SEND1_EN : 0);
  633. }
  634. /* enable/disable analog IN DMA */
  635. static void atiixp_in_enable_dma(atiixp_t *chip, int on)
  636. {
  637. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_IN_DMA_EN,
  638. on ? ATI_REG_CMD_MODEM_IN_DMA_EN : 0);
  639. }
  640. /* start/stop analog IN DMA */
  641. static void atiixp_in_enable_transfer(atiixp_t *chip, int on)
  642. {
  643. if (on) {
  644. unsigned int data = atiixp_read(chip, CMD);
  645. if (! (data & ATI_REG_CMD_MODEM_RECEIVE_EN)) {
  646. data |= ATI_REG_CMD_MODEM_RECEIVE_EN;
  647. atiixp_write(chip, CMD, data);
  648. }
  649. } else
  650. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_RECEIVE_EN, 0);
  651. }
  652. /* flush FIFO of analog IN DMA */
  653. static void atiixp_in_flush_dma(atiixp_t *chip)
  654. {
  655. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_IN_FLUSH);
  656. }
  657. /* set up slots and formats for analog OUT */
  658. static int snd_atiixp_playback_prepare(snd_pcm_substream_t *substream)
  659. {
  660. atiixp_t *chip = snd_pcm_substream_chip(substream);
  661. unsigned int data;
  662. spin_lock_irq(&chip->reg_lock);
  663. /* set output threshold */
  664. data = atiixp_read(chip, MODEM_OUT_FIFO);
  665. data &= ~ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK;
  666. data |= 0x04 << ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT;
  667. atiixp_write(chip, MODEM_OUT_FIFO, data);
  668. spin_unlock_irq(&chip->reg_lock);
  669. return 0;
  670. }
  671. /* set up slots and formats for analog IN */
  672. static int snd_atiixp_capture_prepare(snd_pcm_substream_t *substream)
  673. {
  674. return 0;
  675. }
  676. /*
  677. * hw_params - allocate the buffer and set up buffer descriptors
  678. */
  679. static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
  680. snd_pcm_hw_params_t *hw_params)
  681. {
  682. atiixp_t *chip = snd_pcm_substream_chip(substream);
  683. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  684. int err;
  685. int i;
  686. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  687. if (err < 0)
  688. return err;
  689. dma->buf_addr = substream->runtime->dma_addr;
  690. dma->buf_bytes = params_buffer_bytes(hw_params);
  691. err = atiixp_build_dma_packets(chip, dma, substream,
  692. params_periods(hw_params),
  693. params_period_bytes(hw_params));
  694. if (err < 0)
  695. return err;
  696. /* set up modem rate */
  697. for (i = 0; i < NUM_ATI_CODECS; i++) {
  698. if (! chip->ac97[i])
  699. continue;
  700. snd_ac97_write(chip->ac97[i], AC97_LINE1_RATE, params_rate(hw_params));
  701. snd_ac97_write(chip->ac97[i], AC97_LINE1_LEVEL, 0);
  702. }
  703. return err;
  704. }
  705. static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
  706. {
  707. atiixp_t *chip = snd_pcm_substream_chip(substream);
  708. atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
  709. atiixp_clear_dma_packets(chip, dma, substream);
  710. snd_pcm_lib_free_pages(substream);
  711. return 0;
  712. }
  713. /*
  714. * pcm hardware definition, identical for all DMA types
  715. */
  716. static snd_pcm_hardware_t snd_atiixp_pcm_hw =
  717. {
  718. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  719. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  720. SNDRV_PCM_INFO_MMAP_VALID),
  721. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  722. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
  723. .rate_min = 8000,
  724. .rate_max = 16000,
  725. .channels_min = 2,
  726. .channels_max = 2,
  727. .buffer_bytes_max = 256 * 1024,
  728. .period_bytes_min = 32,
  729. .period_bytes_max = 128 * 1024,
  730. .periods_min = 2,
  731. .periods_max = ATI_MAX_DESCRIPTORS,
  732. };
  733. static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
  734. {
  735. atiixp_t *chip = snd_pcm_substream_chip(substream);
  736. snd_pcm_runtime_t *runtime = substream->runtime;
  737. int err;
  738. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  739. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  740. .count = ARRAY_SIZE(rates),
  741. .list = rates,
  742. .mask = 0,
  743. };
  744. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  745. if (dma->opened)
  746. return -EBUSY;
  747. dma->substream = substream;
  748. runtime->hw = snd_atiixp_pcm_hw;
  749. dma->ac97_pcm_type = pcm_type;
  750. if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
  751. return err;
  752. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  753. return err;
  754. runtime->private_data = dma;
  755. /* enable DMA bits */
  756. spin_lock_irq(&chip->reg_lock);
  757. dma->ops->enable_dma(chip, 1);
  758. spin_unlock_irq(&chip->reg_lock);
  759. dma->opened = 1;
  760. return 0;
  761. }
  762. static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
  763. {
  764. atiixp_t *chip = snd_pcm_substream_chip(substream);
  765. /* disable DMA bits */
  766. snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
  767. spin_lock_irq(&chip->reg_lock);
  768. dma->ops->enable_dma(chip, 0);
  769. spin_unlock_irq(&chip->reg_lock);
  770. dma->substream = NULL;
  771. dma->opened = 0;
  772. return 0;
  773. }
  774. /*
  775. */
  776. static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
  777. {
  778. atiixp_t *chip = snd_pcm_substream_chip(substream);
  779. int err;
  780. down(&chip->open_mutex);
  781. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  782. up(&chip->open_mutex);
  783. if (err < 0)
  784. return err;
  785. return 0;
  786. }
  787. static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
  788. {
  789. atiixp_t *chip = snd_pcm_substream_chip(substream);
  790. int err;
  791. down(&chip->open_mutex);
  792. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  793. up(&chip->open_mutex);
  794. return err;
  795. }
  796. static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
  797. {
  798. atiixp_t *chip = snd_pcm_substream_chip(substream);
  799. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  800. }
  801. static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
  802. {
  803. atiixp_t *chip = snd_pcm_substream_chip(substream);
  804. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  805. }
  806. /* AC97 playback */
  807. static snd_pcm_ops_t snd_atiixp_playback_ops = {
  808. .open = snd_atiixp_playback_open,
  809. .close = snd_atiixp_playback_close,
  810. .ioctl = snd_pcm_lib_ioctl,
  811. .hw_params = snd_atiixp_pcm_hw_params,
  812. .hw_free = snd_atiixp_pcm_hw_free,
  813. .prepare = snd_atiixp_playback_prepare,
  814. .trigger = snd_atiixp_pcm_trigger,
  815. .pointer = snd_atiixp_pcm_pointer,
  816. };
  817. /* AC97 capture */
  818. static snd_pcm_ops_t snd_atiixp_capture_ops = {
  819. .open = snd_atiixp_capture_open,
  820. .close = snd_atiixp_capture_close,
  821. .ioctl = snd_pcm_lib_ioctl,
  822. .hw_params = snd_atiixp_pcm_hw_params,
  823. .hw_free = snd_atiixp_pcm_hw_free,
  824. .prepare = snd_atiixp_capture_prepare,
  825. .trigger = snd_atiixp_pcm_trigger,
  826. .pointer = snd_atiixp_pcm_pointer,
  827. };
  828. static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
  829. .type = ATI_DMA_PLAYBACK,
  830. .llp_offset = ATI_REG_MODEM_OUT_DMA1_LINKPTR,
  831. .dt_cur = ATI_REG_MODEM_OUT_DMA1_DT_CUR,
  832. .enable_dma = atiixp_out_enable_dma,
  833. .enable_transfer = atiixp_out_enable_transfer,
  834. .flush_dma = atiixp_out_flush_dma,
  835. };
  836. static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
  837. .type = ATI_DMA_CAPTURE,
  838. .llp_offset = ATI_REG_MODEM_IN_DMA_LINKPTR,
  839. .dt_cur = ATI_REG_MODEM_IN_DMA_DT_CUR,
  840. .enable_dma = atiixp_in_enable_dma,
  841. .enable_transfer = atiixp_in_enable_transfer,
  842. .flush_dma = atiixp_in_flush_dma,
  843. };
  844. static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
  845. {
  846. snd_pcm_t *pcm;
  847. int err;
  848. /* initialize constants */
  849. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  850. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  851. /* PCM #0: analog I/O */
  852. err = snd_pcm_new(chip->card, "ATI IXP MC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  853. if (err < 0)
  854. return err;
  855. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  856. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  857. pcm->private_data = chip;
  858. strcpy(pcm->name, "ATI IXP MC97");
  859. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  860. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  861. snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
  862. return 0;
  863. }
  864. /*
  865. * interrupt handler
  866. */
  867. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  868. {
  869. atiixp_t *chip = dev_id;
  870. unsigned int status;
  871. status = atiixp_read(chip, ISR);
  872. if (! status)
  873. return IRQ_NONE;
  874. /* process audio DMA */
  875. if (status & ATI_REG_ISR_MODEM_OUT1_XRUN)
  876. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  877. else if (status & ATI_REG_ISR_MODEM_OUT1_STATUS)
  878. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  879. if (status & ATI_REG_ISR_MODEM_IN_XRUN)
  880. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  881. else if (status & ATI_REG_ISR_MODEM_IN_STATUS)
  882. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  883. /* for codec detection */
  884. if (status & CODEC_CHECK_BITS) {
  885. unsigned int detected;
  886. detected = status & CODEC_CHECK_BITS;
  887. spin_lock(&chip->reg_lock);
  888. chip->codec_not_ready_bits |= detected;
  889. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  890. spin_unlock(&chip->reg_lock);
  891. }
  892. /* ack */
  893. atiixp_write(chip, ISR, status);
  894. return IRQ_HANDLED;
  895. }
  896. /*
  897. * ac97 mixer section
  898. */
  899. static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock)
  900. {
  901. ac97_bus_t *pbus;
  902. ac97_template_t ac97;
  903. int i, err;
  904. int codec_count;
  905. static ac97_bus_ops_t ops = {
  906. .write = snd_atiixp_ac97_write,
  907. .read = snd_atiixp_ac97_read,
  908. };
  909. static unsigned int codec_skip[NUM_ATI_CODECS] = {
  910. ATI_REG_ISR_CODEC0_NOT_READY,
  911. ATI_REG_ISR_CODEC1_NOT_READY,
  912. ATI_REG_ISR_CODEC2_NOT_READY,
  913. };
  914. if (snd_atiixp_codec_detect(chip) < 0)
  915. return -ENXIO;
  916. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  917. return err;
  918. pbus->clock = clock;
  919. pbus->shared_type = AC97_SHARED_TYPE_ATIIXP; /* shared with audio driver */
  920. chip->ac97_bus = pbus;
  921. codec_count = 0;
  922. for (i = 0; i < NUM_ATI_CODECS; i++) {
  923. if (chip->codec_not_ready_bits & codec_skip[i])
  924. continue;
  925. memset(&ac97, 0, sizeof(ac97));
  926. ac97.private_data = chip;
  927. ac97.pci = chip->pci;
  928. ac97.num = i;
  929. ac97.scaps = AC97_SCAP_SKIP_AUDIO;
  930. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  931. chip->ac97[i] = NULL; /* to be sure */
  932. snd_printdd("atiixp: codec %d not available for modem\n", i);
  933. continue;
  934. }
  935. codec_count++;
  936. }
  937. if (! codec_count) {
  938. snd_printk(KERN_ERR "atiixp: no codec available\n");
  939. return -ENODEV;
  940. }
  941. /* snd_ac97_tune_hardware(chip->ac97, ac97_quirks); */
  942. return 0;
  943. }
  944. #ifdef CONFIG_PM
  945. /*
  946. * power management
  947. */
  948. static int snd_atiixp_suspend(snd_card_t *card, pm_message_t state)
  949. {
  950. atiixp_t *chip = card->pm_private_data;
  951. int i;
  952. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  953. if (chip->pcmdevs[i])
  954. snd_pcm_suspend_all(chip->pcmdevs[i]);
  955. for (i = 0; i < NUM_ATI_CODECS; i++)
  956. if (chip->ac97[i])
  957. snd_ac97_suspend(chip->ac97[i]);
  958. snd_atiixp_aclink_down(chip);
  959. snd_atiixp_chip_stop(chip);
  960. pci_set_power_state(chip->pci, PCI_D3hot);
  961. pci_disable_device(chip->pci);
  962. return 0;
  963. }
  964. static int snd_atiixp_resume(snd_card_t *card)
  965. {
  966. atiixp_t *chip = card->pm_private_data;
  967. int i;
  968. pci_enable_device(chip->pci);
  969. pci_set_power_state(chip->pci, PCI_D0);
  970. pci_set_master(chip->pci);
  971. snd_atiixp_aclink_reset(chip);
  972. snd_atiixp_chip_start(chip);
  973. for (i = 0; i < NUM_ATI_CODECS; i++)
  974. if (chip->ac97[i])
  975. snd_ac97_resume(chip->ac97[i]);
  976. return 0;
  977. }
  978. #endif /* CONFIG_PM */
  979. /*
  980. * proc interface for register dump
  981. */
  982. static void snd_atiixp_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  983. {
  984. atiixp_t *chip = entry->private_data;
  985. int i;
  986. for (i = 0; i < 256; i += 4)
  987. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  988. }
  989. static void __devinit snd_atiixp_proc_init(atiixp_t *chip)
  990. {
  991. snd_info_entry_t *entry;
  992. if (! snd_card_proc_new(chip->card, "atiixp", &entry))
  993. snd_info_set_text_ops(entry, chip, 1024, snd_atiixp_proc_read);
  994. }
  995. /*
  996. * destructor
  997. */
  998. static int snd_atiixp_free(atiixp_t *chip)
  999. {
  1000. if (chip->irq < 0)
  1001. goto __hw_end;
  1002. snd_atiixp_chip_stop(chip);
  1003. synchronize_irq(chip->irq);
  1004. __hw_end:
  1005. if (chip->irq >= 0)
  1006. free_irq(chip->irq, (void *)chip);
  1007. if (chip->remap_addr)
  1008. iounmap(chip->remap_addr);
  1009. pci_release_regions(chip->pci);
  1010. pci_disable_device(chip->pci);
  1011. kfree(chip);
  1012. return 0;
  1013. }
  1014. static int snd_atiixp_dev_free(snd_device_t *device)
  1015. {
  1016. atiixp_t *chip = device->device_data;
  1017. return snd_atiixp_free(chip);
  1018. }
  1019. /*
  1020. * constructor for chip instance
  1021. */
  1022. static int __devinit snd_atiixp_create(snd_card_t *card,
  1023. struct pci_dev *pci,
  1024. atiixp_t **r_chip)
  1025. {
  1026. static snd_device_ops_t ops = {
  1027. .dev_free = snd_atiixp_dev_free,
  1028. };
  1029. atiixp_t *chip;
  1030. int err;
  1031. if ((err = pci_enable_device(pci)) < 0)
  1032. return err;
  1033. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1034. if (chip == NULL) {
  1035. pci_disable_device(pci);
  1036. return -ENOMEM;
  1037. }
  1038. spin_lock_init(&chip->reg_lock);
  1039. init_MUTEX(&chip->open_mutex);
  1040. chip->card = card;
  1041. chip->pci = pci;
  1042. chip->irq = -1;
  1043. if ((err = pci_request_regions(pci, "ATI IXP MC97")) < 0) {
  1044. kfree(chip);
  1045. pci_disable_device(pci);
  1046. return err;
  1047. }
  1048. chip->addr = pci_resource_start(pci, 0);
  1049. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
  1050. if (chip->remap_addr == NULL) {
  1051. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1052. snd_atiixp_free(chip);
  1053. return -EIO;
  1054. }
  1055. if (request_irq(pci->irq, snd_atiixp_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
  1056. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1057. snd_atiixp_free(chip);
  1058. return -EBUSY;
  1059. }
  1060. chip->irq = pci->irq;
  1061. pci_set_master(pci);
  1062. synchronize_irq(chip->irq);
  1063. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1064. snd_atiixp_free(chip);
  1065. return err;
  1066. }
  1067. snd_card_set_dev(card, &pci->dev);
  1068. *r_chip = chip;
  1069. return 0;
  1070. }
  1071. static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  1072. const struct pci_device_id *pci_id)
  1073. {
  1074. static int dev;
  1075. snd_card_t *card;
  1076. atiixp_t *chip;
  1077. unsigned char revision;
  1078. int err;
  1079. if (dev >= SNDRV_CARDS)
  1080. return -ENODEV;
  1081. if (!enable[dev]) {
  1082. dev++;
  1083. return -ENOENT;
  1084. }
  1085. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1086. if (card == NULL)
  1087. return -ENOMEM;
  1088. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  1089. strcpy(card->driver, "ATIIXP-MODEM");
  1090. strcpy(card->shortname, "ATI IXP Modem");
  1091. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1092. goto __error;
  1093. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1094. goto __error;
  1095. if ((err = snd_atiixp_mixer_new(chip, ac97_clock[dev])) < 0)
  1096. goto __error;
  1097. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1098. goto __error;
  1099. snd_atiixp_proc_init(chip);
  1100. snd_atiixp_chip_start(chip);
  1101. sprintf(card->longname, "%s rev %x at 0x%lx, irq %i",
  1102. card->shortname, revision, chip->addr, chip->irq);
  1103. snd_card_set_pm_callback(card, snd_atiixp_suspend, snd_atiixp_resume, chip);
  1104. if ((err = snd_card_register(card)) < 0)
  1105. goto __error;
  1106. pci_set_drvdata(pci, card);
  1107. dev++;
  1108. return 0;
  1109. __error:
  1110. snd_card_free(card);
  1111. return err;
  1112. }
  1113. static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  1114. {
  1115. snd_card_free(pci_get_drvdata(pci));
  1116. pci_set_drvdata(pci, NULL);
  1117. }
  1118. static struct pci_driver driver = {
  1119. .name = "ATI IXP MC97 controller",
  1120. .id_table = snd_atiixp_ids,
  1121. .probe = snd_atiixp_probe,
  1122. .remove = __devexit_p(snd_atiixp_remove),
  1123. SND_PCI_PM_CALLBACKS
  1124. };
  1125. static int __init alsa_card_atiixp_init(void)
  1126. {
  1127. return pci_module_init(&driver);
  1128. }
  1129. static void __exit alsa_card_atiixp_exit(void)
  1130. {
  1131. pci_unregister_driver(&driver);
  1132. }
  1133. module_init(alsa_card_atiixp_init)
  1134. module_exit(alsa_card_atiixp_exit)