atiixp_modem.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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, struct pt_regs *regs)
  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;
  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_set_power_state(pci, PCI_D3hot);
  974. pci_disable_device(pci);
  975. pci_save_state(pci);
  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_restore_state(pci);
  984. pci_enable_device(pci);
  985. pci_set_power_state(pci, PCI_D0);
  986. pci_set_master(pci);
  987. snd_atiixp_aclink_reset(chip);
  988. snd_atiixp_chip_start(chip);
  989. for (i = 0; i < NUM_ATI_CODECS; i++)
  990. snd_ac97_resume(chip->ac97[i]);
  991. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  992. return 0;
  993. }
  994. #endif /* CONFIG_PM */
  995. #ifdef CONFIG_PROC_FS
  996. /*
  997. * proc interface for register dump
  998. */
  999. static void snd_atiixp_proc_read(struct snd_info_entry *entry,
  1000. struct snd_info_buffer *buffer)
  1001. {
  1002. struct atiixp_modem *chip = entry->private_data;
  1003. int i;
  1004. for (i = 0; i < 256; i += 4)
  1005. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  1006. }
  1007. static void __devinit snd_atiixp_proc_init(struct atiixp_modem *chip)
  1008. {
  1009. struct snd_info_entry *entry;
  1010. if (! snd_card_proc_new(chip->card, "atiixp-modem", &entry))
  1011. snd_info_set_text_ops(entry, chip, snd_atiixp_proc_read);
  1012. }
  1013. #else
  1014. #define snd_atiixp_proc_init(chip)
  1015. #endif
  1016. /*
  1017. * destructor
  1018. */
  1019. static int snd_atiixp_free(struct atiixp_modem *chip)
  1020. {
  1021. if (chip->irq < 0)
  1022. goto __hw_end;
  1023. snd_atiixp_chip_stop(chip);
  1024. synchronize_irq(chip->irq);
  1025. __hw_end:
  1026. if (chip->irq >= 0)
  1027. free_irq(chip->irq, chip);
  1028. if (chip->remap_addr)
  1029. iounmap(chip->remap_addr);
  1030. pci_release_regions(chip->pci);
  1031. pci_disable_device(chip->pci);
  1032. kfree(chip);
  1033. return 0;
  1034. }
  1035. static int snd_atiixp_dev_free(struct snd_device *device)
  1036. {
  1037. struct atiixp_modem *chip = device->device_data;
  1038. return snd_atiixp_free(chip);
  1039. }
  1040. /*
  1041. * constructor for chip instance
  1042. */
  1043. static int __devinit snd_atiixp_create(struct snd_card *card,
  1044. struct pci_dev *pci,
  1045. struct atiixp_modem **r_chip)
  1046. {
  1047. static struct snd_device_ops ops = {
  1048. .dev_free = snd_atiixp_dev_free,
  1049. };
  1050. struct atiixp_modem *chip;
  1051. int err;
  1052. if ((err = pci_enable_device(pci)) < 0)
  1053. return err;
  1054. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1055. if (chip == NULL) {
  1056. pci_disable_device(pci);
  1057. return -ENOMEM;
  1058. }
  1059. spin_lock_init(&chip->reg_lock);
  1060. mutex_init(&chip->open_mutex);
  1061. chip->card = card;
  1062. chip->pci = pci;
  1063. chip->irq = -1;
  1064. if ((err = pci_request_regions(pci, "ATI IXP MC97")) < 0) {
  1065. kfree(chip);
  1066. pci_disable_device(pci);
  1067. return err;
  1068. }
  1069. chip->addr = pci_resource_start(pci, 0);
  1070. chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
  1071. if (chip->remap_addr == NULL) {
  1072. snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
  1073. snd_atiixp_free(chip);
  1074. return -EIO;
  1075. }
  1076. if (request_irq(pci->irq, snd_atiixp_interrupt, IRQF_DISABLED|IRQF_SHARED,
  1077. card->shortname, chip)) {
  1078. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1079. snd_atiixp_free(chip);
  1080. return -EBUSY;
  1081. }
  1082. chip->irq = pci->irq;
  1083. pci_set_master(pci);
  1084. synchronize_irq(chip->irq);
  1085. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1086. snd_atiixp_free(chip);
  1087. return err;
  1088. }
  1089. snd_card_set_dev(card, &pci->dev);
  1090. *r_chip = chip;
  1091. return 0;
  1092. }
  1093. static int __devinit snd_atiixp_probe(struct pci_dev *pci,
  1094. const struct pci_device_id *pci_id)
  1095. {
  1096. struct snd_card *card;
  1097. struct atiixp_modem *chip;
  1098. unsigned char revision;
  1099. int err;
  1100. card = snd_card_new(index, id, THIS_MODULE, 0);
  1101. if (card == NULL)
  1102. return -ENOMEM;
  1103. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  1104. strcpy(card->driver, "ATIIXP-MODEM");
  1105. strcpy(card->shortname, "ATI IXP Modem");
  1106. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1107. goto __error;
  1108. card->private_data = chip;
  1109. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1110. goto __error;
  1111. if ((err = snd_atiixp_mixer_new(chip, ac97_clock)) < 0)
  1112. goto __error;
  1113. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1114. goto __error;
  1115. snd_atiixp_proc_init(chip);
  1116. snd_atiixp_chip_start(chip);
  1117. sprintf(card->longname, "%s rev %x at 0x%lx, irq %i",
  1118. card->shortname, revision, chip->addr, chip->irq);
  1119. if ((err = snd_card_register(card)) < 0)
  1120. goto __error;
  1121. pci_set_drvdata(pci, card);
  1122. return 0;
  1123. __error:
  1124. snd_card_free(card);
  1125. return err;
  1126. }
  1127. static void __devexit snd_atiixp_remove(struct pci_dev *pci)
  1128. {
  1129. snd_card_free(pci_get_drvdata(pci));
  1130. pci_set_drvdata(pci, NULL);
  1131. }
  1132. static struct pci_driver driver = {
  1133. .name = "ATI IXP MC97 controller",
  1134. .id_table = snd_atiixp_ids,
  1135. .probe = snd_atiixp_probe,
  1136. .remove = __devexit_p(snd_atiixp_remove),
  1137. #ifdef CONFIG_PM
  1138. .suspend = snd_atiixp_suspend,
  1139. .resume = snd_atiixp_resume,
  1140. #endif
  1141. };
  1142. static int __init alsa_card_atiixp_init(void)
  1143. {
  1144. return pci_register_driver(&driver);
  1145. }
  1146. static void __exit alsa_card_atiixp_exit(void)
  1147. {
  1148. pci_unregister_driver(&driver);
  1149. }
  1150. module_init(alsa_card_atiixp_init)
  1151. module_exit(alsa_card_atiixp_exit)