atiixp_modem.c 36 KB

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