atiixp_modem.c 35 KB

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