atiixp_modem.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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 <asm/io.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/pci.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/mutex.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/info.h>
  33. #include <sound/ac97_codec.h>
  34. #include <sound/initval.h>
  35. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  36. MODULE_DESCRIPTION("ATI IXP MC97 controller");
  37. MODULE_LICENSE("GPL");
  38. MODULE_SUPPORTED_DEVICE("{{ATI,IXP150/200/250}}");
  39. static int index = -2; /* Exclude the first card */
  40. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  41. static int ac97_clock = 48000;
  42. module_param(index, int, 0444);
  43. MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
  44. module_param(id, charp, 0444);
  45. MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
  46. module_param(ac97_clock, int, 0444);
  47. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  48. /* just for backward compatibility */
  49. static bool enable;
  50. module_param(enable, bool, 0444);
  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. struct atiixp_modem;
  158. /*
  159. * DMA packate descriptor
  160. */
  161. struct atiixp_dma_desc {
  162. u32 addr; /* DMA buffer address */
  163. u16 status; /* status bits */
  164. u16 size; /* size of the packet in dwords */
  165. u32 next; /* address of the next packet descriptor */
  166. };
  167. /*
  168. * stream enum
  169. */
  170. enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, NUM_ATI_DMAS }; /* DMAs */
  171. enum { ATI_PCM_OUT, ATI_PCM_IN, NUM_ATI_PCMS }; /* AC97 pcm slots */
  172. enum { ATI_PCMDEV_ANALOG, NUM_ATI_PCMDEVS }; /* pcm devices */
  173. #define NUM_ATI_CODECS 3
  174. /*
  175. * constants and callbacks for each DMA type
  176. */
  177. struct atiixp_dma_ops {
  178. int type; /* ATI_DMA_XXX */
  179. unsigned int llp_offset; /* LINKPTR offset */
  180. unsigned int dt_cur; /* DT_CUR offset */
  181. /* called from open callback */
  182. void (*enable_dma)(struct atiixp_modem *chip, int on);
  183. /* called from trigger (START/STOP) */
  184. void (*enable_transfer)(struct atiixp_modem *chip, int on);
  185. /* called from trigger (STOP only) */
  186. void (*flush_dma)(struct atiixp_modem *chip);
  187. };
  188. /*
  189. * DMA stream
  190. */
  191. struct atiixp_dma {
  192. const struct atiixp_dma_ops *ops;
  193. struct snd_dma_buffer desc_buf;
  194. struct snd_pcm_substream *substream; /* assigned PCM substream */
  195. unsigned int buf_addr, buf_bytes; /* DMA buffer address, bytes */
  196. unsigned int period_bytes, periods;
  197. int opened;
  198. int running;
  199. int pcm_open_flag;
  200. int ac97_pcm_type; /* index # of ac97_pcm to access, -1 = not used */
  201. };
  202. /*
  203. * ATI IXP chip
  204. */
  205. struct atiixp_modem {
  206. struct snd_card *card;
  207. struct pci_dev *pci;
  208. struct resource *res; /* memory i/o */
  209. unsigned long addr;
  210. void __iomem *remap_addr;
  211. int irq;
  212. struct snd_ac97_bus *ac97_bus;
  213. struct snd_ac97 *ac97[NUM_ATI_CODECS];
  214. spinlock_t reg_lock;
  215. struct atiixp_dma dmas[NUM_ATI_DMAS];
  216. struct ac97_pcm *pcms[NUM_ATI_PCMS];
  217. struct snd_pcm *pcmdevs[NUM_ATI_PCMDEVS];
  218. int max_channels; /* max. channels for PCM out */
  219. unsigned int codec_not_ready_bits; /* for codec detection */
  220. int spdif_over_aclink; /* passed from the module option */
  221. struct mutex open_mutex; /* playback open mutex */
  222. };
  223. /*
  224. */
  225. static DEFINE_PCI_DEVICE_TABLE(snd_atiixp_ids) = {
  226. { PCI_VDEVICE(ATI, 0x434d), 0 }, /* SB200 */
  227. { PCI_VDEVICE(ATI, 0x4378), 0 }, /* SB400 */
  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(struct atiixp_modem *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. /*
  261. * handling DMA packets
  262. *
  263. * we allocate a linear buffer for the DMA, and split it to each packet.
  264. * in a future version, a scatter-gather buffer should be implemented.
  265. */
  266. #define ATI_DESC_LIST_SIZE \
  267. PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(struct atiixp_dma_desc))
  268. /*
  269. * build packets ring for the given buffer size.
  270. *
  271. * IXP handles the buffer descriptors, which are connected as a linked
  272. * list. although we can change the list dynamically, in this version,
  273. * a static RING of buffer descriptors is used.
  274. *
  275. * the ring is built in this function, and is set up to the hardware.
  276. */
  277. static int atiixp_build_dma_packets(struct atiixp_modem *chip,
  278. struct atiixp_dma *dma,
  279. struct snd_pcm_substream *substream,
  280. unsigned int periods,
  281. unsigned int period_bytes)
  282. {
  283. unsigned int i;
  284. u32 addr, desc_addr;
  285. unsigned long flags;
  286. if (periods > ATI_MAX_DESCRIPTORS)
  287. return -ENOMEM;
  288. if (dma->desc_buf.area == NULL) {
  289. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  290. ATI_DESC_LIST_SIZE, &dma->desc_buf) < 0)
  291. return -ENOMEM;
  292. dma->period_bytes = dma->periods = 0; /* clear */
  293. }
  294. if (dma->periods == periods && dma->period_bytes == period_bytes)
  295. return 0;
  296. /* reset DMA before changing the descriptor table */
  297. spin_lock_irqsave(&chip->reg_lock, flags);
  298. writel(0, chip->remap_addr + dma->ops->llp_offset);
  299. dma->ops->enable_dma(chip, 0);
  300. dma->ops->enable_dma(chip, 1);
  301. spin_unlock_irqrestore(&chip->reg_lock, flags);
  302. /* fill the entries */
  303. addr = (u32)substream->runtime->dma_addr;
  304. desc_addr = (u32)dma->desc_buf.addr;
  305. for (i = 0; i < periods; i++) {
  306. struct atiixp_dma_desc *desc;
  307. desc = &((struct atiixp_dma_desc *)dma->desc_buf.area)[i];
  308. desc->addr = cpu_to_le32(addr);
  309. desc->status = 0;
  310. desc->size = period_bytes >> 2; /* in dwords */
  311. desc_addr += sizeof(struct atiixp_dma_desc);
  312. if (i == periods - 1)
  313. desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
  314. else
  315. desc->next = cpu_to_le32(desc_addr);
  316. addr += period_bytes;
  317. }
  318. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  319. chip->remap_addr + dma->ops->llp_offset);
  320. dma->period_bytes = period_bytes;
  321. dma->periods = periods;
  322. return 0;
  323. }
  324. /*
  325. * remove the ring buffer and release it if assigned
  326. */
  327. static void atiixp_clear_dma_packets(struct atiixp_modem *chip,
  328. struct atiixp_dma *dma,
  329. struct snd_pcm_substream *substream)
  330. {
  331. if (dma->desc_buf.area) {
  332. writel(0, chip->remap_addr + dma->ops->llp_offset);
  333. snd_dma_free_pages(&dma->desc_buf);
  334. dma->desc_buf.area = NULL;
  335. }
  336. }
  337. /*
  338. * AC97 interface
  339. */
  340. static int snd_atiixp_acquire_codec(struct atiixp_modem *chip)
  341. {
  342. int timeout = 1000;
  343. while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
  344. if (! timeout--) {
  345. snd_printk(KERN_WARNING "atiixp-modem: codec acquire timeout\n");
  346. return -EBUSY;
  347. }
  348. udelay(1);
  349. }
  350. return 0;
  351. }
  352. static unsigned short snd_atiixp_codec_read(struct atiixp_modem *chip,
  353. unsigned short codec,
  354. 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-modem: codec read timeout (reg %x)\n", reg);
  377. return 0xffff;
  378. }
  379. static void snd_atiixp_codec_write(struct atiixp_modem *chip,
  380. unsigned short codec,
  381. unsigned short reg, unsigned short val)
  382. {
  383. unsigned int data;
  384. if (snd_atiixp_acquire_codec(chip) < 0)
  385. return;
  386. data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
  387. ((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  388. ATI_REG_PHYS_OUT_ADDR_EN | codec;
  389. atiixp_write(chip, PHYS_OUT_ADDR, data);
  390. }
  391. static unsigned short snd_atiixp_ac97_read(struct snd_ac97 *ac97,
  392. unsigned short reg)
  393. {
  394. struct atiixp_modem *chip = ac97->private_data;
  395. return snd_atiixp_codec_read(chip, ac97->num, reg);
  396. }
  397. static void snd_atiixp_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  398. unsigned short val)
  399. {
  400. struct atiixp_modem *chip = ac97->private_data;
  401. if (reg == AC97_GPIO_STATUS) {
  402. atiixp_write(chip, MODEM_OUT_GPIO,
  403. (val << ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT) | ATI_REG_MODEM_OUT_GPIO_EN);
  404. return;
  405. }
  406. snd_atiixp_codec_write(chip, ac97->num, reg, val);
  407. }
  408. /*
  409. * reset AC link
  410. */
  411. static int snd_atiixp_aclink_reset(struct atiixp_modem *chip)
  412. {
  413. int timeout;
  414. /* reset powerdoewn */
  415. if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
  416. udelay(10);
  417. /* perform a software reset */
  418. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
  419. atiixp_read(chip, CMD);
  420. udelay(10);
  421. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
  422. timeout = 10;
  423. while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
  424. /* do a hard reset */
  425. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  426. ATI_REG_CMD_AC_SYNC);
  427. atiixp_read(chip, CMD);
  428. msleep(1);
  429. atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
  430. if (!--timeout) {
  431. snd_printk(KERN_ERR "atiixp-modem: codec reset timeout\n");
  432. break;
  433. }
  434. }
  435. /* deassert RESET and assert SYNC to make sure */
  436. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  437. ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
  438. return 0;
  439. }
  440. #ifdef CONFIG_PM
  441. static int snd_atiixp_aclink_down(struct atiixp_modem *chip)
  442. {
  443. // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
  444. // return -EBUSY;
  445. atiixp_update(chip, CMD,
  446. ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
  447. ATI_REG_CMD_POWERDOWN);
  448. return 0;
  449. }
  450. #endif
  451. /*
  452. * auto-detection of codecs
  453. *
  454. * the IXP chip can generate interrupts for the non-existing codecs.
  455. * NEW_FRAME interrupt is used to make sure that the interrupt is generated
  456. * even if all three codecs are connected.
  457. */
  458. #define ALL_CODEC_NOT_READY \
  459. (ATI_REG_ISR_CODEC0_NOT_READY |\
  460. ATI_REG_ISR_CODEC1_NOT_READY |\
  461. ATI_REG_ISR_CODEC2_NOT_READY)
  462. #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
  463. static int snd_atiixp_codec_detect(struct atiixp_modem *chip)
  464. {
  465. int timeout;
  466. chip->codec_not_ready_bits = 0;
  467. atiixp_write(chip, IER, CODEC_CHECK_BITS);
  468. /* wait for the interrupts */
  469. timeout = 50;
  470. while (timeout-- > 0) {
  471. msleep(1);
  472. if (chip->codec_not_ready_bits)
  473. break;
  474. }
  475. atiixp_write(chip, IER, 0); /* disable irqs */
  476. if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
  477. snd_printk(KERN_ERR "atiixp-modem: no codec detected!\n");
  478. return -ENXIO;
  479. }
  480. return 0;
  481. }
  482. /*
  483. * enable DMA and irqs
  484. */
  485. static int snd_atiixp_chip_start(struct atiixp_modem *chip)
  486. {
  487. unsigned int reg;
  488. /* set up spdif, enable burst mode */
  489. reg = atiixp_read(chip, CMD);
  490. reg |= ATI_REG_CMD_BURST_EN;
  491. if(!(reg & ATI_REG_CMD_MODEM_PRESENT))
  492. reg |= ATI_REG_CMD_MODEM_PRESENT;
  493. atiixp_write(chip, CMD, reg);
  494. /* clear all interrupt source */
  495. atiixp_write(chip, ISR, 0xffffffff);
  496. /* enable irqs */
  497. atiixp_write(chip, IER,
  498. ATI_REG_IER_MODEM_STATUS_EN |
  499. ATI_REG_IER_MODEM_IN_XRUN_EN |
  500. ATI_REG_IER_MODEM_OUT1_XRUN_EN);
  501. return 0;
  502. }
  503. /*
  504. * disable DMA and IRQs
  505. */
  506. static int snd_atiixp_chip_stop(struct atiixp_modem *chip)
  507. {
  508. /* clear interrupt source */
  509. atiixp_write(chip, ISR, atiixp_read(chip, ISR));
  510. /* disable irqs */
  511. atiixp_write(chip, IER, 0);
  512. return 0;
  513. }
  514. /*
  515. * PCM section
  516. */
  517. /*
  518. * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
  519. * position. when SG-buffer is implemented, the offset must be calculated
  520. * correctly...
  521. */
  522. static snd_pcm_uframes_t snd_atiixp_pcm_pointer(struct snd_pcm_substream *substream)
  523. {
  524. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  525. struct snd_pcm_runtime *runtime = substream->runtime;
  526. struct atiixp_dma *dma = runtime->private_data;
  527. unsigned int curptr;
  528. int timeout = 1000;
  529. while (timeout--) {
  530. curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  531. if (curptr < dma->buf_addr)
  532. continue;
  533. curptr -= dma->buf_addr;
  534. if (curptr >= dma->buf_bytes)
  535. continue;
  536. return bytes_to_frames(runtime, curptr);
  537. }
  538. snd_printd("atiixp-modem: invalid DMA pointer read 0x%x (buf=%x)\n",
  539. readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
  540. return 0;
  541. }
  542. /*
  543. * XRUN detected, and stop the PCM substream
  544. */
  545. static void snd_atiixp_xrun_dma(struct atiixp_modem *chip,
  546. struct atiixp_dma *dma)
  547. {
  548. if (! dma->substream || ! dma->running)
  549. return;
  550. snd_printdd("atiixp-modem: XRUN detected (DMA %d)\n", dma->ops->type);
  551. snd_pcm_stream_lock(dma->substream);
  552. snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
  553. snd_pcm_stream_unlock(dma->substream);
  554. }
  555. /*
  556. * the period ack. update the substream.
  557. */
  558. static void snd_atiixp_update_dma(struct atiixp_modem *chip,
  559. struct atiixp_dma *dma)
  560. {
  561. if (! dma->substream || ! dma->running)
  562. return;
  563. snd_pcm_period_elapsed(dma->substream);
  564. }
  565. /* set BUS_BUSY interrupt bit if any DMA is running */
  566. /* call with spinlock held */
  567. static void snd_atiixp_check_bus_busy(struct atiixp_modem *chip)
  568. {
  569. unsigned int bus_busy;
  570. if (atiixp_read(chip, CMD) & (ATI_REG_CMD_MODEM_SEND1_EN |
  571. ATI_REG_CMD_MODEM_RECEIVE_EN))
  572. bus_busy = ATI_REG_IER_MODEM_SET_BUS_BUSY;
  573. else
  574. bus_busy = 0;
  575. atiixp_update(chip, IER, ATI_REG_IER_MODEM_SET_BUS_BUSY, bus_busy);
  576. }
  577. /* common trigger callback
  578. * calling the lowlevel callbacks in it
  579. */
  580. static int snd_atiixp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  581. {
  582. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  583. struct atiixp_dma *dma = substream->runtime->private_data;
  584. int err = 0;
  585. if (snd_BUG_ON(!dma->ops->enable_transfer ||
  586. !dma->ops->flush_dma))
  587. return -EINVAL;
  588. spin_lock(&chip->reg_lock);
  589. switch(cmd) {
  590. case SNDRV_PCM_TRIGGER_START:
  591. dma->ops->enable_transfer(chip, 1);
  592. dma->running = 1;
  593. break;
  594. case SNDRV_PCM_TRIGGER_STOP:
  595. dma->ops->enable_transfer(chip, 0);
  596. dma->running = 0;
  597. break;
  598. default:
  599. err = -EINVAL;
  600. break;
  601. }
  602. if (! err) {
  603. snd_atiixp_check_bus_busy(chip);
  604. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  605. dma->ops->flush_dma(chip);
  606. snd_atiixp_check_bus_busy(chip);
  607. }
  608. }
  609. spin_unlock(&chip->reg_lock);
  610. return err;
  611. }
  612. /*
  613. * lowlevel callbacks for each DMA type
  614. *
  615. * every callback is supposed to be called in chip->reg_lock spinlock
  616. */
  617. /* flush FIFO of analog OUT DMA */
  618. static void atiixp_out_flush_dma(struct atiixp_modem *chip)
  619. {
  620. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_OUT1_FLUSH);
  621. }
  622. /* enable/disable analog OUT DMA */
  623. static void atiixp_out_enable_dma(struct atiixp_modem *chip, int on)
  624. {
  625. unsigned int data;
  626. data = atiixp_read(chip, CMD);
  627. if (on) {
  628. if (data & ATI_REG_CMD_MODEM_OUT_DMA1_EN)
  629. return;
  630. atiixp_out_flush_dma(chip);
  631. data |= ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  632. } else
  633. data &= ~ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  634. atiixp_write(chip, CMD, data);
  635. }
  636. /* start/stop transfer over OUT DMA */
  637. static void atiixp_out_enable_transfer(struct atiixp_modem *chip, int on)
  638. {
  639. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_SEND1_EN,
  640. on ? ATI_REG_CMD_MODEM_SEND1_EN : 0);
  641. }
  642. /* enable/disable analog IN DMA */
  643. static void atiixp_in_enable_dma(struct atiixp_modem *chip, int on)
  644. {
  645. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_IN_DMA_EN,
  646. on ? ATI_REG_CMD_MODEM_IN_DMA_EN : 0);
  647. }
  648. /* start/stop analog IN DMA */
  649. static void atiixp_in_enable_transfer(struct atiixp_modem *chip, int on)
  650. {
  651. if (on) {
  652. unsigned int data = atiixp_read(chip, CMD);
  653. if (! (data & ATI_REG_CMD_MODEM_RECEIVE_EN)) {
  654. data |= ATI_REG_CMD_MODEM_RECEIVE_EN;
  655. atiixp_write(chip, CMD, data);
  656. }
  657. } else
  658. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_RECEIVE_EN, 0);
  659. }
  660. /* flush FIFO of analog IN DMA */
  661. static void atiixp_in_flush_dma(struct atiixp_modem *chip)
  662. {
  663. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_IN_FLUSH);
  664. }
  665. /* set up slots and formats for analog OUT */
  666. static int snd_atiixp_playback_prepare(struct snd_pcm_substream *substream)
  667. {
  668. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  669. unsigned int data;
  670. spin_lock_irq(&chip->reg_lock);
  671. /* set output threshold */
  672. data = atiixp_read(chip, MODEM_OUT_FIFO);
  673. data &= ~ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK;
  674. data |= 0x04 << ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT;
  675. atiixp_write(chip, MODEM_OUT_FIFO, data);
  676. spin_unlock_irq(&chip->reg_lock);
  677. return 0;
  678. }
  679. /* set up slots and formats for analog IN */
  680. static int snd_atiixp_capture_prepare(struct snd_pcm_substream *substream)
  681. {
  682. return 0;
  683. }
  684. /*
  685. * hw_params - allocate the buffer and set up buffer descriptors
  686. */
  687. static int snd_atiixp_pcm_hw_params(struct snd_pcm_substream *substream,
  688. struct snd_pcm_hw_params *hw_params)
  689. {
  690. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  691. struct atiixp_dma *dma = substream->runtime->private_data;
  692. int err;
  693. int i;
  694. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  695. if (err < 0)
  696. return err;
  697. dma->buf_addr = substream->runtime->dma_addr;
  698. dma->buf_bytes = params_buffer_bytes(hw_params);
  699. err = atiixp_build_dma_packets(chip, dma, substream,
  700. params_periods(hw_params),
  701. params_period_bytes(hw_params));
  702. if (err < 0)
  703. return err;
  704. /* set up modem rate */
  705. for (i = 0; i < NUM_ATI_CODECS; i++) {
  706. if (! chip->ac97[i])
  707. continue;
  708. snd_ac97_write(chip->ac97[i], AC97_LINE1_RATE, params_rate(hw_params));
  709. snd_ac97_write(chip->ac97[i], AC97_LINE1_LEVEL, 0);
  710. }
  711. return err;
  712. }
  713. static int snd_atiixp_pcm_hw_free(struct snd_pcm_substream *substream)
  714. {
  715. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  716. struct atiixp_dma *dma = substream->runtime->private_data;
  717. atiixp_clear_dma_packets(chip, dma, substream);
  718. snd_pcm_lib_free_pages(substream);
  719. return 0;
  720. }
  721. /*
  722. * pcm hardware definition, identical for all DMA types
  723. */
  724. static struct snd_pcm_hardware snd_atiixp_pcm_hw =
  725. {
  726. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  727. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  728. SNDRV_PCM_INFO_MMAP_VALID),
  729. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  730. .rates = (SNDRV_PCM_RATE_8000 |
  731. SNDRV_PCM_RATE_16000 |
  732. SNDRV_PCM_RATE_KNOT),
  733. .rate_min = 8000,
  734. .rate_max = 16000,
  735. .channels_min = 2,
  736. .channels_max = 2,
  737. .buffer_bytes_max = 256 * 1024,
  738. .period_bytes_min = 32,
  739. .period_bytes_max = 128 * 1024,
  740. .periods_min = 2,
  741. .periods_max = ATI_MAX_DESCRIPTORS,
  742. };
  743. static int snd_atiixp_pcm_open(struct snd_pcm_substream *substream,
  744. struct atiixp_dma *dma, int pcm_type)
  745. {
  746. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  747. struct snd_pcm_runtime *runtime = substream->runtime;
  748. int err;
  749. static unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  750. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  751. .count = ARRAY_SIZE(rates),
  752. .list = rates,
  753. .mask = 0,
  754. };
  755. if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
  756. return -EINVAL;
  757. if (dma->opened)
  758. return -EBUSY;
  759. dma->substream = substream;
  760. runtime->hw = snd_atiixp_pcm_hw;
  761. dma->ac97_pcm_type = pcm_type;
  762. if ((err = snd_pcm_hw_constraint_list(runtime, 0,
  763. SNDRV_PCM_HW_PARAM_RATE,
  764. &hw_constraints_rates)) < 0)
  765. return err;
  766. if ((err = snd_pcm_hw_constraint_integer(runtime,
  767. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  768. return err;
  769. runtime->private_data = dma;
  770. /* enable DMA bits */
  771. spin_lock_irq(&chip->reg_lock);
  772. dma->ops->enable_dma(chip, 1);
  773. spin_unlock_irq(&chip->reg_lock);
  774. dma->opened = 1;
  775. return 0;
  776. }
  777. static int snd_atiixp_pcm_close(struct snd_pcm_substream *substream,
  778. struct atiixp_dma *dma)
  779. {
  780. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  781. /* disable DMA bits */
  782. if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
  783. return -EINVAL;
  784. spin_lock_irq(&chip->reg_lock);
  785. dma->ops->enable_dma(chip, 0);
  786. spin_unlock_irq(&chip->reg_lock);
  787. dma->substream = NULL;
  788. dma->opened = 0;
  789. return 0;
  790. }
  791. /*
  792. */
  793. static int snd_atiixp_playback_open(struct snd_pcm_substream *substream)
  794. {
  795. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  796. int err;
  797. mutex_lock(&chip->open_mutex);
  798. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  799. mutex_unlock(&chip->open_mutex);
  800. if (err < 0)
  801. return err;
  802. return 0;
  803. }
  804. static int snd_atiixp_playback_close(struct snd_pcm_substream *substream)
  805. {
  806. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  807. int err;
  808. mutex_lock(&chip->open_mutex);
  809. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  810. mutex_unlock(&chip->open_mutex);
  811. return err;
  812. }
  813. static int snd_atiixp_capture_open(struct snd_pcm_substream *substream)
  814. {
  815. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  816. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  817. }
  818. static int snd_atiixp_capture_close(struct snd_pcm_substream *substream)
  819. {
  820. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  821. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  822. }
  823. /* AC97 playback */
  824. static struct snd_pcm_ops snd_atiixp_playback_ops = {
  825. .open = snd_atiixp_playback_open,
  826. .close = snd_atiixp_playback_close,
  827. .ioctl = snd_pcm_lib_ioctl,
  828. .hw_params = snd_atiixp_pcm_hw_params,
  829. .hw_free = snd_atiixp_pcm_hw_free,
  830. .prepare = snd_atiixp_playback_prepare,
  831. .trigger = snd_atiixp_pcm_trigger,
  832. .pointer = snd_atiixp_pcm_pointer,
  833. };
  834. /* AC97 capture */
  835. static struct snd_pcm_ops snd_atiixp_capture_ops = {
  836. .open = snd_atiixp_capture_open,
  837. .close = snd_atiixp_capture_close,
  838. .ioctl = snd_pcm_lib_ioctl,
  839. .hw_params = snd_atiixp_pcm_hw_params,
  840. .hw_free = snd_atiixp_pcm_hw_free,
  841. .prepare = snd_atiixp_capture_prepare,
  842. .trigger = snd_atiixp_pcm_trigger,
  843. .pointer = snd_atiixp_pcm_pointer,
  844. };
  845. static struct atiixp_dma_ops snd_atiixp_playback_dma_ops = {
  846. .type = ATI_DMA_PLAYBACK,
  847. .llp_offset = ATI_REG_MODEM_OUT_DMA1_LINKPTR,
  848. .dt_cur = ATI_REG_MODEM_OUT_DMA1_DT_CUR,
  849. .enable_dma = atiixp_out_enable_dma,
  850. .enable_transfer = atiixp_out_enable_transfer,
  851. .flush_dma = atiixp_out_flush_dma,
  852. };
  853. static struct atiixp_dma_ops snd_atiixp_capture_dma_ops = {
  854. .type = ATI_DMA_CAPTURE,
  855. .llp_offset = ATI_REG_MODEM_IN_DMA_LINKPTR,
  856. .dt_cur = ATI_REG_MODEM_IN_DMA_DT_CUR,
  857. .enable_dma = atiixp_in_enable_dma,
  858. .enable_transfer = atiixp_in_enable_transfer,
  859. .flush_dma = atiixp_in_flush_dma,
  860. };
  861. static int __devinit snd_atiixp_pcm_new(struct atiixp_modem *chip)
  862. {
  863. struct snd_pcm *pcm;
  864. int err;
  865. /* initialize constants */
  866. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  867. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  868. /* PCM #0: analog I/O */
  869. err = snd_pcm_new(chip->card, "ATI IXP MC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  870. if (err < 0)
  871. return err;
  872. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  873. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  874. pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
  875. pcm->private_data = chip;
  876. strcpy(pcm->name, "ATI IXP MC97");
  877. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  878. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  879. snd_dma_pci_data(chip->pci),
  880. 64*1024, 128*1024);
  881. return 0;
  882. }
  883. /*
  884. * interrupt handler
  885. */
  886. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id)
  887. {
  888. struct atiixp_modem *chip = dev_id;
  889. unsigned int status;
  890. status = atiixp_read(chip, ISR);
  891. if (! status)
  892. return IRQ_NONE;
  893. /* process audio DMA */
  894. if (status & ATI_REG_ISR_MODEM_OUT1_XRUN)
  895. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  896. else if (status & ATI_REG_ISR_MODEM_OUT1_STATUS)
  897. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  898. if (status & ATI_REG_ISR_MODEM_IN_XRUN)
  899. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  900. else if (status & ATI_REG_ISR_MODEM_IN_STATUS)
  901. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  902. /* for codec detection */
  903. if (status & CODEC_CHECK_BITS) {
  904. unsigned int detected;
  905. detected = status & CODEC_CHECK_BITS;
  906. spin_lock(&chip->reg_lock);
  907. chip->codec_not_ready_bits |= detected;
  908. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  909. spin_unlock(&chip->reg_lock);
  910. }
  911. /* ack */
  912. atiixp_write(chip, ISR, status);
  913. return IRQ_HANDLED;
  914. }
  915. /*
  916. * ac97 mixer section
  917. */
  918. static int __devinit snd_atiixp_mixer_new(struct atiixp_modem *chip, int clock)
  919. {
  920. struct snd_ac97_bus *pbus;
  921. struct snd_ac97_template ac97;
  922. int i, err;
  923. int codec_count;
  924. static struct snd_ac97_bus_ops ops = {
  925. .write = snd_atiixp_ac97_write,
  926. .read = snd_atiixp_ac97_read,
  927. };
  928. static unsigned int codec_skip[NUM_ATI_CODECS] = {
  929. ATI_REG_ISR_CODEC0_NOT_READY,
  930. ATI_REG_ISR_CODEC1_NOT_READY,
  931. ATI_REG_ISR_CODEC2_NOT_READY,
  932. };
  933. if (snd_atiixp_codec_detect(chip) < 0)
  934. return -ENXIO;
  935. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  936. return err;
  937. pbus->clock = clock;
  938. chip->ac97_bus = pbus;
  939. codec_count = 0;
  940. for (i = 0; i < NUM_ATI_CODECS; i++) {
  941. if (chip->codec_not_ready_bits & codec_skip[i])
  942. continue;
  943. memset(&ac97, 0, sizeof(ac97));
  944. ac97.private_data = chip;
  945. ac97.pci = chip->pci;
  946. ac97.num = i;
  947. ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
  948. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  949. chip->ac97[i] = NULL; /* to be sure */
  950. snd_printdd("atiixp-modem: codec %d not available for modem\n", i);
  951. continue;
  952. }
  953. codec_count++;
  954. }
  955. if (! codec_count) {
  956. snd_printk(KERN_ERR "atiixp-modem: no codec available\n");
  957. return -ENODEV;
  958. }
  959. /* snd_ac97_tune_hardware(chip->ac97, ac97_quirks); */
  960. return 0;
  961. }
  962. #ifdef CONFIG_PM
  963. /*
  964. * power management
  965. */
  966. static int snd_atiixp_suspend(struct pci_dev *pci, pm_message_t state)
  967. {
  968. struct snd_card *card = pci_get_drvdata(pci);
  969. struct atiixp_modem *chip = card->private_data;
  970. int i;
  971. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  972. for (i = 0; i < NUM_ATI_PCMDEVS; i++)
  973. snd_pcm_suspend_all(chip->pcmdevs[i]);
  974. for (i = 0; i < NUM_ATI_CODECS; i++)
  975. snd_ac97_suspend(chip->ac97[i]);
  976. snd_atiixp_aclink_down(chip);
  977. snd_atiixp_chip_stop(chip);
  978. pci_disable_device(pci);
  979. pci_save_state(pci);
  980. pci_set_power_state(pci, pci_choose_state(pci, state));
  981. return 0;
  982. }
  983. static int snd_atiixp_resume(struct pci_dev *pci)
  984. {
  985. struct snd_card *card = pci_get_drvdata(pci);
  986. struct atiixp_modem *chip = card->private_data;
  987. int i;
  988. pci_set_power_state(pci, PCI_D0);
  989. pci_restore_state(pci);
  990. if (pci_enable_device(pci) < 0) {
  991. printk(KERN_ERR "atiixp-modem: pci_enable_device failed, "
  992. "disabling device\n");
  993. snd_card_disconnect(card);
  994. return -EIO;
  995. }
  996. pci_set_master(pci);
  997. snd_atiixp_aclink_reset(chip);
  998. snd_atiixp_chip_start(chip);
  999. for (i = 0; i < NUM_ATI_CODECS; i++)
  1000. snd_ac97_resume(chip->ac97[i]);
  1001. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1002. return 0;
  1003. }
  1004. #endif /* CONFIG_PM */
  1005. #ifdef CONFIG_PROC_FS
  1006. /*
  1007. * proc interface for register dump
  1008. */
  1009. static void snd_atiixp_proc_read(struct snd_info_entry *entry,
  1010. struct snd_info_buffer *buffer)
  1011. {
  1012. struct atiixp_modem *chip = entry->private_data;
  1013. int i;
  1014. for (i = 0; i < 256; i += 4)
  1015. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  1016. }
  1017. static void __devinit snd_atiixp_proc_init(struct atiixp_modem *chip)
  1018. {
  1019. struct snd_info_entry *entry;
  1020. if (! snd_card_proc_new(chip->card, "atiixp-modem", &entry))
  1021. snd_info_set_text_ops(entry, chip, snd_atiixp_proc_read);
  1022. }
  1023. #else
  1024. #define snd_atiixp_proc_init(chip)
  1025. #endif
  1026. /*
  1027. * destructor
  1028. */
  1029. static int snd_atiixp_free(struct atiixp_modem *chip)
  1030. {
  1031. if (chip->irq < 0)
  1032. goto __hw_end;
  1033. snd_atiixp_chip_stop(chip);
  1034. __hw_end:
  1035. if (chip->irq >= 0)
  1036. free_irq(chip->irq, chip);
  1037. if (chip->remap_addr)
  1038. iounmap(chip->remap_addr);
  1039. pci_release_regions(chip->pci);
  1040. pci_disable_device(chip->pci);
  1041. kfree(chip);
  1042. return 0;
  1043. }
  1044. static int snd_atiixp_dev_free(struct snd_device *device)
  1045. {
  1046. struct atiixp_modem *chip = device->device_data;
  1047. return snd_atiixp_free(chip);
  1048. }
  1049. /*
  1050. * constructor for chip instance
  1051. */
  1052. static int __devinit snd_atiixp_create(struct snd_card *card,
  1053. struct pci_dev *pci,
  1054. struct atiixp_modem **r_chip)
  1055. {
  1056. static struct snd_device_ops ops = {
  1057. .dev_free = snd_atiixp_dev_free,
  1058. };
  1059. struct atiixp_modem *chip;
  1060. int err;
  1061. if ((err = pci_enable_device(pci)) < 0)
  1062. return err;
  1063. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1064. if (chip == NULL) {
  1065. pci_disable_device(pci);
  1066. return -ENOMEM;
  1067. }
  1068. spin_lock_init(&chip->reg_lock);
  1069. mutex_init(&chip->open_mutex);
  1070. chip->card = card;
  1071. chip->pci = pci;
  1072. chip->irq = -1;
  1073. if ((err = pci_request_regions(pci, "ATI IXP MC97")) < 0) {
  1074. kfree(chip);
  1075. pci_disable_device(pci);
  1076. return err;
  1077. }
  1078. chip->addr = pci_resource_start(pci, 0);
  1079. chip->remap_addr = pci_ioremap_bar(pci, 0);
  1080. if (chip->remap_addr == NULL) {
  1081. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1082. snd_atiixp_free(chip);
  1083. return -EIO;
  1084. }
  1085. if (request_irq(pci->irq, snd_atiixp_interrupt, IRQF_SHARED,
  1086. KBUILD_MODNAME, chip)) {
  1087. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1088. snd_atiixp_free(chip);
  1089. return -EBUSY;
  1090. }
  1091. chip->irq = pci->irq;
  1092. pci_set_master(pci);
  1093. synchronize_irq(chip->irq);
  1094. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1095. snd_atiixp_free(chip);
  1096. return err;
  1097. }
  1098. snd_card_set_dev(card, &pci->dev);
  1099. *r_chip = chip;
  1100. return 0;
  1101. }
  1102. static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  1103. const struct pci_device_id *pci_id)
  1104. {
  1105. struct snd_card *card;
  1106. struct atiixp_modem *chip;
  1107. int err;
  1108. err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  1109. if (err < 0)
  1110. return err;
  1111. strcpy(card->driver, "ATIIXP-MODEM");
  1112. strcpy(card->shortname, "ATI IXP Modem");
  1113. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1114. goto __error;
  1115. card->private_data = chip;
  1116. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1117. goto __error;
  1118. if ((err = snd_atiixp_mixer_new(chip, ac97_clock)) < 0)
  1119. goto __error;
  1120. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1121. goto __error;
  1122. snd_atiixp_proc_init(chip);
  1123. snd_atiixp_chip_start(chip);
  1124. sprintf(card->longname, "%s rev %x at 0x%lx, irq %i",
  1125. card->shortname, pci->revision, chip->addr, chip->irq);
  1126. if ((err = snd_card_register(card)) < 0)
  1127. goto __error;
  1128. pci_set_drvdata(pci, card);
  1129. return 0;
  1130. __error:
  1131. snd_card_free(card);
  1132. return err;
  1133. }
  1134. static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  1135. {
  1136. snd_card_free(pci_get_drvdata(pci));
  1137. pci_set_drvdata(pci, NULL);
  1138. }
  1139. static struct pci_driver driver = {
  1140. .name = KBUILD_MODNAME,
  1141. .id_table = snd_atiixp_ids,
  1142. .probe = snd_atiixp_probe,
  1143. .remove = __devexit_p(snd_atiixp_remove),
  1144. #ifdef CONFIG_PM
  1145. .suspend = snd_atiixp_suspend,
  1146. .resume = snd_atiixp_resume,
  1147. #endif
  1148. };
  1149. static int __init alsa_card_atiixp_init(void)
  1150. {
  1151. return pci_register_driver(&driver);
  1152. }
  1153. static void __exit alsa_card_atiixp_exit(void)
  1154. {
  1155. pci_unregister_driver(&driver);
  1156. }
  1157. module_init(alsa_card_atiixp_init)
  1158. module_exit(alsa_card_atiixp_exit)